【问题标题】:Redirect database value to field of another object将数据库值重定向到另一个对象的字段
【发布时间】:2017-04-25 16:34:50
【问题描述】:

我正在使用 Derby 数据库和 Hibernate 将表列拉入 Java 类字段。在为名为@9​​87654321@ 的类提取数据时,我得到一个 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;
    ...
}

是否可以将课程列中的字符串存储到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();

问题不在于这个执行代码,因为如果我使用字符串类型的字段而不是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 有什么意义?它似乎表示一个新的列名。我看到在我能找到的每个示例中都使用了它,但在定义它之后从未引用过它。

【问题讨论】:

    标签: hibernate jpa derby model-associations


    【解决方案1】:

    为此使用关联。

    您需要决定基数(一对一?多对多?)

    假设一对一和你需要的情况

    @OneToOne
    @JoinColumn(name = "course_id")
    private Course course;
    

    使用关联时,必须存在链接表(在您的情况下是诅咒)。您可以手动创建它或从 Hibernate 注释生成 db 架构。

    Hibernate 文档包含所有案例的示例。文档包括基础表结构。

    更多详情请查看http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#associations

    【讨论】:

    • 感谢您的快速回复。我已经阅读了文档并调整了程序(请参阅有问题的更新)。不幸的是,它并没有完全解决问题。相反,它只会在我现在尝试查询表时抱怨。您能否解释一下course_id@JoinColumn 中的意义?从文档来看,它似乎引用了表 Course 中的列名,但没有这样的表。
    • 使用关联时,必须存在链接表(诅咒你)。您可以手动创建它或从 Hibernate 注释生成数据库模式。
    猜你喜欢
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    相关资源
    最近更新 更多