You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Ajay Aggarwal <aa...@covergence.com> on 2007/10/31 18:34:42 UTC

when ManyToOne is part of the primaryKey

I have 2 simple tables, user and contacts. A user may have many
contacts. So I use OneToMany and ManyToOne in corresponding entities:

Class User {
   @Id 
   long userId;
   String username;

   @OneToMany (mappedBy="user")
   List <UserContact> contacts;
}

Class UserContact {
   String contactPhone;
   String contactName;

  @ManyToOne
  User user;
}

Now I am struggling with the UserContact entity:
Q1. How do I specify that it's PRIMARY KEY=user+contactPhone? 
Q2. How do I specify a UNIQUE constraint on a combination of
user+contactName?

Thanks.

RE: when ManyToOne is part of the primaryKey

Posted by Ajay Aggarwal <aa...@covergence.com>.
I am just getting back to this. While trying to use the ant task for
application identity tool, I am getting following error. Any ideas what
is it that it's not liking? 

appids:
[appidtool] <4|true|0.9.7>
org.apache.openjpa.util.InvalidStateException:Attempt to run the
application id tool under metadata configuration that does not support
it.  The application id tool can only run under certain metadata factory
settings.
[appidtool]     at
org.apache.openjpa.enhance.ApplicationIdTool.loadObjectIds(ApplicationId
Tool.java:1442)
[appidtool]     at
org.apache.openjpa.enhance.ApplicationIdTool.run(ApplicationIdTool.java:
1343)
[appidtool]     at
org.apache.openjpa.ant.ApplicationIdToolTask.executeOn(ApplicationIdTool
Task.java:106)
[appidtool]     at
org.apache.openjpa.lib.ant.AbstractTask.execute(AbstractTask.java:166)

-Ajay



-----Original Message-----
From: Michael Dick [mailto:michael.d.dick@gmail.com] 
Sent: Thursday, November 01, 2007 5:24 PM
To: users@openjpa.apache.org
Subject: Re: when ManyToOne is part of the primaryKey

The misunderstanding is mine, I'm sorry about that.

I believe just moving the @Id from contactName to user

public class UserContact {
   @Id
   String contactPhone;

   String contactName;

   @Id
   @ManyToOne
   User user;
   . . .
}

Then generate the composite ID class, and so on.

The SQL will look a bit different, but from some limited testing it
looks like it will do what you want.

-Mike

On Nov 1, 2007 2:41 PM, Ajay Aggarwal <aa...@covergence.com> wrote:
> Mike,
>
> 1) You probably misunderstood me. I want the primary key for
UserContact
> class to be a composite of user(the @ManyToOne field) and
contactPhone.
>
> 2) similarly I want combination of user (again the @ManyToOne field)
and
> contactName to be unique inside UserContact class.
>
> Or did I miss something in your response?
>
> -Ajay
>
>
> -----Original Message-----
> From: michael.d.dick@gmail.com [mailto:michael.d.dick@gmail.com] On
> Behalf Of Michael Dick
> Sent: Thursday, November 01, 2007 11:45 AM
> To: users@openjpa.apache.org
> Subject: Re: when ManyToOne is part of the primaryKey
>
> Hi,
>
> 1.) You probably want to create a composite ID class.
>
> @Entity
> public class UserContact {
>     @Id
>     String contactPhone;
>     @Id
>     String contactName;
>
>     @ManyToOne
>     User user;
>     . . .
> }
>
> You then need to create a composite ID class. You can do this by hand,
> or have the OpenJPA Application ID tool generate one for you (I'd
> prefer to use the tool). Here's how I created one.
>
> Assuming your java code is in src/main/java/
> # You will need to add OpenJPA, its dependencies and
> META-INF/persistence.xml to your classpath before running the command.
> $ java org.apache.openjpa.enhance.ApplicationIdTool -s Id -d
> src/main/java src/test/main/package/UserContact.java
>
> This will generate src/main/java/UserContactId.java. You can then add
> @IdClass to the entity and compile, enhance, package etc. Here's where
> you'd add the @IdClass :
> @Entity
> @IdClass(value=UserContactId.class)
> public class UserContact {
> . . .
> }
>
> 2.) If you let OpenJPA generate the tables for you (via the
> MappingTool, or openjpa.jdbc.SynchronizeMappings configuration
> property) it will create the tables like this (on Derby) :
>
> CREATE TABLE MY_USER (U_ID BIGINT NOT NULL, U_NAME VARCHAR(255),
> PRIMARY KEY (U_ID))
> CREATE TABLE UserContact (contactName VARCHAR(255) NOT NULL,
> contactPhone VARCHAR(255) NOT NULL, user_U_ID BIGINT, PRIMARY KEY
> (contactName, contactPhone))
> CREATE INDEX I_SRCNTCT_USER ON UserContact (user_U_ID)
>
> Your mileage may vary on other databases :-)
>
> Hope this helps,
> -Mike
>
>
> On Oct 31, 2007 12:34 PM, Ajay Aggarwal <aa...@covergence.com>
> wrote:
> > I have 2 simple tables, user and contacts. A user may have many
> > contacts. So I use OneToMany and ManyToOne in corresponding
entities:
> >
> > Class User {
> >    @Id
> >    long userId;
> >    String username;
> >
> >    @OneToMany (mappedBy="user")
> >    List <UserContact> contacts;
> > }
> >
> > Class UserContact {
> >    String contactPhone;
> >    String contactName;
> >
> >   @ManyToOne
> >   User user;
> > }
> >
> > Now I am struggling with the UserContact entity:
> > Q1. How do I specify that it's PRIMARY KEY=user+contactPhone?
> > Q2. How do I specify a UNIQUE constraint on a combination of
> > user+contactName?
> >
> > Thanks.
> >
>

