I'm am positive that this is somewhere on these boards, and I've spent some time searching but no luck.
I have the following table in mysql:
CREATE TABLE IF NOT EXISTS `application_groups` ( `id` int(11) NOT NULL auto_increment, `application_id` int(11) NOT NULL, `group_id` int(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `group_app` (`group_id`,`application_id`), KEY `application_id` (`application_id`), KEY `group_id` (`group_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=162 ;
I have the following config files: ApplicationGroups.hbm.xml
<hibernate-mapping> <class catalog="security" name="com.dhenton9000.hibernatesecurity.ApplicationGroups" table="application_groups"> <id name="id" type="java.lang.Integer" unsaved-value="0" > <column name="id"/> <generator class="identity"/> </id> <many-to-one class="com.dhenton9000.hibernatesecurity.Groups" fetch="select" name="groups"> <column name="group_id" not-null="true"/> </many-to-one> <many-to-one class="com.dhenton9000.hibernatesecurity.Applications" fetch="select" name="applications"> <column name="application_id" not-null="true"/> </many-to-one> </class> </hibernate-mapping>
Hibernate config:
<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/security</property> <property name="hibernate.connection.username">root</property> <property name="current_session_context_class">thread</property>
<property name="hibernate.format_sql">true</property> <property name="show_sql">true</property>
<mapping resource="com/dhenton9000/hibernatesecurity/Users.hbm.xml"/> <mapping resource="com/dhenton9000/hibernatesecurity/GroupAssignments.hbm.xml"/> <mapping resource="com/dhenton9000/hibernatesecurity/Applications.hbm.xml"/> <mapping resource="com/dhenton9000/hibernatesecurity/Groups.hbm.xml"/> <mapping resource="com/dhenton9000/hibernatesecurity/ApplicationGroups.hbm.xml"/> </session-factory> </hibernate-configuration>
Here is the bean:
package com.dhenton9000.hibernatesecurity; // Generated Jun 17, 2010 4:41:51 PM by Hibernate Tools 3.2.1.GA
/** * ApplicationGroups generated by hbm2java */ public class ApplicationGroups implements java.io.Serializable {
private int id; private Groups groups; private Applications applications;
public ApplicationGroups() { }
public ApplicationGroups(int id, Groups groups, Applications applications) { this.id = id; this.groups = groups; this.applications = applications; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public Groups getGroups() { return this.groups; } public void setGroups(Groups groups) { this.groups = groups; } public Applications getApplications() { return this.applications; } public void setApplications(Applications applications) { this.applications = applications; }
}
Here is my sample test:
public void testSave() throws DataAccessLayerException { Session session = null; Transaction tx = null; SessionFactory factory = null; try {
SecurityDAO instance = new SecurityDAO(); ApplicationGroups aG = new ApplicationGroups(); Applications aa = (Applications) instance.find(Applications.class, new Integer(2));
Groups gg = (Groups) instance.find(Groups.class, new Integer(7));
aG.setApplications(aa); aG.setGroups(gg);
factory = new Configuration().configure().buildSessionFactory();
session = factory.openSession(); tx = session.beginTransaction();
Integer ii = (Integer) session.save(aG); log.debug("ii "+ii.intValue());
session.flush(); tx.commit();
} catch (Exception e) { tx.rollback(); throw new DataAccessLayerException(Utils.createErrorMessage(e)); } finally { session.close(); } } ------------------
My expectation is that the Integer ii above should contain the key of the inserted record. The insert takes place correctly, but the value of aG.id and ii are both 0.
I know I'm missing something obvious, as I'm new to all of this, but I can't figure out what. Tracing the sql output from hibernate shows the insert statement sent out, but no select to last_insert_id, which would get the id value and put it in my object.
Everything I've seen here and on the Internet says that's a service delivered by hibernate, but I don't understand why it isn't working in this case.
Any help greatly appreciated!!!!!
|