-->
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.  [ 1 post ] 
Author Message
 Post subject: one to one mapping problem I met
PostPosted: Mon Apr 11, 2005 4:35 am 
Newbie

Joined: Thu Apr 07, 2005 5:54 am
Posts: 2
I am trying an one to one mapping example and there are something confused me. thank you for your help.
-------------------------------
Hibernate version:2.1.2
-------------------------------
the whole files include:
Author.java --POJO
Person.java --POJO
TempTest.java
--------------------------------
and two tables were defined:
--------------------------------
Code:
DROP TABLE AUTHOR CASCADE CONSTRAINTS ;

CREATE TABLE AUTHOR (
  ID     INTEGER       NOT NULL,
  ALIAS  CHAR (50),
  PRIMARY KEY ( ID ) ) ;

ALTER TABLE AUTHOR ADD
FOREIGN KEY (ID)
  REFERENCES HIBERNATE.PERSON (ID) ;

---------------------------------------------------------
Code:
DROP TABLE PERSON CASCADE CONSTRAINTS ;

CREATE TABLE PERSON (
  ID     INTEGER       NOT NULL,
  NAME   CHAR (50),
  EMAIL  CHAR (50),
  PRIMARY KEY ( ID ) ) ;

--------------------------------
Mapping documents:
--------------------------------
Code:
<hibernate-mapping>
   <class name="POJO.Person" table="PERSON">
      <id name="id" column="ID">
        <generator class="assigned" />
      </id>
      <property name="name" column="NAME" length="50">
      </property>
      <property name="email" column="EMAIL" length="50">
      </property>
   </class>
</hibernate-mapping>

----------------------------------
<hibernate-mapping>
<class name="POJO.Author" table="AUTHOR">
<id name="id" column="ID">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="alias" column="ALIAS"/>
<!-- Associations -->
<one-to-one name="person" class="POJO.Person" cascade="all" constrained="true"/>
</class>
</hibernate-mapping>
------------------------------------------------------------
Code between sessionFactory.openSession() and session.close():
Code:
    public static void main(String[] args){
       
        Person person = new Person();
       
        person.setId(1);
        person.setName("tom");
        person.setEmail("tom@aa.dd");
               
        Author author = new Author();
        author.setAlias("hello");       
        author.setPerson(person);
         
       
        try{
          SessionFactory sf = new Configuration().configure().buildSessionFactory();
          Transaction tx = null;   
          Session session = sf.openSession();
          tx = session.beginTransaction();
                session.save(author);
                tx.commit();
                session.close();
                System.out.println("over");
        }catch(HibernateException he){
           he.printStackTrace();
        }
       
}

------------------------------------------------------------
when I set person id previously in the DB table PERSON(say:id=1), the result is:
Full stack trace of any exception that occurs:
Hibernate: insert into AUTHOR (ALIAS, ID) values (?, ?)
Hibernate: update PERSON set NAME=?, EMAIL=? where ID=?
over
------------------------
and the result in the DB will be:
in the PERSON's table id=1, but other values were updated by code
and in the Author's table, a new line was inserted and the id = 1.
--------------------------
question here: is it suppose to execute two insert phrases and save both person and author's information to DB? why here is update?
-------------------------------------------------------------
when I didn't set any values previously in DB, the result comes to:
Full stack trace of any exception that occurs:
Hibernate: insert into AUTHOR (ALIAS, ID) values (?, ?)
net.sf.hibernate.JDBCException: Could not execute JDBC batch update
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:129)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2385)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2335)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at Test.TempTest.main(TempTest.java:39)
Caused by: java.sql.BatchUpdateException: ORA-02291:
at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:459)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:3907)
at net.sf.hibernate.impl.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:54)
at net.sf.hibernate.impl.BatcherImpl.executeBatch(BatcherImpl.java:122)
... 5 more

and there were nothing saved in the DB
--------------------------------------------------------------------------
Name and version of the database you are using:Oracle 9i
---------------------------------------------------------------------------
It seems I didn't make one to one mapping successfully execute, but I just couldn't find anything not right. again, thank you for your patient and your help.

linna.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.