-->
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.  [ 3 posts ] 
Author Message
 Post subject: (Beginner) foreign key mapping issue
PostPosted: Wed Sep 02, 2009 5:57 am 
Newbie

Joined: Fri Aug 14, 2009 6:20 am
Posts: 5
Hi folks,

I did not know a good title for my question. I hope my problem related to this group too. I have a sample project and two objects called User and UserDetail. I want a normal select query with a given user id then see all user information and details in a row.

When I debug my project, I can see user information is ready but UserDetails are null. What did I do wrongly? Here is my sample code...

my user object
Code:
package net.tolgaozdemir.client.dto;

import java.io.Serializable;


public class User implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = -3336917240964722630L;
   private int id;
   private String username;
   private String password;
   private String email;
   private UserDetail userdetail;
   
   
   
   public UserDetail getUserdetail() {
      return userdetail;
   }
   public void setUserdetail(UserDetail userdetail) {
      this.userdetail = userdetail;
   }
   
   public String getEmail() {
      return email;
   }
   public void setEmail(String email) {
      this.email = email;
   }
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getUsername() {
      return username;
   }
   public void setUsername(String username) {
      this.username = username;
   }
   public String getPassword() {
      return password;
   }
   public void setPassword(String password) {
      this.password = password;
   }
      
}


my user detail object
Code:
package net.tolgaozdemir.client.dto;

import java.io.Serializable;

public class UserDetail  implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = -8035616118364001074L;
   private int id;
   private String location;
   private String birthday;
   
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   
   
   public String getLocation() {
      return location;
   }
   public void setLocation(String location) {
      this.location = location;
   }
   
   public String getBirthday() {
      return birthday;
   }
   public void setBirthday(String birthday) {
      this.birthday = birthday;
   }
   
   public UserDetail(){}
   
}


and hibernate map files
Code:
<hibernate-mapping package="net.tolgaozdemir.client.dto" >
   
  <class name="User" table="tbluser" >
       
        <id name="id" column="id" type="integer"> 
              <generator class="native"/>
        </id>
        <property name="username" column="username" type="string" />
        <property name="password" column="password"  type="string" />
        <property name="email" column="email"  type="string" />
        <many-to-one name="userdetail" column="userdetail_id" class="UserDetail" />
     
  </class>
 
</hibernate-mapping>

Code:
<hibernate-mapping package="net.tolgaozdemir.client.dto" >
   
  <class name="UserDetail" table="tbluserdetails" lazy="true"  >
       
        <id name="id" column="id" type="integer"> 
              <generator class="native"/>
        </id>
        <property name="location" column="location" type="string" />
        <property name="birthday" column="birthday"  type="string" />
       
  </class>
 
</hibernate-mapping>



Regards,

Tolga


Top
 Profile  
 
 Post subject: Re: (Beginner) foreign key mapping issue
PostPosted: Wed Sep 02, 2009 6:26 am 
Beginner
Beginner

Joined: Sat Aug 01, 2009 5:28 am
Posts: 42
u r mappings are not correct.better if u take a look at association and collections

_________________
Warm Regards,
Tarun Walia


Top
 Profile  
 
 Post subject: Re: (Beginner) foreign key mapping issue
PostPosted: Wed Sep 02, 2009 6:37 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Check out this little tutorial on one-to-one mappings with Hibernate:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=17mappingonetooneassociations

I use an Exam and ExamDetail in the example, but it's similar:

Image


Code:
@Entity
@Table(name = "exam", schema = "examscam")
public class Exam {
/* @Basic annotation has been
added to all basic fields*/

  private int id;
  private String shortName;

  private ExamDetail detail;

  @Id
  @GeneratedValue
  @Column(name = "id")
  public int getId() { return id; }
  private void setId(int id) {this.id = id;}

@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
  @JoinColumn(name="detail_id")
  public ExamDetail getDetail() {return detail; }
  public void setDetail(ExamDetail detail) {
    this.detail = detail;
  }   

  @Basic
  public String getShortName() { return shortName;}
  public void setShortName(String shortName) {
    this.shortName = shortName;
  }
}



Image
Code:
@Entity
@Table(name = "exam_detail", schema = "examscam")
public class ExamDetail {
/* @Basic annotation has been
added to all basic fields*/
  private int id;
  private String fullName;
  private int numberOfQuestions;
  private int passingPercentage; 
  private Exam exam;
 
  @Id
  @GeneratedValue
  @Column(name = "id")
  public int getId() {return id;}
  private void setId(int id) {this.id = id;}

  @OneToOne(cascade=CascadeType.ALL, mappedBy="detail")
  public Exam getExam(){return exam;}
  public void setExam(Exam exam){this.exam=exam;}

  @Basic
  public String getFullName() {return fullName;}
  public void setFullName(String fullName) {
    this.fullName = fullName;
  }

  @Basic
  public int getNumberOfQuestions() {
   return numberOfQuestions;
  }
public void setNumberOfQuestions(int numberOfQuestions){
    this.numberOfQuestions = numberOfQuestions;
  }

  @Basic
  public int getPassingPercentage() {
   return passingPercentage;
  }
  public void setPassingPercentage(int passingPercentage){
    this.passingPercentage = passingPercentage;
  }
}

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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