gpt4 book ai didi

java - JPA/eclipse-link 2.6.0/mysql : incorrect generated table name

转载 作者:行者123 更新时间:2023-11-29 12:03:58 24 4
gpt4 key购买 nike

我创建了两个表:

@Entity
@Table(name="instanceOfClass")
public class InstanceOfClass {
(...)
@OneToMany(fetch=FetchType.LAZY)
public List<InstanceOfDataProperty> getDataProperties() {
return dataProperties;
}
(...)
}

@Entity
@Table(name="instanceOfDataProperty")
public class InstanceOfDataProperty {
(...)
@ManyToOne(optional=false,fetch=FetchType.LAZY)
@JoinColumn(referencedColumnName="instance_id", nullable=false, updatable=false)
public InstanceOfClass getInstance()
{
return this.instance;
}
(...)
}

当调用 getDataProperties() 时,我在日志中收到以下错误

Call: SELECT t1.id, t1.smallContent, t1.VALUE, t1.INSTANCE_id,t1.prop_id, t1.LARGECONTENT_id FROM instanceOfClass_instanceOfDataProperty t0, instanceOfDataProperty t1 WHERE ((t0.InstanceOfClass_id = ?) AND (t1.id = t0.dataProperties_id)) bind => [1 parameter bound]
Query: ReadAllQuery(name="file:/path/to/WEB-INF/classes/_jdbc/mydao" referenceClass=InstanceOfDataProperty sql="SELECT t1.id, t1.smallContent, t1.VALUE, t1.INSTANCE_id, t1.prop_id, t1.LARGECONTENT_id FROM instanceOfClass_instanceOfDataProperty t0, instanceOfDataProperty t1 WHERE ((t0.InstanceOfClass_id = ?) AND (t1.id = t0.dataProperties_id))")

(....) 导致:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'user_me.instanceOfClass_instanceOfDataProperty'不存在

这个user_me.instanceOfClass_instanceOfDataProperty来自哪里?我该如何解决这个问题?

最佳答案

我认为您必须在 InstanceOfClass 实体中使用 @OneToMany 注释的 mappedBy 值,如下所示:

@Entity
@Table(name="instanceOfClass")
public class InstanceOfClass {
(...)
@OneToMany(fetch=FetchType.LAZY, mappedBy="instanceOfClass")
public List<InstanceOfDataProperty> getDataProperties() {
return dataProperties;
}
(...)
}

其中 instanceOfClass 应该是 InstanceOfDataProperty 类中 getter 的名称。

来自JavaEE7 pdf ,第 37.1.5.1 章:

Bidirectional relationships must follow these rules.

The inverse side of a bidirectional relationship must refer to its owning side by using the mappedBy element of the @OneToOne, @OneToMany, or @ManyToMany annotation.

The mappedBy element designates the property or field in the entity that is the owner of the relationship. The many side of many-to-one bidirectional relationships must not define the mappedBy element. The many side is always the owning side of the relationship.

我使用实体“人员”和“地址”(1:N) 进行了快速测试。首先,我省略了mappedBy,并创建了一个名为Person_Address 的表。添加mappedBy后,就创建了一个表Address

关于java - JPA/eclipse-link 2.6.0/mysql : incorrect generated table name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31882360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com