Hello,

I am writing some code (for learning / proof of concept) and I am
using OpenJPA for the persistence framework.

Right now, I have two objects - User and Subscription. There is a
OneToMany relationship that one user can have many Subscriptions (or
none). A subscription should have a composite key based on two of it's
own string fields and the user object that it is linked to...

Here is how I have it coded currently -

User.java -
@Entity
@Table(name="reader_users")
public class User {

       @Id
       @Column(length=32)
       private String username;

       @Column(length=64)
       private String fromAddress;

       @Column(length=255)
       private String fullName;

       @Column(length=32)
       private String password;

       @Column(length=255)
       private String replyToAddress;

       @OneToMany(cascade =PERSIST)
       private List<Subscription> subscriptions;

...
}

Subscription.java -
@Entity
@Table(name="reader_subscriptions")
@IdClass(Subscription.SubscriptionPK.class)
public class Subscription {

       @Id
       @Column(name="host", length=255)
       private String host;

       @Column(name="server_type", length=16)
       private String type;

       @Id
       @Column(name="username", length=32)
       private String username;

       @Column(name="password", length=32)
       private String password;

       @Column(name="auto_connect")
       private boolean autoConnect;

       @Id
       @ManyToOne
       @JoinColumn(name="username")
       private User user;

       public static class SubscriptionPK {

               private String host;
               private String username;
               private User user;

               @Override
               public int hashCode() {
                       ...
               }

               @Override
               public boolean equals(Object obj) {
                       ...
               }
               ...
       }
...
}

In the code above, the '...' represent where I've snipped the code for
the sake of bandwidth. I am new to JPA and I could be doing something
wrong, so I am looking for feedback. Specifically, when I run the
mapping tool, I get the following error message -

Exception in thread "main" <openjpa-1.0.0-r420667:568756 fatal user
error> org.apache.openjpa.util.MetaDataException: The id class
specified by type "class
org.apache.struts2.apps.mailreader.jpa.Subscription" does not match
the primary key fields of the class.  Make sure your identity class
has the same primary keys as your persistent type, including pk field
types. Mismatched property: "user"

Right now, I suppose that the combination of host/username/user does
not need to be the primary key. If it's not, is there a way I can
enforce uniqueness? Is this just a crappy design and I should be doing
it differently?

-- 
Wesley Wannemacher
President, Head Engineer/Consultant
WanTii, Inc.
http://www.wantii.com

Reply via email to