-->
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: Unidirectional one to one mapping on foreign key lazy loadin
PostPosted: Wed Apr 10, 2013 8:55 pm 
Newbie

Joined: Wed Jul 27, 2011 9:29 pm
Posts: 4
Hello everyone,

I was trying to lazy fetch a unidectional one to one mapped object but it failed. Basically I have following two tables:


CREAT TABLE `A` (
`NAME` varchar(128) NOT NULL default '',
`AGE` int(11) default NULL,
PRIMARY KEY (`NAME`),
)

CREATE TABLE B (
`ID` int(11) NOT NULL auto_increment,
`NAME` varchar(128) NOT NULL default '',
`AGE` int(11) default NULL,
PRIMARY KEY (`ID`),
CONSTRAINT BName UNIQUE (`NAME`),
FOREIGN KEY (`NAME`) REFERENCES A(`NAME`) ON DELETE CASCADE
)


The corresponding objects are:



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.fhp.ems.common.data.dao.HibernateInitializer;

@Entity
@Table(name = "A")
public class A {

private String name;
private Integer age;
private B b;


@Id
@Column(name = "NAME", length = 128)
public String getName() {
return this.name;
}


public void setName(String name) {
this.name = name;
}


@Column(name = "AGE")
public Integer getAge() {
return this.age;
}


public void setAge(Integer age) {
this.age = age;
}


@OneToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "name", referencedColumnName="NAME")
public B getB() {
return b;
}


public void setB(B b) {
this.b = b;
}

}



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "B")
public class B {

private Integer id;
private String name;



@Id
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return this.id;
}


public void setId(Integer id) {
this.id = id;
}


@Column(name = "NAME", length = 128)
public String getName() {
return this.name;
}


public void setName(String name) {
this.name = name;
}


}



I am expecting that it does a lazy loading. But it actually fetch B using a SELECT query even if I don't call getB() method at all:

Hibernate:
select
a0_.NAME as NAME69_0_,
a0_.AGE as AGE69_0_,
a0_.name as name69_0_
from
A a0_
where
a0_.NAME=?
Hibernate:
select
b0_.ID as ID70_0_,
b0_.NAME as NAME70_0_
from
B b0_
where
b0_.NAME=?

If I change it to FetchType.EAGER, then it does what it is expected - join on B table.


Does anyone know how can I make the lazy loading works? I don't want B is fected until it is used.


Thanks for help.


Top
 Profile  
 
 Post subject: Re: Unidirectional one to one mapping on foreign key lazy loadin
PostPosted: Thu Apr 11, 2013 12:10 am 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
Hi.,

Try many to one with unique constraint.

I think it should do

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


Top
 Profile  
 
 Post subject: Re: Unidirectional one to one mapping on foreign key lazy loadin
PostPosted: Thu Apr 11, 2013 1:16 pm 
Newbie

Joined: Wed Jul 27, 2011 9:29 pm
Posts: 4
Thanks for the quick response. I don't really want to change the one to one relationship if possible, is this the only way to solve this issue?


Akash Mittal wrote:
Hi.,

Try many to one with unique constraint.

I think it should do


Top
 Profile  
 
 Post subject: Re: Unidirectional one to one mapping on foreign key lazy loadin
PostPosted: Fri Apr 19, 2013 2:36 am 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
In that case you have to add the Table B's ID to table A

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


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.