JPA concatenating table names for parent/child @OneToMany
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开发者_C百科 t0.CHILDMESSAGES_CHILD_ID = t1.CHILD_ID WHERE t0.PARENTMESSAGE_PARENT_ID = ?} [code=30000, state=42X05]
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.
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.
精彩评论