简体   繁体   English

双向问题ManyToOne

[英]Problem with bidirection ManyToOne

I have two entities: Message and GeneratedMemberMessage. 我有两个实体:Message和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. 我正在尝试做一个简单的双向OneToMany关系,从我读过的所有内容来看,这就是它要做的事情。 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. 正如ST建议的那样,我在WAS 7.0中使用openJPA 1.2.2。 I'm developing with RAD 7.5.4 and EJB 3.0. 我正在使用RAD 7.5.4和EJB 3.0进行开发。

Message.java 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 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 我的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 came across the sample problem today and found out a solution that worked for me. 我今天遇到了样本问题,并找到了适合我的解决方案。

I added the class name to the persistence.xml . 我将类名添加到persistence.xml Try to add the Message class to the persistence.xml using <class>Message</class> . 尝试使用<class>Message</class>Message类添加到persistence.xml

当你有一个具有子实体ABC [可能是一个字段或一个集合]的实体XYZ时,你会在JPA中得到这个HandlerFieldStrategy问题,并且在我们的例子中没有在persistence.xml中声明子实体,即ABC [META-INF / persistence .XML]

I was having the same problem. 我遇到了同样的问题。

The message is not clear! 消息不清楚!

What solved my problem was implementing equals() and hashCode() in the entity object and its pk object . 解决了我的问题是在实体对象及其pk对象中实现equals()hashCode()

Hope it helps someone. 希望它可以帮助某人。

Why have you made MessagePK class static? 为什么你让MessagePK类静态? Isn't that somehow part of the problem? 这不是问题的一部分吗?

I had the same issue, but in my case I mixed mappedBy = "foo" where foo must declare the field! 我有同样的问题,但在我的情况下,我混合mappedBy = "foo" ,其中foo必须声明该字段! Not the column. 不是专栏。

I have recently run into this. 我最近碰到了这个。 I generated JPA annotations using hibernate tools and deployed them to Websphere 7 which threw this error. 我使用hibernate工具生成了JPA注释,并将它们部署到Websphere 7,这引发了这个错误。

The solution? 解决方案? Just remove the mappedBy="XXX" portion. 只需删除mappedBy="XXX"部分即可。

Everything loaded after that. 之后一切都加载了。 I still do not know why it worked. 我仍然不知道它为什么有效。

Edit 编辑

Ignore this answer - this can create other JPQL problems. 忽略这个答案 - 这可能会产生其他JPQL问题。 I should do more thorough testing before posting. 我应该在发布前做更全面的测试。 Sorry 抱歉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM