views:

273

answers:

1

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)
...
A: 

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

mgamer
Because you can't instantiate inner classes (non-static nested classes) from the outside (so using a static nested class is the way to go).
Pascal Thivent