-->
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.  [ 2 posts ] 
Author Message
 Post subject: many to one mapping not working
PostPosted: Thu May 05, 2011 8:39 am 
Newbie

Joined: Thu May 05, 2011 8:12 am
Posts: 1
Location: INDIA
Hi I am new to hibernate. I am facing problem while retriving data from DB using hibernates many to one mapping below is the mapping and code.
I want to retirive the list of a students who lives at XXX Street

Thnks in advance

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.test.dto.StudentDTO" table="TAB_STUDENT">
      <!-- Shared primary key generator, dependend on "user" property -->
      <id name="studentId" column="STUDENT_ID">
         <generator class="increment" />
      </id>
      <property name="firstName" column="FIRST_NAME" />
      <property name="lastName" column="LAST_NAME" />
      
      <many-to-one name="address" class="com.test.dto.Address"
      cascade="all" not-null="true" />
      
   </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.test.dto.Address" table="TAB_ADDRESS">
      <!-- Shared primary key generator, dependend on "user" property -->
      <id name="addressId" column="ADDRESS_ID">
         <generator class="native" />
      </id>
      <property name="street" column="ADDRESS_STREET" />
      <property name="city" column="ADDRESS_CITY" />
      <property name="zipcode" column="ADDRESS_ZIP_CODE" />
      <property name="state" column="ADDRESS_STATE" />
      
   </class>
</hibernate-mapping>


Code:
Criteria criteria = session.createCriteria(Address.class)
      .add(Restrictions.eq("street", "NIBM"));
      
      List l = criteria.list();
      
      List s = new ArrayList();
      Address dto = null;
      for (Iterator iterator = l.iterator(); iterator.hasNext();) {
         dto = (Address) iterator.next();
         System.out.println("ADDRESS ID : "+dto.getAddressId());
      }
      
      s = new TestMain().getStudentFromAddress(dto.getAddressId());
      
      for (Iterator iterator = s.iterator(); iterator.hasNext();) {
         StudentDTO object = (StudentDTO) iterator.next();
         System.out.println("STUDENT FIRST NAME IS "+object.getFirstName());
         System.out.println("ADDRESS IS "+ object.getAddress().getStreet());
         
      }

public List getStudentFromAddress(Long id){
      Session session = HibernateUtils.getSessionFactory().getCurrentSession();
      session.getTransaction().begin();
      Criteria criteria = session.createCriteria(StudentDTO.class)
      .add(Restrictions.eq("addressId", id));
      
      List l = criteria.list();
      
      return l;
   }

_________________
Thanks & regards


Top
 Profile  
 
 Post subject: Re: many to one mapping not working
PostPosted: Fri May 06, 2011 5:10 am 
Newbie

Joined: Thu May 05, 2011 1:56 pm
Posts: 4
Location: China
Hi, I think you may need to do a one-to-many mapping in the class com.test.dto.Address.

You have the many-to-one mapping in the StudentDTO class with a Address class reference "address", and you can use it to find out the street a specific student lives in.

Also you need to have a one-to-many mapping with a StudentDTO reference in your Address class so that you can use it to find who lives in a specific street.

I am not good at English so you may just test the following codes in case I didnt make myself clear.

Noted that a column name"addressId" has been added to the property "address"

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.test.dto.StudentDTO" table="TAB_STUDENT">
      <!-- Shared primary key generator, dependend on "user" property -->
      <id name="studentId" column="STUDENT_ID">
         <generator class="increment" />
      </id>
      <property name="firstName" column="FIRST_NAME" />
      <property name="lastName" column="LAST_NAME" />
     
      <many-to-one name="address" column="addressId" class="com.test.dto.Address"
      cascade="all" not-null="true" />     
   </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.test.dto.Address" table="TAB_ADDRESS">
      <!-- Shared primary key generator, dependend on "user" property -->
      <id name="addressId" column="ADDRESS_ID">
         <generator class="native" />
      </id>
      <property name="street" column="ADDRESS_STREET" />
      <property name="city" column="ADDRESS_CITY" />
      <property name="zipcode" column="ADDRESS_ZIP_CODE" />
      <property name="state" column="ADDRESS_STATE" />
      <set name="students"  inverse="true">
          <key column="addressId" />
          <one-to-many class="com.test.dto.StudentDTO" />
      </set>     
   </class>
</hibernate-mapping>


Also you need to add the following into your Address class. Noted that the set name"student" is the "student" used in your mapping above.
Code:
Set<StudentDTO> students = new HashSet<StudentDTO>();


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