问题描述
我创建了两个表:
@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来自何处? 我怎样才能解决这个问题 ?
1楼
我认为你必须使用mappedBy
值的@OneToMany
标注在InstanceOfClass
实体,就像这样:
@Entity
@Table(name="instanceOfClass")
public class InstanceOfClass {
(...)
@OneToMany(fetch=FetchType.LAZY, mappedBy="instanceOfClass")
public List<InstanceOfDataProperty> getDataProperties() {
return dataProperties;
}
(...)
}
其中instanceOfClass
应该是InstanceOfDataProperty类中的获取方法的名称。
从第37.1.5.1章:
双向关系必须遵循这些规则。
双向关系的反面必须通过使用@ OneToOne,@ OneToMany或@ManyToMany批注的mappingBy元素来引用其所属的面。
mapledBy元素指定实体中作为关系所有者的属性或字段。 多对一双向关系的多面一定不能定义mapledBy元素。 关系的多方面始终是拥有关系的一方。
我使用实体Person and Address(1:N)进行了快速测试。
首先我离开了mappedBy
,并命名表Person_Address
已创建。
添加mappedBy
,已经创建了一个表Address
。