Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with bidirection ManyToOne

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)
...
like image 499
dharga Avatar asked Mar 17 '10 19:03

dharga


2 Answers

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>.

like image 130
Alex Cheng Avatar answered Oct 15 '22 05:10

Alex Cheng


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]

like image 31
Durgadas Kamath Avatar answered Oct 15 '22 06:10

Durgadas Kamath