-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: @Id and @OneToOne
PostPosted: Mon Feb 15, 2010 12:29 pm 
Beginner
Beginner

Joined: Wed Sep 06, 2006 12:08 pm
Posts: 48
Location: Warsaw - Poland - Europe - Milky Way
Hello

Is it possible to use those two annotation on the same field?

Let's have a look at pice of Hibernate Annotation:

Code:
@Entity
public class Customer implements Serializable {
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="passport_fk")
    public Passport getPassport() {
        ...
    }

@Entity
public class Passport implements Serializable {
    @OneToOne(mappedBy = "passport")
    public Customer getOwner() {
    ...
}


S my question is:
Is it possible to use @Id annotation at Customer.passport field?
If yes - how to annotate this?

Kind Regards, Adam


Top
 Profile  
 
 Post subject: Re: @Id and @OneToOne
PostPosted: Tue Feb 16, 2010 6:24 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I recently tried to combinate @Id annotation with an relation annotation on the same property,
but it seems that hibernate don't supports this.
(I got always an annotation exception while buiding the sessionfactory).


Top
 Profile  
 
 Post subject: Re: @Id and @OneToOne
PostPosted: Tue Feb 16, 2010 10:56 am 
Beginner
Beginner

Joined: Wed Sep 06, 2006 12:08 pm
Posts: 48
Location: Warsaw - Poland - Europe - Milky Way
Thank you, pb00067, for your reply!

To be more precisely I will show what I am trying to gain. Unfortunately with no luck :(

Code:
@Entity
@Table(name="AWO_CUSTOMER")
public class Customer
{
   private long custId;

   @Id
   @Column(name="CU_ID", unique=true, nullable=false)
   @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="customer")
   public long getCustId(){
      return custId;
   }

   public void setCustId(long custId){
      this.custId = custId;
   }
}

@Entity
@Table(name="AWO_PASSPORT")
public class Passport
{
   private Customer customer;

   @Id
   @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY, optional=false)
   @JoinColumn(name="CU_ID")
   public Customer getCustomer(){
      return customer;
   }

   public void setCustomer(Customer customer){
      this.customer = customer;
   }
}


With such code I am not able to create EntityManagerFactory. See an exception:
Code:
javax.persistence.PersistenceException: [PersistenceUnit: bonobo-manager] Unable to build EntityManagerFactory
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
Caused by: org.hibernate.MappingException: Could not determine type for: pl.capgemini.tme.bonobo.model.adam.Customer, at table: AWO_PASSPORT, for columns: [org.hibernate.mapping.Column(customer)]
   at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:292)
   at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:276)
   at org.hibernate.mapping.RootClass.validate(RootClass.java:216)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:1135)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1320)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
   ... 5 more


Is there a way to get such mapping?

Kind regards,
Adam


Top
 Profile  
 
 Post subject: Re: @Id and @OneToOne
PostPosted: Tue Feb 16, 2010 1:03 pm 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
As said, you cannot use

@Id
and
@OneToOne
for the same property.
You always will get a org.hibernate.MappingException

To circumvent the problem I used a generated id as primary key,
and defined a unique constraint on the relation (@OneToOne) property.


Top
 Profile  
 
 Post subject: Re: @Id and @OneToOne
PostPosted: Tue Feb 16, 2010 7:19 pm 
Beginner
Beginner

Joined: Wed Sep 06, 2006 12:08 pm
Posts: 48
Location: Warsaw - Poland - Europe - Milky Way
pb00067, now I think, that I had to have a mind blackout ;]
The same functionality (described in my initial post) can be gained by regular @OneToOne Hibernate mapping.

If some is interested - I will give my example.

Relation between Person and Office is: 1 to {0 or 1}

There a 3 classes:
* PersonPk
* Person
* Office

Code:
import javax.persistence.Column;

public class PersonPk implements Serializable
{
   private static final long   serialVersionUID   = 7494175680933460258L;

   private String firstName;
   private String lastName;

   PersonPk(){}

   public PersonPk(String firstName, String lastName)
   {
      this.firstName = firstName;
      this.lastName  = lastName;
   }

   @Column(name="first_name")
   public String getFirstName()
   {
      return firstName;
   }

   public void setFirstName(String firstName)
   {
      this.firstName = firstName;
   }

   @Column(name="last_name")
   public String getLastName()
   {
      return lastName;
   }

   public void setLastName(String lastName)
   {
      this.lastName = lastName;
   }
}


Class Person:

Code:
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "PERSON")
public class Person
{
   private PersonPk pk;
   private String   zodiac;
   private Office   office;

   @EmbeddedId
   public PersonPk getPk(){
      return pk;
   }

   public void setPk(PersonPk pk){
      this.pk = pk;
   }

   @OneToOne(optional=true, fetch=FetchType.EAGER)
   @PrimaryKeyJoinColumn
   public Office getOffice(){
      return office;
   }

   public void setOffice(Office office){
      this.office = office;
   }

   @Column
   public String getZodiac(){
      return zodiac;
   }

   public void setZodiac(String zodiac){
      this.zodiac = zodiac;
   }
}


Class Office:

Code:
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "OFFICE")
public class Office
{
   private PersonPk pk;
   private Person   person;
   private String   address;

   @EmbeddedId
   public PersonPk getPk(){
      return pk;
   }

   public void setPk(PersonPk pk){
      this.pk = pk;
   }

   @OneToOne(optional=false, fetch=FetchType.LAZY)
   @PrimaryKeyJoinColumn
   public Person getPerson(){
      return person;
   }

   public void setPerson(Person person){
      this.person = person;
   }

   @Column(name="address")
   public String getAddress(){
      return address;
   }

   public void setAddress(String address){
      this.address = address;
   }
}


Take care!
Adam Woźniak / Poland


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.