-->
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: Problem with one-to-one mapping with join table
PostPosted: Thu Feb 14, 2008 3:45 am 
Newbie

Joined: Thu Feb 14, 2008 1:47 am
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.2

Mapping documents:
userjoin.hbm.xml

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

<hibernate-mapping>

   <class name="com.hibernateexample.jointable.Users" table="JOIN_USERS">
   
      <id name="usersId" type="long" column="USERS_ID">
         <generator class="native"/>
      </id>
      
      <property name="firstname">
         <column name="FIRSTNAME"/>
      </property>
      
      <property name="lastname">
         <column name="LASTNAME"/>
      </property>
      
      <property name="username">
         <column name="USERNAME"/>
      </property>
      
      <property name="password">
         <column name="PASSWORD"/>
      </property>
      
      <property name="email">
         <column name="EMAIL"/>
      </property>
      
      <join table="UserAddress" optional="true">
         <key column="USERS_ID" unique="true"/>   
         <many-to-one name="shippingAddress"
               class="com.hibernateexample.jointable.Address"
               column="SHIPPING_ADDRESS_ID"
               cascade="save-update,delete"
               unique="true"
               not-null="true"/>      
      </join>
      
   </class>
   
</hibernate-mapping>




addressjoin.hbm.xml

Code:

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

<hibernate-mapping>

   <class name="com.hibernateexample.jointable.Address" table="JOIN_ADDRESS">
   
      <id name="addressId" column="ADDRESS_ID" type="long">
         <generator class="native"/>
      </id>
   
      <property name="street" column="STREET"/>
      <property name="city" column="CITY"/>
      <property name="zipcode" column="ZIPCODE"/>
      
      <join table="UserAddress" optional="true" inverse="true">
         <key column="ADDRESS_ID" unique="true"/>
         <many-to-one name="users"
               class="com.hibernateexample.jointable.Users"
               column="SHIPPING_USERS_ID"
               unique="true"
               not-null="true"/>      
      </join>

   </class>

</hibernate-mapping>



[/b]

Code between sessionFactory.openSession() and session.close():

Code:
SessionFactory sessionFactory = new Configuration()
                                    .addResource("userjoin.hbm.xml")
                                    .addResource("addressjoin.hbm.xml")
                                    .setProperty("hibernate.connection.url","jdbc:mysql://localhost/hibernateonetoonemapping")
                                    .setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver")
                                    .setProperty("hibernate.connection.username","root")
                                    .setProperty("hibernate.connection.password","root")
                                    .setProperty("hibernate.connection.pool_size", "10")
                                    .setProperty("show_sql","true")
                                    .setProperty("format_sql","true")
                                    .setProperty("dialect","org.hibernate.dialect.MySQLDialect")
                                    .setProperty("hibernate.hbm2ddl.auto","update")
                                    .buildSessionFactory();
         
         session = sessionFactory.openSession();
         
         Transaction tx = session.beginTransaction();
         
         Address a = new Address();
         a.setCity("New Delhi");
         a.setStreet("Street No. 1");
         a.setZipcode("11002");
         
         Users us = new Users();
         us.setFirstname("Siddharth");
         us.setLastname("Bhargava");
         us.setUsername("Sid");
         us.setPassword("XYZ");
         us.setEmail("sid@email.com");
         us.setShippingAddress(a);
         
                  
         session.save(us);


Full stack trace of any exception that occurs:
Quote:

org.hibernate.exception.GenericJDBCException: could not insert: [com.hibernateexample.jointable.Users]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2267)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2640)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at com.hibernateexample.jointable.MappingJoinTableApplication.main(MappingJoinTableApplication.java:53)
Caused by: java.sql.SQLException: Field 'ADDRESS_ID' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2250)
... 15 more






Name and version of the database you are using:
Name : MySQL
Version : MySQL 5.0.45

The generated SQL (show_sql=true):

Quote:
Hibernate:
insert
into
JOIN_ADDRESS
(STREET, CITY, ZIPCODE)
values
(?, ?, ?)
Hibernate:
insert
into
JOIN_USERS
(FIRSTNAME, LASTNAME, USERNAME, PASSWORD, EMAIL)
values
(?, ?, ?, ?, ?)
Hibernate:
insert
into
UserAddress
(SHIPPING_ADDRESS_ID, USERS_ID)
values
(?, ?)



Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html
[/code][/quote]


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.