Re: when ManyToOne is part of the primaryKey

Posted by Michael Dick <mi...@gmail.com>.
The misunderstanding is mine, I'm sorry about that.

I believe just moving the @Id from contactName to user

public class UserContact {
   @Id
   String contactPhone;

   String contactName;

   @Id
   @ManyToOne
   User user;
   . . .
}

Then generate the composite ID class, and so on.

The SQL will look a bit different, but from some limited testing it
looks like it will do what you want.

-Mike

On Nov 1, 2007 2:41 PM, Ajay Aggarwal <aa...@covergence.com> wrote:
> Mike,
>
> 1) You probably misunderstood me. I want the primary key for UserContact
> class to be a composite of user(the @ManyToOne field) and contactPhone.
>
> 2) similarly I want combination of user (again the @ManyToOne field) and
> contactName to be unique inside UserContact class.
>
> Or did I miss something in your response?
>
> -Ajay
>
>
> -----Original Message-----
> From: michael.d.dick@gmail.com [mailto:michael.d.dick@gmail.com] On
> Behalf Of Michael Dick
> Sent: Thursday, November 01, 2007 11:45 AM
> To: users@openjpa.apache.org
> Subject: Re: when ManyToOne is part of the primaryKey
>
> Hi,
>
> 1.) You probably want to create a composite ID class.
>
> @Entity
> public class UserContact {
>     @Id
>     String contactPhone;
>     @Id
>     String contactName;
>
>     @ManyToOne
>     User user;
>     . . .
> }
>
> You then need to create a composite ID class. You can do this by hand,
> or have the OpenJPA Application ID tool generate one for you (I'd
> prefer to use the tool). Here's how I created one.
>
> Assuming your java code is in src/main/java/
> # You will need to add OpenJPA, its dependencies and
> META-INF/persistence.xml to your classpath before running the command.
> $ java org.apache.openjpa.enhance.ApplicationIdTool -s Id -d
> src/main/java src/test/main/package/UserContact.java
>
> This will generate src/main/java/UserContactId.java. You can then add
> @IdClass to the entity and compile, enhance, package etc. Here's where
> you'd add the @IdClass :
> @Entity
> @IdClass(value=UserContactId.class)
> public class UserContact {
> . . .
> }
>
> 2.) If you let OpenJPA generate the tables for you (via the
> MappingTool, or openjpa.jdbc.SynchronizeMappings configuration
> property) it will create the tables like this (on Derby) :
>
> CREATE TABLE MY_USER (U_ID BIGINT NOT NULL, U_NAME VARCHAR(255),
> PRIMARY KEY (U_ID))
> CREATE TABLE UserContact (contactName VARCHAR(255) NOT NULL,
> contactPhone VARCHAR(255) NOT NULL, user_U_ID BIGINT, PRIMARY KEY
> (contactName, contactPhone))
> CREATE INDEX I_SRCNTCT_USER ON UserContact (user_U_ID)
>
> Your mileage may vary on other databases :-)
>
> Hope this helps,
> -Mike
>
>
> On Oct 31, 2007 12:34 PM, Ajay Aggarwal <aa...@covergence.com>
> wrote:
> > I have 2 simple tables, user and contacts. A user may have many
> > contacts. So I use OneToMany and ManyToOne in corresponding entities:
> >
> > Class User {
> >    @Id
> >    long userId;
> >    String username;
> >
> >    @OneToMany (mappedBy="user")
> >    List <UserContact> contacts;
> > }
> >
> > Class UserContact {
> >    String contactPhone;
> >    String contactName;
> >
> >   @ManyToOne
> >   User user;
> > }
> >
> > Now I am struggling with the UserContact entity:
> > Q1. How do I specify that it's PRIMARY KEY=user+contactPhone?
> > Q2. How do I specify a UNIQUE constraint on a combination of
> > user+contactName?
> >
> > Thanks.
> >
>

RE: when ManyToOne is part of the primaryKey

Posted by Ajay Aggarwal <aa...@covergence.com>.
Mike,

1) You probably misunderstood me. I want the primary key for UserContact
class to be a composite of user(the @ManyToOne field) and contactPhone.

2) similarly I want combination of user (again the @ManyToOne field) and
contactName to be unique inside UserContact class.

Or did I miss something in your response?

-Ajay

