JPA/eclipse-link 2.6.0/mysql:生成的 table 名称不正确
JPA / eclipse-link 2.6.0 / mysql : incorrect generated table name
我创建了两个表:
@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:Table '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 class.
中 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.
我用实体 Person 和 Address (1:N) 进行了快速测试。首先,我省略了 mappedBy
,并创建了一个名为 Person_Address
的 table。添加 mappedBy
后,创建了 table Address
。
我创建了两个表:
@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:Table '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 class.
来自 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.
我用实体 Person 和 Address (1:N) 进行了快速测试。首先,我省略了 mappedBy
,并创建了一个名为 Person_Address
的 table。添加 mappedBy
后,创建了 table Address
。