Why won't my bi-directional mapping save? Am I missing something?
Code:
package co.za.easypay.reportconfig;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.mysql.jdbc.Driver;
public class GetTest {
public static void linkup(String email_id,String report_id){
System.out.println("doing linkup");
File fl = new File("WEB-INF/classes/hibernate.cfg.xml");
System.out.println(fl.getAbsolutePath());
// building a session factory with the given configuration
SessionFactory sf = new Configuration().configure(fl).buildSessionFactory();
// Open a session
Session sess = sf.openSession();
// Initialize a Transaction
Transaction tx = null;
try {
tx = sess.beginTransaction();
Report report = (Report) sess.load(Report.class,Long.parseLong(email_id));
Email mail = (Email) sess.load(Email.class,Long.parseLong(report_id));
Set mails = report.getEmails();
mails.add(mail);
report.setEmails(mails);
Set reports = mail.getReports();
reports.add(report);
mail.setReports(reports);
sess.persist(report);
sess.persist(mail);
System.out.println("did save or update");
} catch (HibernateException e) {
// If exceptions occur and there is an unfinished transaction, rollback before exception is thrown
if (tx != null)
tx.rollback();
throw e;
} finally {
// Always close the session
sess.close();
}
}
public static void main(String[] args) throws Exception {
linkup("1","3");
}
}
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">
<hibernate-mapping
>
<class
name="co.za.easypay.reportconfig.Email"
table="Email_tbl"
>
<id
name="Email_ID"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="assigned">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Inject.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="Email_Address"
type="java.lang.String"
update="true"
insert="true"
/>
<property
name="Name"
type="java.lang.String"
update="true"
insert="true"
/>
<set
name="reports"
table="Report_email__tbl"
lazy="false"
fetch="join"
cascade="all"
sort="unsorted"
>
<key
column="Email_ID"
>
</key>
<many-to-many
class="co.za.easypay.reportconfig.Report"
column="Report_ID"
/>
</set>
</class>
</hibernate-mapping>
<?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">
<hibernate-mapping
>
<class
name="co.za.easypay.reportconfig.Report"
table="Report_tbl"
>
<id
name="Report_ID"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="assigned">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Inject.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="Report_name"
column="Report_name"
type="java.lang.String"
update="true"
insert="true"
/>
<set
name="emails"
table="Report_email__tbl"
lazy="false"
fetch="join"
cascade="all"
sort="unsorted"
>
<key
column="Report_ID"
>
</key>
<many-to-many
class="co.za.easypay.reportconfig.Email"
column="Email_ID"
/>
</set>
</class>
</hibernate-mapping>
Code:
doing linkup
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ReportEmailConfig\WEB-INF\classes\hibernate.cfg.xml
2005/08/17 02:13:04 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.1 beta 1
2005/08/17 02:13:04 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
2005/08/17 02:13:04 org.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
2005/08/17 02:13:04 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
2005/08/17 02:13:04 org.hibernate.cfg.Configuration configure
INFO: configuring from file: hibernate.cfg.xml
2005/08/17 02:13:04 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource: co/za/easypay/reportconfig/Email.hbm.xml
2005/08/17 02:13:04 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: co.za.easypay.reportconfig.Email -> Email_tbl
2005/08/17 02:13:04 org.hibernate.cfg.HbmBinder bindCollection
INFO: Mapping collection: co.za.easypay.reportconfig.Email.reports -> Report_email__tbl
2005/08/17 02:13:04 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource: co/za/easypay/reportconfig/Report.hbm.xml
2005/08/17 02:13:04 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: co.za.easypay.reportconfig.Report -> Report_tbl
2005/08/17 02:13:04 org.hibernate.cfg.HbmBinder bindCollection
INFO: Mapping collection: co.za.easypay.reportconfig.Report.emails -> Report_email__tbl
2005/08/17 02:13:04 org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
2005/08/17 02:13:04 org.hibernate.cfg.Configuration secondPassCompile
INFO: processing extends queue
2005/08/17 02:13:04 org.hibernate.cfg.Configuration secondPassCompile
INFO: processing collection mappings
2005/08/17 02:13:04 org.hibernate.cfg.Configuration secondPassCompile
INFO: processing association property references
2005/08/17 02:13:04 org.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
2005/08/17 02:13:04 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
2005/08/17 02:13:04 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
2005/08/17 02:13:04 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
2005/08/17 02:13:04 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://196.34.50.156:3306/rec
2005/08/17 02:13:04 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root}
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 4.1.13-standard
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-3.0.14-production ( $Date: 2004/04/24 15:49:43 $, $Revision: 1.27.2.39 $ )
2005/08/17 02:13:05 org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
2005/08/17 02:13:05 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
2005/08/17 02:13:05 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: null
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2005/08/17 02:13:05 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.EhCacheProvider
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
Hibernate: select report0_.Report_ID as Report1_2_1_, report0_.Report_name as Report2_2_1_, emails1_.Report_ID as Report2_3_, email2_.Email_ID as Email1_3_, email2_.Email_ID as Email1_0_0_, email2_.Email_Address as Email2_0_0_, email2_.Name as Name0_0_ from Report_tbl report0_ left outer join Report_email__tbl emails1_ on report0_.Report_ID=emails1_.Report_ID left outer join Email_tbl email2_ on emails1_.Email_ID=email2_.Email_ID where report0_.Report_ID=?
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
2005/08/17 02:13:05 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
2005/08/17 02:13:05 org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
2005/08/17 02:13:05 net.sf.ehcache.config.Configurator configure
WARNING: No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/C:/Program%20Files/Java/jre1.5.0_04/lib/ext/ehcache-1.0.jar!/ehcache-failsafe.xml
Hibernate: select reports0_.Email_ID as Email1_1_, reports0_.Report_ID as Report2_1_, report1_.Report_ID as Report1_2_0_, report1_.Report_name as Report2_2_0_ from Report_email__tbl reports0_ left outer join Report_tbl report1_ on reports0_.Report_ID=report1_.Report_ID where reports0_.Email_ID=?
Hibernate: select emails0_.Report_ID as Report2_1_, emails0_.Email_ID as Email1_1_, email1_.Email_ID as Email1_0_0_, email1_.Email_Address as Email2_0_0_, email1_.Name as Name0_0_ from Report_email__tbl emails0_ left outer join Email_tbl email1_ on emails0_.Email_ID=email1_.Email_ID where emails0_.Report_ID=?
2005/08/17 02:13:05 org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
2005/08/17 02:13:05 org.hibernate.impl.SessionFactoryImpl checkNamedQueries
INFO: Checking 0 named queries
Hibernate: select reports0_.Email_ID as Email1_1_, reports0_.Report_ID as Report2_1_, report1_.Report_ID as Report1_2_0_, report1_.Report_name as Report2_2_0_ from Report_email__tbl reports0_ left outer join Report_tbl report1_ on reports0_.Report_ID=report1_.Report_ID where reports0_.Email_ID=?
Hibernate: select email0_.Email_ID as Email1_0_1_, email0_.Email_Address as Email2_0_1_, email0_.Name as Name0_1_, reports1_.Email_ID as Email1_3_, report2_.Report_ID as Report2_3_, report2_.Report_ID as Report1_2_0_, report2_.Report_name as Report2_2_0_ from Email_tbl email0_ left outer join Report_email__tbl reports1_ on email0_.Email_ID=reports1_.Email_ID left outer join Report_tbl report2_ on reports1_.Report_ID=report2_.Report_ID where email0_.Email_ID=?
did save or update