双向ManyToOne的问题
我有两个实体:消息和生成的成员消息。字段数量比我在此处的代码中显示的字段要多,但这些字段应该足以为我的问题提供上下文。
我正在尝试建立一个简单的双向 OneToMany 关系,从我读过的所有内容来看,这就是它的完成方式。您会注意到我使用的是复合键。
我收到以下错误和堆栈跟踪。我在互联网上见过其他人也遇到过这个问题,但没有人有适合我的解决方案。有人看到这里有问题吗?
正如 ST 所建议的,我在 WAS 7.0 中使用 openJPA 1.2.2。我正在使用 RAD 7.5.4 和 EJB 3.0 进行开发。
Message.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;
}
StackTrace
[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)
...
I have two entities: Message and GeneratedMemberMessage. There are more fields than I'll show in code here, but these should provide enough for context to my question.
I'm trying to do a simple bidirectional OneToMany relationship and from all that I've read, this is how it's to be done. You'll notice that I'm using composite keys.
I get the following error and stacktrace. I've seen others who've had this problem around the interwebs, but nobody has a solution that works for me. Does anyone see a problem here?
As the ST suggests, I'm using openJPA 1.2.2 in WAS 7.0. I'm developing with RAD 7.5.4 and EJB 3.0.
Message.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;
...
}
My EJB method
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;
}
StackTrace
[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)
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我今天遇到了示例问题,并找到了适合我的解决方案。
我将类名添加到persistence.xml 中。尝试使用
Message
将Message
类添加到persistence.xml
中。I came across the sample problem today and found out a solution that worked for me.
I added the class name to the
persistence.xml
. Try to add theMessage
class to thepersistence.xml
using<class>Message</class>
.当您的实体 XYZ 具有子实体 ABC [可能是字段或集合]并且子实体(即我们示例中的 ABC)未在 persistence.xml [META-INF/persistence 中声明] 时,您会在 JPA 中遇到此 HandlerFieldStrategy 问题.xml]
You get this HandlerFieldStrategy issue in JPA when you have an Entity XYZ which has child Entity ABC [ may be a field or a collection ] and the child entity i.e ABC in our case is not declared in the persistence.xml [META-INF/persistence.xml]
为什么将 MessagePK 类设为静态?这难道不是问题的一部分吗?
Why have you made MessagePK class static? Isn't that somehow part of the problem?
我最近遇到了这个。我使用 hibernate 工具生成 JPA 注释并将其部署到 Websphere 7,这引发了此错误。
解决方案是什么?只需删除
mappedBy="XXX"
部分即可。之后一切都加载了。我仍然不知道为什么它有效。
编辑
忽略此答案 - 这可能会产生其他 JPQL 问题。我应该在发布之前进行更彻底的测试。对不起
I have recently run into this. I generated JPA annotations using hibernate tools and deployed them to Websphere 7 which threw this error.
The solution? Just remove the
mappedBy="XXX"
portion.Everything loaded after that. I still do not know why it worked.
Edit
Ignore this answer - this can create other JPQL problems. I should do more thorough testing before posting. Sorry
我也遇到了同样的问题。
消息不明确!
解决我的问题的是在实体对象及其中实现
equals()
和hashCode()
PK对象。希望它能帮助某人。
I was having the same problem.
The message is not clear!
What solved my problem was implementing
equals()
andhashCode()
in the entity object and its pk object.Hope it helps someone.
我有同样的问题,但就我而言,我混合了
<代码>
映射者=“foo”
其中 foo 必须声明该字段!不是专栏。
I had the same issue, but in my case I mixed
mappedBy = "foo"
where foo must declare the field! Not the column.