Hi Peco,
I have tried this, however it still tries to insert a NULL into the foreign key of the composite key:
org.springframework.dao.DataIntegrityViolationException: (HibernateAccessor): data integrity violated by SQL 'null'; nested exception is java.sql.SQLException: [SQL0407] Null values not allowed in column or variable CUSTID. java.sql.SQLException: [SQL0407] Null values not allowed in column or variable CUSTID. at com.ibm.as400.access.JDError.throwSQLException(JDError.java:594) at com.ibm.as400.access.JDError.throwSQLException(JDError.java:565) at com.ibm.as400.access.AS400JDBCStatement.commonExecute(AS400JDBCStatement.java:845) at com.ibm.as400.access.AS400JDBCPreparedStatement.executeUpdate(AS400JDBCPreparedStatement.java:1109) at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:464) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:438) at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2391) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260) at org.springframework.orm.hibernate.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:202) at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:151) at org.springframework.orm.hibernate.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:257) at net.targetgroup.broker.correspondent.TelephoneDAOHibernate.saveTelephone(TelephoneDAOHibernate.java:45) at net.targetgroup.broker.hibernate.TelephoneDAOTest.testSaveTelephone(TelephoneDAOTest.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:41) at java.lang.reflect.Method.invoke(Method.java:386) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:392) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:276) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:167)
Am I doing something wrong? Here are my amended mapping files and the code for the telephones composite key class:
mapping file for telephone:
<hibernate-mapping>
<class name="Telephone" table="TELEPHONES">
<composite-id name="comp_id" class="TelephonePK"> <!-- <key-property name="custid" column="CUSTID" type="java.math.BigDecimal" length="9" /> --> <key-many-to-one name="correspondent" class="Correspondent"> <column name="CUSTID" /> </key-many-to-one> <key-property name="type" column="TELTYPE" type="CustomStringTrimType" length="30" /> </composite-id>
<timestamp name="timeStamp" column="DATETIME" unsaved-value="null" />
<property name="userStamp" type="java.lang.String" column="USERSTAMP" length="10" /> <property name="number" type="CustomStringTrimType" column="TELNUM" length="20" /> <property name="std" type="CustomStringTrimType" column="TELCODE" length="10" />
<property name="extension" type="CustomStringTrimType" column="EXTENSION" length="10" />
</class> </hibernate-mapping>
Mapping of telephone in the correspondent:
<!-- bi-directional one-to-many association to Telephone --> <set name="telephones" lazy="false" inverse="true" cascade="all"> <key> <column name="CUSTID" /> </key> <one-to-many class="net.targetgroup.broker.correspondent.Telephone" /> </set>
composite key class:
public class TelephonePK implements Serializable { private static final Logger LOG = Logger.getLogger(TelephonePK.class); /** identifier field */ private Correspondent correspondent;
/** identifier field */ private String type;
/** full constructor */ public TelephonePK(Correspondent newCorrespondent, String newType) { this.correspondent = newCorrespondent; this.type = newType; }
/** default constructor */ public TelephonePK() { }
/** * @see java.lang.Object#toString() */ public String toString() {
return new ToStringBuilder(this) .append("cuorrespondent", getCorrespondent()) .append("type", getType()) .toString(); }
/** * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object other) { if (!(other instanceof TelephonePK)) return false; TelephonePK castOther = (TelephonePK) other; EqualsBuilder builder = new EqualsBuilder(); if (getCorrespondent() != null && castOther.getCorrespondent() != null) { builder.append( this.getCorrespondent().toString(), castOther.getCorrespondent().toString()); } else { if (getCorrespondent() != castOther.getCorrespondent()) { //if one is null but not the other return false; } }
builder.append(this.getType(), castOther.getType());
return builder.isEquals(); }
/** * @see java.lang.Object#hashCode() */ public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder(); if (getCorrespondent() != null) { builder.append(getCorrespondent().toString()); }
builder.append(getType()).toHashCode(); return builder.toHashCode(); }
/** * @return */ public String getType() { return type; }
/** * @param string */ public void setType(String string) { type = string; }
/** * @return */ public Correspondent getCorrespondent() { return correspondent; }
/** * @param correspondent */ public void setCorrespondent(Correspondent correspondent) { this.correspondent = correspondent; }
Cheers for your help,
Elena
|