-->
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.  [ 11 posts ] 
Author Message
 Post subject: @OneToOne@PrimaryKeyJoinColumn doesn't create key constraint
PostPosted: Sat Mar 04, 2006 11:12 pm 
Newbie

Joined: Sat Mar 04, 2006 10:20 pm
Posts: 3
I have a bidirectional @OneToOne association mapped using @PrimaryKeyJoinColumn, like this:

Code:
@Entity
public class Body {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    public String getId() {return _id;}

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    public Heart getHeart() {return _heart;}
    ...
}

@Entity
public class Heart {
    @Id
    @GeneratedValue(generator = "system-foreign")
    @GenericGenerator(name = "system-foreign", strategy = "foreign", parameters = {
        @Parameter(name = "property", value = "body")
    })
    public String getId() {return _id;}

    @OneToOne(mappedBy = "heart")
    public Body getBody() {return _body;}
    ...
}


When the schema is generated, I would expect there to be a foreign key constraint on the Heart.id back to the Body.id, but there isn't one. Do I need to do something additional to cause the constaint to be generated?

Hibernate version: 3.1.2

Hibernate Annotations: 3.1 beta 8


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 12, 2006 4:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
There is no such "system-foreign" generator

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 12, 2006 7:47 pm 
Newbie

Joined: Sat Mar 04, 2006 10:20 pm
Posts: 3
emmanuel wrote:
There is no such "system-foreign" generator


I thought I was free to pick whatever name I wanted for the @GenericGenerator. If I have defined something incorrectly here, it would be helpful if you could tell me what it is.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 8:09 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Doh, I misread, forget what I've said

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 8:09 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you try this using hbm files, is it generated?

_________________
Emmanuel


Top
 Profile  
 
 Post subject: OneToOne and PrimaryKeyJoinColumn
PostPosted: Fri Mar 31, 2006 5:29 am 
Newbie

Joined: Fri Mar 31, 2006 5:18 am
Posts: 9
Hi. I am struggling with the same problem even after a day of searching and trying various configurations, and was wondering if there was a resolution.

Specifically I have the following fields linking two entities:

User entity
@OneToOne(fetch = FetchType.LAZY,optional = true,mappedBy = "user")
private Hand hand = null;

Hand entity
@OneToOne(fetch = FetchType.LAZY,optional = false)
@PrimaryKeyJoinColumn()
private User user = null;

Everything works, unit tests run just fine, just the ddl does not include a fk constraint. I had thought that setting optional to false would indicate the need for a fk constraint to the ddl generator.

I am using:
Hibernate 3.2.0.CR1
HibernateAnnotations 3.1 beta 9
HibernateEntityManager 3.1 beta 7
HibernateTools 3.1 beta 4
MySQL 5.0.16 InnoDB
NetBeans 5.0
Java 1.5 r 6

Any info would be greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 8:06 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
After half a bottle of wine and some times, I found the bug
http://opensource.atlassian.com/project ... se/ANN-300

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 03, 2006 5:00 am 
Newbie

Joined: Fri Mar 31, 2006 5:18 am
Posts: 9
Big big big thank you emmanuel!!!

I'll be downloading the beta 10 release as soon as it is ready.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 02, 2006 5:04 am 
Regular
Regular

Joined: Thu Aug 28, 2003 6:30 am
Posts: 58
emmanuel wrote:
After half a bottle of wine and some times, I found the bug
http://opensource.atlassian.com/project ... se/ANN-300

I use Annotation 3.2.0.cr1 and Hibernate 3.2.0.cr2 and Postgres 8.1

And i have 2 objects User and Portfolio, User can have nullable portfolio but portfolio always has teh User.
Here's my mapping:
Code:
class User {
....
@OneToOne(targetEntity = com.mypack.core.dao.hibernate.persistent.Portfolio.class,
            cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    public IPortfolio getPortfolio() {
        return portfolio;
    }
....
}

class Portfolio {
....
@Id
    @GeneratedValue(generator = "system-foreign")
    @GenericGenerator(name = "system-foreign", strategy = "foreign", parameters = {
        @Parameter(name = "property", value = "user")
    })
    public long getId() {
        return id;
    }
....


@OneToOne(targetEntity = com.mypack.core.dao.hibernate.persistent.User.class,
            mappedBy = "portfolio", fetch = FetchType.LAZY, optional = false)
    public IUser getUser() {
        return user;
    }
}


And i expected FK from Portfolio id to User id. But hibernate doesn't generate it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 14, 2007 5:47 pm 
Newbie

Joined: Wed Feb 14, 2007 5:35 pm
Posts: 1
Location: Krakow, Poland
This example was very usuefull anyhow I have one additional problem to this. How to remove Portfolio from User
simple calling:

Code:
user.setPortfolio(null)
session.update(user)


does not work... - portfolio is still in database and is successfuly loaded in next access to the user.
I really do not want to call session.delete() directly on the portfolio.
so what is the solution?
I use Hibernate Annotations 3.2.1.ga

thanks in advance
Dominik

_________________
radzisz


Top
 Profile  
 
 Post subject: @PrimaryKeyJoinColumn @OneToOne foreign key constraint
PostPosted: Thu Jul 03, 2008 4:38 am 
Newbie

Joined: Thu Jul 03, 2008 2:58 am
Posts: 1
Hi,

I was having problem getting hibernate ddl to generate foreign key constraint on the referencing entity on tables with shared primary key.

I started out with an example as follows:

Code:
@Entity
public class Employee {
   
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    Integer id;
   
   
    @OneToOne
    @PrimaryKeyJoinColumn
    EmployeeInfo info;
   
}

@Entity
public class EmployeeInfo {

    @Id Integer id;
   
}


Employee is the referenced table and EmployeeInfo is referencing the Employee table where both tables have shared primary keys.

I thought that a foreign key constraint on Employee.id should be placed on the EmployeeInfo.id as indicated by the @PrimaryKeyJoinColumn on Employee.

However, the foreign key constraint on EmployeeInfo table is not generated by hibernate ddl as I expected it to be.

I found this thread and deviced the following workaround:
Code:
@Entity
public class Employee {
   
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    Integer id;
   
   
    @OneToOne
    @PrimaryKeyJoinColumn
    EmployeeInfo info;
   
}

@Entity
public class EmployeeInfo {

    @Id
    @Column(name="id", updatable=false, unique=true) // does not allow
    Integer id;
   
    //Must use optional=false to force generate the foreign key constraint
    @OneToOne(optional=false)
    @PrimaryKeyJoinColumn(name="id", referencedColumnName="id")
    Employee employee;
}

To save and associate both entities, we will need to save Employee and EmployeeInfo
in the following order:

Code:
 
  Employee employee = new Employee();
  em.persist(employee);
  EmployeeInfo info = new EmployeeInfo();
  info.id = employee.id;
  employee.info = info;
  info.employee = employee;
  em.persist(info);
  em.refresh(employee);


This will create the EmployeeInfo table with a foreign key constraint on EmployeeInfo.id to Employee.id, which is what I want.

Is this the expected behaviour with respect to the JPA specification, specifically should @PrimaryKeyJoinColumn cause the foreign key constraint on the referencing table to be generate by default?

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.