-----Original Message-----
From: michael.d.dick@gmail.com [mailto:michael.d.dick@gmail.com] On
Behalf Of Michael Dick
Sent: Thursday, November 01, 2007 11:45 AM
To: users@openjpa.apache.org
Subject: Re: when ManyToOne is part of the primaryKey

Hi,

1.) You probably want to create a composite ID class.

@Entity
public class UserContact {
    @Id
    String contactPhone;
    @Id
    String contactName;

    @ManyToOne
    User user;
    . . .
}

You then need to create a composite ID class. You can do this by hand,
or have the OpenJPA Application ID tool generate one for you (I'd
prefer to use the tool). Here's how I created one.

Assuming your java code is in src/main/java/
# You will need to add OpenJPA, its dependencies and
META-INF/persistence.xml to your classpath before running the command.
$ java org.apache.openjpa.enhance.ApplicationIdTool -s Id -d
src/main/java src/test/main/package/UserContact.java

This will generate src/main/java/UserContactId.java. You can then add
@IdClass to the entity and compile, enhance, package etc. Here's where
you'd add the @IdClass :
@Entity
@IdClass(value=UserContactId.class)
public class UserContact {
. . .
}

2.) If you let OpenJPA generate the tables for you (via the
MappingTool, or openjpa.jdbc.SynchronizeMappings configuration
property) it will create the tables like this (on Derby) :

CREATE TABLE MY_USER (U_ID BIGINT NOT NULL, U_NAME VARCHAR(255),
PRIMARY KEY (U_ID))
CREATE TABLE UserContact (contactName VARCHAR(255) NOT NULL,
contactPhone VARCHAR(255) NOT NULL, user_U_ID BIGINT, PRIMARY KEY
(contactName, contactPhone))
CREATE INDEX I_SRCNTCT_USER ON UserContact (user_U_ID)

Your mileage may vary on other databases :-)

Hope this helps,
-Mike


On Oct 31, 2007 12:34 PM, Ajay Aggarwal <aa...@covergence.com>
wrote:
> I have 2 simple tables, user and contacts. A user may have many
> contacts. So I use OneToMany and ManyToOne in corresponding entities:
>
> Class User {
>    @Id
>    long userId;
>    String username;
>
>    @OneToMany (mappedBy="user")
>    List <UserContact> contacts;
> }
>
> Class UserContact {
>    String contactPhone;
>    String contactName;
>
>   @ManyToOne
>   User user;
> }
>
> Now I am struggling with the UserContact entity:
> Q1. How do I specify that it's PRIMARY KEY=user+contactPhone?
> Q2. How do I specify a UNIQUE constraint on a combination of
> user+contactName?
>
> Thanks.
>

Re: when ManyToOne is part of the primaryKey

Posted by Michael Dick <mi...@apache.org>.
Hi,

1.) You probably want to create a composite ID class.

@Entity
public class UserContact {
    @Id
    String contactPhone;
    @Id
    String contactName;

    @ManyToOne
    User user;
    . . .
}

You then need to create a composite ID class. You can do this by hand,
or have the OpenJPA Application ID tool generate one for you (I'd
prefer to use the tool). Here's how I created one.

Assuming your java code is in src/main/java/
# You will need to add OpenJPA, its dependencies and
META-INF/persistence.xml to your classpath before running the command.
$ java org.apache.openjpa.enhance.ApplicationIdTool -s Id -d
src/main/java src/test/main/package/UserContact.java

This will generate src/main/java/UserContactId.java. You can then add
@IdClass to the entity and compile, enhance, package etc. Here's where
you'd add the @IdClass :
@Entity
@IdClass(value=UserContactId.class)
public class UserContact {
. . .
}

2.) If you let OpenJPA generate the tables for you (via the
MappingTool, or openjpa.jdbc.SynchronizeMappings configuration
property) it will create the tables like this (on Derby) :

CREATE TABLE MY_USER (U_ID BIGINT NOT NULL, U_NAME VARCHAR(255),
PRIMARY KEY (U_ID))
CREATE TABLE UserContact (contactName VARCHAR(255) NOT NULL,
contactPhone VARCHAR(255) NOT NULL, user_U_ID BIGINT, PRIMARY KEY
(contactName, contactPhone))
CREATE INDEX I_SRCNTCT_USER ON UserContact (user_U_ID)

Your mileage may vary on other databases :-)

Hope this helps,
-Mike


On Oct 31, 2007 12:34 PM, Ajay Aggarwal <aa...@covergence.com> wrote:
> I have 2 simple tables, user and contacts. A user may have many
> contacts. So I use OneToMany and ManyToOne in corresponding entities:
>
> Class User {
>    @Id
>    long userId;
>    String username;
>
>    @OneToMany (mappedBy="user")
>    List <UserContact> contacts;
> }
>
> Class UserContact {
>    String contactPhone;
>    String contactName;
>
>   @ManyToOne
>   User user;
> }
>
> Now I am struggling with the UserContact entity:
> Q1. How do I specify that it's PRIMARY KEY=user+contactPhone?
> Q2. How do I specify a UNIQUE constraint on a combination of
> user+contactName?
>
> Thanks.
>