File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Certification
»
EJB Certification (SCBCD/OCPJBCD)
Author
Dout in @JoinColumn annotation
Satyajeet Kadam
Ranch Hand
Joined: Oct 19, 2006
Posts: 202
posted
Sep 06, 2011 21:41:55
0
I am having Person table that is having one to one relationship with PersonDetail table.
Person.java
@Entity public class Person { private int personId; //PK private String personName; private int personDetailId;//FK public int getPersonDetailId() { return personDetailId; } public void setPersonDetailId(int personDetailId) { this.personDetailId = personDetailId; } @OneToOne @JoinColumn(name="personDetailId",referencedColumnName="personDetailId") private PersonDetail personDetail; public PersonDetail getPersonDetail() { return personDetail; } public void setPersonDetail(PersonDetail personDetail) { this.personDetail = personDetail; } @Id @GeneratedValue public int getPersonId() { return personId; } public void setPersonId(int personId) { this.personId = personId; } public String getPersonName() { return personName; } public void setPersonName(String personName) { this.personName = personName; } }
Perondetails
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package OnetoOneB; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.*; ; /** * * @author satyajeet */ @Entity public class PersonDetail { private int personDetailId; private String zipCode; private String job; private double income; private Person person; // @OneToOne(mappedBy="personDetail",cascade=CascadeType.ALL,fetch=FetchType.EAGER) public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } public double getIncome() { return income; } public void setIncome(double income) { this.income = income; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } @Id @GeneratedValue // @Column(name="Person_pK") public int getPersonDetailId() { return personDetailId; } public void setPersonDetailId(int personDetailId) { this.personDetailId = personDetailId; } public String getZipCode() { return zipCode; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } }
personDetailId is primary key in the Persondetail class and it is mapped to the personDetailId in Peron class which is foregin key.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package OnetoOneB; /** * * @author satyajeet */ import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class TestPeron { public static void main(String[] args) { AnnotationConfiguration c = new AnnotationConfiguration(); c.addAnnotatedClass(Person.class); c.addAnnotatedClass(PersonDetail.class); c.configure("hibernate.cfg.xml"); new SchemaExport(c).create(true, true); SessionFactory sf = c.buildSessionFactory(); Session s = sf.getCurrentSession(); s.beginTransaction(); PersonDetail pd=new PersonDetail(); pd.setIncome(2000); pd.setJob("Accountant"); pd.setZipCode("20345"); Person p=new Person(); p.setPersonName("satyajeet"); p.setPersonDetail(pd); s.save(p); s.getTransaction().commit(); } }
I am getting the exception as below
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: OnetoOneB.PersonDetail, at table: Person, for columns: [org.hibernate.mapping.Column(personDetail)]
Q1) Please kindly let me know what has gone wrong in using @JoinColumn?
Frits Walraven
Rancher
Joined: Apr 07, 2010
Posts: 1039
I like...
posted
Sep 07, 2011 08:14:20
0
Hi,
Q1) Please kindly let me know what has gone wrong in using @JoinColumn?
The problem is not with the @JoinColumn, but with the @OneToOne annotation. Just place the @OneToOne above the getter:
@OneToOne(cascade={CascadeType.ALL}) public PersonDetail getPersonDetail() { return personDetail; }
Regards,
Frits
subject: Dout in @JoinColumn annotation
Similar Threads
Foreign key value null if nullable true in @OneToMany assoc
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property
Value for JoinColumn in ManyToOne is getting inserted as Null
jpa problem
java.sql.SQLData - Oracle object mapping problem
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter