-->
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: One To Many Unidirectional(Hibernate)
PostPosted: Fri Apr 08, 2011 3:28 am 
Newbie

Joined: Fri Apr 08, 2011 3:14 am
Posts: 2
Following is a program to demonstrate working of One to Many association.
Its not working.I am also pasting the exception raised. Please Find out the fault.
I am using myeclipseIDE 8.5, with Oracle as a backend

CollegeEntity
Code:

package org.nit;

import java.io.Serializable;

public class CollegeEntity implements Serializable {
   /**
    *
    */
   private static final long serialVersionUID = 1L;
   private Long colCode;
   private String colName;
   java.util.Set<StudentEntity> students;
   
   
   public CollegeEntity() {}

   public Long getColCode() {
      return colCode;
   }

   public void setColCode(Long colCode) {
      this.colCode = colCode;
   }

   public String getColName() {
      return colName;
   }

   public void setColName(String colName) {
      this.colName = colName;
   }

   public java.util.Set<StudentEntity> getStudents() {
      return students;
   }

   public void setStudents(java.util.Set<StudentEntity> students) {
      this.students = students;
   }

}





StudentEntity
Code:


package org.nit;

import java.io.Serializable;

public class StudentEntity implements Serializable {
   /**
    *
    */
   private static final long serialVersionUID = 1L;
   private Long id;
   private String name;
   
   public StudentEntity() {}

   public Long getId() {
      return id;
   }

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

   public String getName() {
      return name;
   }

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

}





hibernate.cfg.xml
Code:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">hibernate</property>
        <property name="connection.password">hibernate</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="myeclipse.connection.profile">Hibernate</property>
       <mapping resource="org/nit/college.hbm.xml"/>
    </session-factory>

</hibernate-configuration>



college.hbm.xml
Code:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

   <!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-mapping>
   <class name="org.nit.CollegeEntity" table="College_stud">

      <id name="colCode" type="java.lang.Long" column="College Code">
         <generator class="assigned"></generator>
      </id>

      <property name="colName" type="java.lang.String">
         <column name="College Name"></column>
      </property>

      <set name="students" cascade="save-update">
         <key>
            <column name="college_stud_fk"></column>
         </key>
         <one-to-many class="org.nit.StudentEntity" />
      </set>
   </class>

      <class name="org.nit.StudentEntity" table="student_coll">
      <id name="id" type="java.lang.Long" column="id">
         <generator class="assigned"></generator>
      </id>

      <property name="name" type="java.lang.String" column="name"></property>


   </class>

</hibernate-mapping>



TestClient
Code:

package org.nit;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class TestClient {

   /**
    * @param args
    */
   public static void main(String[] args) {
      Configuration config = new Configuration();
      config.configure();
      
      new SchemaExport(config).create(true, true);
      
      SessionFactory factory = config.buildSessionFactory();
      
      Session session = factory.openSession();
      
      Transaction trans = session.beginTransaction();
      
      System.out.println("Success1");
      
      StudentEntity se1 = new StudentEntity();
      se1.setId(1L);
      se1.setName("Kunal");
      
      StudentEntity se2 = new StudentEntity();
      se2.setId(2L);
      se2.setName("Karan");
      
      System.out.println("Success2");
      
      java.util.Set<StudentEntity> students=new java.util.HashSet<StudentEntity>();
      students.add(se1);
      students.add(se2);
      
      CollegeEntity ce = new CollegeEntity();
      ce.setColCode(1L);
      ce.setColName("St.Xaviers");
      ce.setStudents(students);
      
      System.out.println("Success3");
      
      
      session.saveOrUpdate(ce);
      /*session.save(se1);
      session.save(se2);*/
      
      trans.commit();
      session.close();
      factory.close();
   }
}




Output in the console


log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
drop table College_stud cascade constraints
drop table student_coll cascade constraints
create table College_stud (College Code number(19,0) not null, College Name varchar2(255 char), primary key (College Code))
create table student_coll (id number(19,0) not null, name varchar2(255 char), college_stud_fk number(19,0), primary key (id))
alter table student_coll add constraint FKFEDA1270A10B24A7 foreign key (college_stud_fk) references College_stud
Success1
Success2
Success3
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not retrieve snapshot: [org.nit.CollegeEntity#1]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.getDatabaseSnapshot(AbstractEntityPersister.java:1065)
at org.hibernate.engine.StatefulPersistenceContext.getDatabaseSnapshot(StatefulPersistenceContext.java:269)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:212)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:535)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:103)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:523)
at org.nit.TestClient.main(TestClient.java:50)
Caused by: java.sql.SQLException: ORA-00923: FROM keyword not found where expected

at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:213)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:796)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1031)
at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:836)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1124)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3329)
at org.hibernate.persister.entity.AbstractEntityPersister.getDatabaseSnapshot(AbstractEntityPersister.java:1038)
... 9 more


Top
 Profile  
 
 Post subject: Re: One To Many Unidirectional(Hibernate)
PostPosted: Sat Apr 09, 2011 11:13 am 
Newbie

Joined: Fri Apr 08, 2011 3:14 am
Posts: 2
No reply yet


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.