?
Solved

Composite primary key with annotations

Posted on 2011-10-21
10
Medium Priority
?
398 Views
Last Modified: 2012-06-27
Hello,
So i have 2 EJB Entity class: Account and Contact.
An Account is linked to one (and only one) Contact, so the primary key of Account should be id and Contact but i dont know how to do.
I dont want to have a "int id_Contact" instead of my Contact class and i dont want to create a "AccountPK" class as i have seen on some java exemple web site!
My Account class is an Id and ,a Contact and an login. i dont want to change that by adding some dirty tricks.
Here my code:
@Entity
@Table(name="account")
public class Account implements Serializable{
	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	@OneToOne(cascade={CascadeType.ALL})
	@JoinColumn(name="id_contact")
	private Contact contact;
	private String login;

Open in new window


@Entity
@Table(name="contact")
public class Contact implements Serializable {
         @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	private String name;

Open in new window

I have already tried by putting an annotation @Idclass above the class declaration and 2 @Id on the 2 parametes "id" and "Contact" but i have an error because "Contact" is not an idea (logical, its a Contact)
So how to do? i have looked at the  @JoinColumn to specify to use this column to create to Composite primary key...but no way apparently
I am using EJB 3.0 with native JAVA JPA (based on Hibernate) with JBOSS AS 7
Thanks
0
Comment
Question by:numtech
  • 6
  • 4
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37007467

I think this ahows similar example for person - address in this link (look below the xml coding which is on top, but below they show it in annotations);
http://www.roseindia.net/jpa/eclipsejpaexamples/jparelationship/one-to-one.shtml
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37007545
This is basically one-to-one correspondence, so I think they show it in

@OneToOne annotaion on person field  within class Address

0
 
LVL 47

Expert Comment

by:for_yan
ID: 37007589
This is another example:
http://en.wikibooks.org/wiki/Java_Persistence/OneToOne
Example of a OneToOne relationship annotations

@Entity
public class Employee {
  @Id
  @Column(name="EMP_ID")
  private long id;
  ...
  @OneToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="ADDRESS_ID")
  private Address address;
  ...
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:numtech
ID: 37016355
Its not just a one-ton-one relationship that i want. Because in youre example i can create 2 person with the same adress even if i put a OneToOne on the target table. Thats why i have to made a composite primary key, to prevent this.
Thus i dont want to modify the target table "Contact": A Contact dont have to know  "Account" table, more a i have other table that are linked to a contact.
So the problem is still not resolved, how to made a composite priamary key (for prevanting having 2 Account with the same Contact )
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37016423
Can't you specify Unique column for contact?
Because I guess you can have two contacts for account with one-to-one relationship between tables only if you have more than one row with the same contact?
Is there other way how it can happen?
0
 

Author Comment

by:numtech
ID: 37016674
I have tried:
@Column(unique=true)

Open in new window

but java told me :
@Column(s) not allowed on a @OneToOne property

for the moment i can do :
Contact cnt= new Contact("bob" ,"bob@mail.com")
createNewAccount(1 , Bob , cnt)
createNewAccount(2 , Alice , cnt)

Open in new window

But it dont want to allow that,it should throw an error because cnt is already linked with a Account. That's the role of a composite primary key.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37016697
Then maybe you can get along withhout one-to-one property?
Just declare contact unique and have join column through id's?
would not that be enough?
0
 

Accepted Solution

by:
numtech earned 0 total points
ID: 37016754
mmh...Finnaly i have made work the first solution :
@IdClass(Account.class)
public Account implements Serializable{
        @Id @GeneratedValue
	private int id;
	@Id
	@OneToOne(cascade={CascadeType.ALL})
	@JoinColumn(name="id_contact")
	private Contact contact;

Open in new window


it create a primary key composed by both id and contact and now i cannot create 2 Account with the same Contact :)
My mistake was using:
@GeneratedValue(strategy=GenerationType.IDENTITY)

Open in new window

the "(strategy=GenerationType.IDENTITY)" created an error misunderstable!

So finnaly the "clean" solution worked perfectly! Thanks by the way, trying several option with you help me figure it out!
Cheers,


0
 

Author Closing Comment

by:numtech
ID: 37049155
The best way to create a composite primary key with clean JPA annotation is by using this way
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37016761
Great,
Good it worked for you
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question