我正在使用 Derby 数据库和 Hibernate 将表列拉入 Java 类字段。为名为 CourseCredits.class
的类提取数据时,我得到一个 MappingException。这是因为名称为 Course 的列的类型为 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;
...
}
是否可以将类(class)列中的字符串存储到
Course.class
的实例中? ,尤其是全局变量 name
?更新
按照 Bartosz 的建议,这些类现在看起来像这样:
@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
),这是真的。我对数据库一无所知,所以如果这是一个新手问题,请原谅我,但是这个名字的意义是什么 CourseId
在 @JoinColumn
?它似乎表示一个新的列名。我看到它在我能找到的每个示例中都使用过,但在定义它之后从未引用过它。
最佳答案
为此使用关联。
您需要决定基数(一对一?多对多?)
假设一对一和您需要的情况
@OneToOne
@JoinColumn(name = "course_id")
private Course course;
使用关联时,必须存在链接表(在您的情况下是诅咒)。您可以手动创建它或从 Hibernate 注释生成数据库模式。
Hibernate 文档包含所有案例的示例。文档包括基础表结构。
更多详情请查看 http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#associations
关于hibernate - 将数据库值重定向到另一个对象的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43616338/