双向ManyToOne的问题

发布于 2024-08-26 03:01:46 字数 4176 浏览 5 评论 0原文

我有两个实体:消息和生成的成员消息。字段数量比我在此处的代码中显示的字段要多,但这些字段应该足以为我的问题提供上下文。

我正在尝试建立一个简单的双向 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 技术交流群。

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

发布评论

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

评论(6

焚却相思 2024-09-02 03:01:46

我今天遇到了示例问题,并找到了适合我的解决方案。

我将类名添加到persistence.xml 中。尝试使用 MessageMessage 类添加到 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 the Message class to the persistence.xml using <class>Message</class>.

洋洋洒洒 2024-09-02 03:01:46

当您的实体 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]

娇纵 2024-09-02 03:01:46

为什么将 MessagePK 类设为静态?这难道不是问题的一部分吗?

Why have you made MessagePK class static? Isn't that somehow part of the problem?

落在眉间の轻吻 2024-09-02 03:01:46

我最近遇到了这个。我使用 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

倾城°AllureLove 2024-09-02 03:01:46

我也遇到了同样的问题。

消息不明确!

解决我的问题的是在实体对象及其中实现equals()hashCode() PK对象。

希望它能帮助某人。

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.

Hope it helps someone.

爱殇璃 2024-09-02 03:01:46

我有同样的问题,但就我而言,我混合了
<代码>
映射者=“foo”

其中 foo 必须声明该字段!不是专栏。

I had the same issue, but in my case I mixed

mappedBy = "foo"

where foo must declare the field! Not the column.

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