我有两个实体:Message 和 GeneratedMemberMessage。这里的字段比我在代码中显示的要多,但是这些应该为我的问题提供足够的上下文。
我正在尝试做一个简单的双向 OneToMany 关系,从我读过的所有内容来看,这就是它的完成方式。您会注意到我正在使用复合键。
我收到以下错误和堆栈跟踪。我见过其他人在互联网上遇到过这个问题,但没有人有适合我的解决方案。有人在这里看到问题吗?
正如 ST 建议的那样,我在 WAS 7.0 中使用 openJPA 1.2.2。我正在使用 RAD 7.5.4 和 EJB 3.0 进行开发。
消息.java
@Entity
@Table(schema="dbo", name="Messages")
public class Message implements Serializable {
@Embeddable
public static class MessagePK implements Serializable {
@Column(name="SourceApplication")
String sourceApplication;
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="MessageId")
BigDecimal messageId;
//Getters and setters here
}
@EmbeddedId
MessagePK pk = new MessagePK();
...
@OneToMany(cascade={CascadeType.ALL}, mappedBy="message")
List<GeneratedMemberMessage> generatedMemberMessages = new ArrayList<GeneratedMemberMessage>();
...
}
GeneratedMemberMessage.java
@Entity
@Table(schema="dbo", name="GeneratedMemberMessages")
public class GeneratedMemberMessage implements Serializable {
@Embeddable
public static class GMMPK implements Serializable {
@Column(name="SourceApplication")
String sourceApplication;
@Column(name="MessageId")
int messageId;
@Column(name="MessageGenerationDateTime")
Date messageGenerationDateTime;
//Getters and setters here
}
...
@ManyToOne(optional = true)
@JoinColumns({
@JoinColumn(name="sourceApplication", referencedColumnName="SourceApplication"),
@JoinColumn(name="messageId", referencedColumnName="MessageId")
})
Message message;
...
}
我的 EJB 方法
public Message newMessage(String sourceApplication, String text, int priority, String type){
Message m = new Message(sourceApplication);
m.setMessageText(text);
m.setMessagePriority(priority);
m.setMessageType(type);
m.setSysLstUpdtUserId("me");
em.persist(m);
return m;
}
堆栈跟踪
[3/17/10 14:55:05:457 EDT] 0000001e SystemErr R javax.ejb.EJBException: See nested exception; nested exception is: <openjpa-1.2.2-SNAPSHOT-r422266:778978M-OPENJPA-975 fatal user error> org.apache.openjpa.persistence.ArgumentException: Field "com.bcbst.bamp.jpa.Message.generatedMemberMessages" cannot declare that it is mapped by another field. Its mapping strategy (org.apache.openjpa.jdbc.meta.strats.HandlerFieldStrategy) does not support mapping by another field.
[3/17/10 14:55:05:457 EDT] 0000001e SystemErr R Caused by: <openjpa-1.2.2-SNAPSHOT-r422266:778978M-OPENJPA-975 fatal user error> org.apache.openjpa.persistence.ArgumentException: Field "com.bcbst.bamp.jpa.Message.generatedMemberMessages" cannot declare that it is mapped by another field. Its mapping strategy (org.apache.openjpa.jdbc.meta.strats.HandlerFieldStrategy) does not support mapping by another field.
[3/17/10 14:55:05:457 EDT] 0000001e SystemErr R at org.apache.openjpa.jdbc.meta.strats.AbstractFieldStrategy.assertNotMappedBy(AbstractFieldStrategy.java:59)
[3/17/10 14:55:05:457 EDT] 0000001e SystemErr R at org.apache.openjpa.jdbc.meta.strats.HandlerFieldStrategy.map(HandlerFieldStrategy.java:71)
[3/17/10 14:55:05:457 EDT] 0000001e SystemErr R at org.apache.openjpa.jdbc.meta.FieldMapping.setStrategy(FieldMapping.java:121)
[3/17/10 14:55:05:457 EDT] 0000001e SystemErr R at org.apache.openjpa.jdbc.meta.RuntimeStrategyInstaller.installStrategy(RuntimeStrategyInstaller.java:80)
[3/17/10 14:55:05:457 EDT] 0000001e SystemErr R at org.apache.openjpa.jdbc.meta.FieldMapping.resolveMapping(FieldMapping.java:454)
[3/17/10 14:55:05:457 EDT] 0000001e SystemErr R at org.apache.openjpa.jdbc.meta.FieldMapping.resolve(FieldMapping.java:419)
[3/17/10 14:55:05:457 EDT] 0000001e SystemErr R at org.apache.openjpa.jdbc.meta.ClassMapping.resolveNonRelationMappings(ClassMapping.java:879)
[3/17/10 14:55:05:457 EDT] 0000001e SystemErr R at org.apache.openjpa.jdbc.meta.MappingRepository.prepareMapping(MappingRepository.java:339)
...