我正在使用Derby數據庫和Hibernate將表列移入Java類字段。當爲一個名爲CourseCredits.class
的類提取數據時,我得到一個MappingException。這是因爲,與名字課程列是VARCHAR(30)
類型和相應的類字段是Course.class
類型:將數據庫值重定向到另一個對象的字段
@Entity
@Table(name="CourseCredits")
public class CourseCredit implements Serializable {
@Id
@Column(name="Course")
private Course course;
public CourseCredit(){
}
// getters and setters
}
如果我改變了全局變量course
爲String類型,然後該程序按預期工作:
@Entity
@Table(name="CourseCredits")
public class CourseCredit implements Serializable {
@Id
@Column(name="Course")
private String course;
...
}
鑑於類Course.class
看起來是這樣的:
public class Course {
private String name;
...
}
是否可以將來自課程列的字符串存儲到實例的Course.class
,特別是全局變量name
?
更新
繼巴爾託什的建議下,類現在這個樣子:
@Entity
@Table(name="CourseCredits")
public class CourseCredit implements Serializable {
@Id
@OneToOne
@JoinColumn(name = "CourseId")
private Course course;
...
}
@Entity
public class Course implements Serializable{
@Id
@Column(name="CourseId")
private String name;
...
}
我做出來的Course.class
實體,因爲對關聯映射指定實體之間的關係。不幸的是,這個錯誤還沒有解決。現在我收到一個錯誤ERROR 42X04
。這是我跑:
em.getTransaction().begin(); // em is the entity manager
List<CourseCredit> credits = this.em.createQuery("from CourseCredit").getResultList();
的問題並不在此執行代碼謊言,因爲如果我使用String類型,而不是Course
的場程序工作正常。錯誤輸出我得到的是:
Caused by: java.sql.SQLSyntaxErrorException: Column 'COURSECRED0_.COURSEID' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'COURSECRED0_.COURSEID' is not a column in the target table.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement42.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver42.newEmbedPreparedStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
... 19 more
這似乎抱怨不存在的列(CourseId
),這將是真實的。我對數據庫根本不瞭解,所以如果這是一個新手問題,請原諒我,但@JoinColumn
中名稱CourseId
的意義是什麼?它似乎表示一個新的列名稱。我看到它用在我能找到的每個例子中,但在定義它之後從未引用它。
感謝您的快速回復。我已閱讀了文檔並調整了程序(請參閱更新)。不幸的是,它不能解決問題。相反,它只是在我嘗試查詢表格時發出抱怨。你能否解釋'course_id'在'@ JoinColumn'中的意義?從文檔看來,它似乎是從「Course」表引用一個列名,但是沒有這樣的表。 – Babyburger
當使用關聯時,鏈表(對你有詛咒)必須存在。您可以手動創建它或從Hibernate註釋生成數據庫模式。 –