JPA 连接父/子表名称 @OneToMany

发布于 2024-08-25 04:13:18 字数 1788 浏览 3 评论 0原文

我们正在尝试使用基本的 @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_MESSAGEAPP.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不再让梦枯萎 2024-09-01 04:13:18

您可能想要将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.

如梦 2024-09-01 04:13:18

您还必须在实体关系上使用 @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 as insertable=false, updatable=false since you cannot have two write-able fields with the same name in an entity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文