-->
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.  [ 4 posts ] 
Author Message
 Post subject: One-to-One sharing PK problem!
PostPosted: Thu Sep 20, 2007 4:13 pm 
Newbie

Joined: Thu Sep 20, 2007 2:42 pm
Posts: 3
Hibernate version:
Hibernate 3.2
Hibernate Annotations 3.2.0.CR2

Name and version of the database you are using:
McKoi Database v.1.0.3

The Problem
I'm trying to save two entities in the database.
They have a One-to-One relationship sharing primary keys, but their values stored in the database differ.
When i saw that i tried to use an incremental @GenericGenerator in one of the classes and a foreign @GenericGenerator in the other,
but i don't know how to make it work.

The Code
This is a short example of code, assuming that class/variables names and database table/field names are the same.

I saw an example (I'm not really sure if it was) in a taiwanese forum and tried to apply it to my code.
Here's the URL: http://www.javaworld.com.tw/jute/post/view?bid=54&id=205471

Here's the code:

NOTE: In my code both PK's have the same name.

//FIRST CLASS

@Entity
@Table(name = "Body")
public class Body implements java.io.Serializable{

private int body_PK;
private Heart heart;

public Body() {
}

@Id
@GeneratedValue(generator = "BodyPKGenerator")
@GenericGenerator(name="BodyPKGenerator",
strategy = "increment")
public int getBody_PK() {
return body_PK;
}

public void setBody_PK(int body_PK) {
this.body_PK = body_PK;
}

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

public void setHeart(Heart heart) {
this.heart = heart;
}

}

//SECOND CLASS

@Entity
@Table(name = "Heart")
public class Heart implements java.io.Serializable{

private int Heart_PK;
private boolean healthy;

public Heart(){
}

@Id @GeneratedValue(generator="foreign")
@GenericGenerator(
name = "foreign",
strategy = "foreign",
parameters = @Parameter(name = "property", value = "body")
)
public int getHeart_PK() {
return Heart_PK;
}
@OneToOne(cascade = CascadeType.ALL, mappedBy = "heart")
private Body body;

public void setHeart_PK(int Heart_PK) {
this.Heart_PK = Heart_PK;
}

public boolean isHealthy() {
return healthy;
}

public void setHealthy(boolean healthy) {
this.healthy = healthy;
}

}

The Error
org.hibernate.HibernateException: Unable to resolve property: heart


I'm pretty sure that i've missed something and any help will be appreciated.

Thanks in advance and sorry for my bad english.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 20, 2007 4:41 pm 
Beginner
Beginner

Joined: Tue Aug 14, 2007 6:29 pm
Posts: 27
Location: chicago
Make sure that

mappedBy = "heart" , the column heart is part of both tables.


Can you post your hbm.xml?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 20, 2007 5:06 pm 
Newbie

Joined: Thu Sep 20, 2007 2:42 pm
Posts: 3
public class HibernateUtil {

private static final SessionFactory sessionFactory;
static {
try {
Properties myDbConfiguration = new Properties();

myDbConfiguration.put("hibernate.dialect","org.hibernate.dialect.MckoiDialect");
myDbConfiguration.put("hibernate.connection.driver_class","com.mckoi.JDBCDriver");
myDbConfiguration.put("hibernate.connection.url","jdbc:mckoi:local://C:/PCNutricion/nutriciondb.conf");
myDbConfiguration.put("hibernate.connection.username","admin_user");
myDbConfiguration.put("hibernate.connection.password","aupass00");


sessionFactory = new AnnotationConfiguration()
.addPackage("medical.pojos")
.addAnnotatedClass(Body.class)
.addAnnotatedClass(Heart.class)
...
.buildSessionFactory();
} catch (Throwable ex) {

throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession()
throws HibernateException {
return sessionFactory.openSession();
}

}

It's mapped in HibernateUtil.java using AnnotatedClasses.

Field heart in both classes?

Do I need to create a new Heart heart inside the Heart class?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 20, 2007 5:14 pm 
Newbie

Joined: Thu Sep 20, 2007 2:42 pm
Posts: 3
And thanks for reading & answering :)


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