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.