JPA 连接父/子表名称 @OneToMany
我们正在尝试使用基本的 @OneToMany 关系:
@Entity
@Table(name = "PARENT_MESSAGE")
public class ParentMessage {
@Id
@Column(name = "PARENT_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer parentId;
@OneToMany(mappedBy="parentMsg",fetch=FetchType.LAZY)
private List childMessages;
public List getChildMessages() {
return this.childMessages;
}
...
}
@Entity
@Table(name = "CHILD_MSG_USER_MAP")
public class ChildMessage {
@Id
@Column(name = "CHILD_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer childId;
@ManyToOne(optional=false,targetEntity=ParentMessage.class,cascade={CascadeType.REFRESH}, fetch=FetchType.LAZY)
@JoinColumn(name="PARENT_ID")
private ParentMessage parentMsg;
public ParentMessage getParentMsg() {
return parentMsg;
}
...
}
ChildMessage child = new ChildMessage();
em.getTransaction().begin();
ParentMessage parentMessage = (ParentMessage) em.find(ParentMessage.class, parentId);
child.setParentMsg(parentMessage);
List list = parentMessage.getChildMessages();
if(list == null) list = new ArrayList<ChildMessage>();
list.add(child);
em.getTransaction().commit();
我们收到以下错误。为什么 OpenJPA 将表名称连接到 APP.PARENT_MESSAGE_CHILD_MSG_USER_MAP?当然该表不存在。定义的表是 APP.PARENT_MESSAGE
和 APP.CHILD_MSG_USER_MAP
原因: org.apache.openjpa.lib.jdbc.ReportingSQLException: 表/视图 'APP.PARENT_MESSAGE_CHILD_MSG_USER_MAP' 不存在。 {选择 t1.CHILD_ID, t1.PARENT_ID、t1.CREATED_TIME、 t1.USER_ID 来自 APP.PARENT_MESSAGE_CHILD_MSG_USER_MAP t0 内连接 APP.CHILD_MSG_USER_MAP t1 开启 t0.CHILDMESSAGES_CHILD_ID = t1.CHILD_ID 位置 t0.PARENTMESSAGE_PARENT_ID = ?} [代码=30000,状态=42X05]
We are trying to use a basic @OneToMany relationship:
@Entity
@Table(name = "PARENT_MESSAGE")
public class ParentMessage {
@Id
@Column(name = "PARENT_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer parentId;
@OneToMany(mappedBy="parentMsg",fetch=FetchType.LAZY)
private List childMessages;
public List getChildMessages() {
return this.childMessages;
}
...
}
@Entity
@Table(name = "CHILD_MSG_USER_MAP")
public class ChildMessage {
@Id
@Column(name = "CHILD_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer childId;
@ManyToOne(optional=false,targetEntity=ParentMessage.class,cascade={CascadeType.REFRESH}, fetch=FetchType.LAZY)
@JoinColumn(name="PARENT_ID")
private ParentMessage parentMsg;
public ParentMessage getParentMsg() {
return parentMsg;
}
...
}
ChildMessage child = new ChildMessage();
em.getTransaction().begin();
ParentMessage parentMessage = (ParentMessage) em.find(ParentMessage.class, parentId);
child.setParentMsg(parentMessage);
List list = parentMessage.getChildMessages();
if(list == null) list = new ArrayList<ChildMessage>();
list.add(child);
em.getTransaction().commit();
We receive the following error. Why is OpenJPA concatenating the table names to APP.PARENT_MESSAGE_CHILD_MSG_USER_MAP
? Of course that table doesn't exist.. the tables defined are APP.PARENT_MESSAGE
and APP.CHILD_MSG_USER_MAP
Caused by:
org.apache.openjpa.lib.jdbc.ReportingSQLException:
Table/View
'APP.PARENT_MESSAGE_CHILD_MSG_USER_MAP'
does not exist. {SELECT t1.CHILD_ID,
t1.PARENT_ID, t1.CREATED_TIME,
t1.USER_ID FROM
APP.PARENT_MESSAGE_CHILD_MSG_USER_MAP
t0 INNER JOIN
APP.CHILD_MSG_USER_MAP t1 ON
t0.CHILDMESSAGES_CHILD_ID =
t1.CHILD_ID WHERE
t0.PARENTMESSAGE_PARENT_ID = ?}
[code=30000, state=42X05]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想要将mappedBy 属性添加到关系的拥有方。这告诉 JPA 这只是一种关系,而不是两种不同的关系。也许从很多方面来说。
You may want to add a mappedBy attribute to the owning side of the relationship. This tells JPA that it is only one relatiuonship and not two different relationships. Maybe on the many side.
您还必须在实体关系上使用
@JoinColumn
和可能的@JoinColumns
注释来告诉 JPA 如何计算关系。请注意,您必须将实体中已存在的字段指定为insertable=false, updatable=false
,因为实体中不能有两个同名的可写字段。You'd have to use the
@JoinColumn
and possibly@JoinColumns
annotations as well on your entity relationships to tell JPA how to work out the relationship. Note you'll have to specify those fields that are already present in an entity asinsertable=false, updatable=false
since you cannot have two write-able fields with the same name in an entity.