Hi,
I have a problem with entering data into an oracle database.
The database setup is:
2 tables:
Business_unit_link
Business_unit
The 2 tables are related through Business_unit_id which is a primary key in table Business_unit and 2 foreign keys in table Business_unit_link(Primary_business_id and Secondary_business_id).
The JUnit code follows:
Code:
AllPurposeDAO dao = new AllPurposeDAO();
BusinessUnit bu = (BusinessUnit) dao.getById(new Integer(502006014),BusinessUnit.class, false );
BusinessUnit buOne = (BusinessUnit) dao.getById(new Integer(502012014),BusinessUnit.class, false );
BusinessUnitLinkId buLnkId = new BusinessUnitLinkId();
buLnkId.setBusinessUnit_Primary(bu);
buLnkId.setBusinessUnit_Secondary(buOne);
buLnkId.setLinkStartDate(new Date());
buLnkId.setLinkEndDate(new Date());
buLnkId.setLinkType("hierarchy");
buLnkId.setLinkStatus(BusinessUnit.OPEN);
BusinessUnitLink buLnk = new BusinessUnitLink();
buLnk.setId(buLnkId);
bu.getBusinessUnitLink_Primary().add(buLnk);
buOne.getBusinessUnitLink_Secondary().add(buLnk);
dao.persist(buLnk);
dao.persist(bu);
dao.persist(buOne);
HibernateUtil.commitTransaction();
As you can see, Im using a dao and a HybernateUtil class.
The error I get when executing the JUnit is:
Quote:
Process started
Attached successfully.
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
.Hibernate: select businessun0_.BUSINESS_ID as BUSINESS1_0_, businessun0_.VERSION as VERSION0_0_, businessun0_.PARENT_BUSINESS_ID as PARENT3_0_0_, businessun0_.BUSINESS_NAME as BUSINESS4_0_0_, businessun0_.PREV_BUSINESS_NAME as PREV5_0_0_, businessun0_.PREFERRED_PAYMENT_METHOD as PREFERRED6_0_0_, businessun0_.KF_NOTIFICATION_IND as KF7_0_0_, businessun0_.UNIT_TYPE as UNIT8_0_0_, businessun0_.VAT_REG_NO as VAT9_0_0_, businessun0_.COY_HSE_NO as COY10_0_0_, businessun0_.CHARITY_NO as CHARITY11_0_0_, businessun0_.OPEN_ON_CCP_IND as OPEN12_0_0_, businessun0_.ENROLLED_FOR_CCP_IND as ENROLLED13_0_0_, businessun0_.REGISTERED_ON_GG_IND as REGISTERED14_0_0_, businessun0_.BUSINESS_EMAIL as BUSINESS15_0_0_, businessun0_.RECORD_CREATED_DATE as RECORD16_0_0_, businessun0_.RECORD_CREATOR as RECORD17_0_0_, businessun0_.RECORD_CREATION_COMMENT as RECORD18_0_0_, businessun0_.STATUS as STATUS0_0_, businessun0_.BUSINESS_KEY as BUSINESS20_0_0_, businessun0_.CURRENT_USER_ID as CURRENT21_0_0_ from VCCP.CCD_BUSINESS_UNIT businessun0_ where businessun0_.BUSINESS_ID=?
Hibernate: select businessun0_.PRIMARY_BUSINESS_ID as PRIMARY2___, businessun0_.SECONDARY_BUSINESS_ID as SECONDARY1___, businessun0_.LINK_TYPE as LINK3___, businessun0_.LINK_STATUS as LINK4___, businessun0_.LINK_START_DATE as LINK5___, businessun0_.LINK_END_DATE as LINK6___, businessun0_.VERSION as VERSION__, businessun0_.SECONDARY_BUSINESS_ID as SECONDARY1_0_, businessun0_.PRIMARY_BUSINESS_ID as PRIMARY2_0_, businessun0_.LINK_TYPE as LINK3_0_, businessun0_.LINK_STATUS as LINK4_0_, businessun0_.LINK_START_DATE as LINK5_0_, businessun0_.LINK_END_DATE as LINK6_0_, businessun0_.VERSION as VERSION0_ from VCCP.CCD_BUSINESS_UNIT_LINK businessun0_ where businessun0_.PRIMARY_BUSINESS_ID=?
Hibernate: select businessun0_.BUSINESS_ID as BUSINESS1_0_, businessun0_.VERSION as VERSION0_0_, businessun0_.PARENT_BUSINESS_ID as PARENT3_0_0_, businessun0_.BUSINESS_NAME as BUSINESS4_0_0_, businessun0_.PREV_BUSINESS_NAME as PREV5_0_0_, businessun0_.PREFERRED_PAYMENT_METHOD as PREFERRED6_0_0_, businessun0_.KF_NOTIFICATION_IND as KF7_0_0_, businessun0_.UNIT_TYPE as UNIT8_0_0_, businessun0_.VAT_REG_NO as VAT9_0_0_, businessun0_.COY_HSE_NO as COY10_0_0_, businessun0_.CHARITY_NO as CHARITY11_0_0_, businessun0_.OPEN_ON_CCP_IND as OPEN12_0_0_, businessun0_.ENROLLED_FOR_CCP_IND as ENROLLED13_0_0_, businessun0_.REGISTERED_ON_GG_IND as REGISTERED14_0_0_, businessun0_.BUSINESS_EMAIL as BUSINESS15_0_0_, businessun0_.RECORD_CREATED_DATE as RECORD16_0_0_, businessun0_.RECORD_CREATOR as RECORD17_0_0_, businessun0_.RECORD_CREATION_COMMENT as RECORD18_0_0_, businessun0_.STATUS as STATUS0_0_, businessun0_.BUSINESS_KEY as BUSINESS20_0_0_, businessun0_.CURRENT_USER_ID as CURRENT21_0_0_ from VCCP.CCD_BUSINESS_UNIT businessun0_ where businessun0_.BUSINESS_ID=?
Hibernate: select businessun0_.SECONDARY_BUSINESS_ID as SECONDARY1___, businessun0_.PRIMARY_BUSINESS_ID as PRIMARY2___, businessun0_.LINK_TYPE as LINK3___, businessun0_.LINK_STATUS as LINK4___, businessun0_.LINK_START_DATE as LINK5___, businessun0_.LINK_END_DATE as LINK6___, businessun0_.VERSION as VERSION__, businessun0_.SECONDARY_BUSINESS_ID as SECONDARY1_0_, businessun0_.PRIMARY_BUSINESS_ID as PRIMARY2_0_, businessun0_.LINK_TYPE as LINK3_0_, businessun0_.LINK_STATUS as LINK4_0_, businessun0_.LINK_START_DATE as LINK5_0_, businessun0_.LINK_END_DATE as LINK6_0_, businessun0_.VERSION as VERSION0_ from VCCP.CCD_BUSINESS_UNIT_LINK businessun0_ where businessun0_.SECONDARY_BUSINESS_ID=?
E
Time: 23.624
There was 1 error:
1) testCreateHierarchy(uk.gov.vosa.ccps.model.CompanyStructureTest)java.lang.UnsupportedOperationException: cannot perform lookups on timestamps
at org.hibernate.type.TimestampType.getHashCode(TimestampType.java:99)
at org.hibernate.type.AbstractType.getHashCode(AbstractType.java:119)
at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:162)
at org.hibernate.engine.EntityKey.getHashCode(EntityKey.java:68)
at org.hibernate.engine.EntityKey.<init>(EntityKey.java:41)
at org.hibernate.engine.PersistenceContext.getDatabaseSnapshot(PersistenceContext.java:275)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:183)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:407)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:82)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:416)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:411)
at uk.gov.vosa.ccps.model.dao.AbstractDAO.persist(AbstractDAO.java:92)
at uk.gov.vosa.ccps.model.CompanyStructureTest.testCreateHierarchy(CompanyStructureTest.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at all.AllTests.main(AllTests.java:77)
Now, obviously the problem lies with the java date object. If I remove the two lines
buLnkId.setLinkStartDate(new Date()); and
buLnkId.setLinkEndDate(new Date()); the JUnit executes fine and adds the entries to the database (adding null to the date entries of course).
The mapping files for the 2 hibernate entities follow (I have removed data not apllicable to this issue):
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>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="uk.gov.vosa.ccps.model.BusinessUnit" table="CCD_BUSINESS_UNIT" schema="VCCP">
<id name="BusinessId" type="java.lang.Integer">
<column name="BUSINESS_ID" scale="12" precision="0" not-null="true" unique="true" sql-type="INTEGER" />
</id>
<!-- A versioned entity. -->
<version name="Version" column="VERSION" access="org.hibernate.property.DirectPropertyAccessor"/>
<!--some properties appear her e, but do not apply
<set name="BusinessUnitLink_Secondary" inverse="true" lazy="true">
<key>
<column name="SECONDARY_BUSINESS_ID" scale="22" precision="0" not-null="false" />
</key>
<one-to-many class="uk.gov.vosa.ccps.model.BusinessUnitLink" />
</set>
<set name="BusinessUnitLink_Primary" inverse="true" lazy="true">
<key>
<column name="PRIMARY_BUSINESS_ID" scale="22" precision="0" not-null="false" />
</key>
<one-to-many class="uk.gov.vosa.ccps.model.BusinessUnitLink" />
</set>
</class>
</hibernate-mapping>
and
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>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="uk.gov.vosa.ccps.model.BusinessUnitLink" table="CCD_BUSINESS_UNIT_LINK" schema="VCCP">
<composite-id name="id" class="uk.gov.vosa.ccps.model.BusinessUnitLinkId">
<key-many-to-one name="BusinessUnit_Secondary" class="uk.gov.vosa.ccps.model.BusinessUnit">
<column name="SECONDARY_BUSINESS_ID" scale="22" precision="0" not-null="false" />
</key-many-to-one>
<key-many-to-one name="BusinessUnit_Primary" class="uk.gov.vosa.ccps.model.BusinessUnit">
<column name="PRIMARY_BUSINESS_ID" scale="22" precision="0" not-null="false" />
</key-many-to-one>
<key-property name="LinkType" type="java.lang.String">
<column name="LINK_TYPE" scale="10" precision="0" not-null="false" sql-type="VARCHAR2" />
</key-property>
<key-property name="LinkStatus" type="java.lang.String">
<column name="LINK_STATUS" scale="10" precision="0" not-null="false" sql-type="VARCHAR2" />
</key-property>
<key-property name="LinkStartDate" type="java.util.Date">
<column name="LINK_START_DATE" scale="7" precision="0" not-null="false" sql-type="DATE" />
</key-property>
<key-property name="LinkEndDate" type="java.util.Date">
<column name="LINK_END_DATE" scale="7" precision="0" not-null="false" sql-type="DATE" />
</key-property>
<key-property name="Version" type="java.math.BigDecimal">
<column name="VERSION" scale="22" precision="0" not-null="false" sql-type="NUMBER" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
additionally included is the underlying POJO class for the composite key mapping
Code:
package uk.gov.vosa.ccps.model;
import uk.gov.vosa.ccps.model.persistence.HibernateEntity;
public class BusinessUnitLinkId implements HibernateEntity {
private uk.gov.vosa.ccps.model.BusinessUnit BusinessUnit_Secondary;
private uk.gov.vosa.ccps.model.BusinessUnit BusinessUnit_Primary;
private java.lang.String LinkType;
private java.lang.String LinkStatus;
private java.util.Date LinkStartDate;
private java.util.Date LinkEndDate;
private java.math.BigDecimal Version;
/** default constructor */
public BusinessUnitLinkId() {}
public uk.gov.vosa.ccps.model.BusinessUnit getBusinessUnit_Secondary() {
return this.BusinessUnit_Secondary;
}
public void setBusinessUnit_Secondary(uk.gov.vosa.ccps.model.BusinessUnit BusinessUnit) {
this.BusinessUnit_Secondary = BusinessUnit;
}
public uk.gov.vosa.ccps.model.BusinessUnit getBusinessUnit_Primary() {
return this.BusinessUnit_Primary;
}
public void setBusinessUnit_Primary(uk.gov.vosa.ccps.model.BusinessUnit BusinessUnit_Primary) {
this.BusinessUnit_Primary = BusinessUnit_Primary;
}
public java.lang.String getLinkType() {
return this.LinkType;
}
public void setLinkType(java.lang.String LinkType) {
this.LinkType = LinkType;
}
public java.lang.String getLinkStatus() {
return this.LinkStatus;
}
public void setLinkStatus(java.lang.String LinkStatus) {
this.LinkStatus = LinkStatus;
}
public java.util.Date getLinkStartDate() {
return this.LinkStartDate;
}
public void setLinkStartDate(java.util.Date LinkStartDate) {
this.LinkStartDate = LinkStartDate;
}
public java.util.Date getLinkEndDate() {
return this.LinkEndDate;
}
public void setLinkEndDate(java.util.Date LinkEndDate) {
this.LinkEndDate = LinkEndDate;
}
public java.math.BigDecimal getVersion() {
return this.Version;
}
public void setVersion(java.math.BigDecimal Version) {
this.Version = Version;
}
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( (other == null ) ) return false;
if ( !(other instanceof BusinessUnitLinkId) ) return false;
BusinessUnitLinkId castOther = ( BusinessUnitLinkId ) other;
return (this.getBusinessUnit_Secondary()==castOther.getBusinessUnit_Secondary()) || (this.getBusinessUnit_Secondary()==null ? false : (castOther.getBusinessUnit_Secondary()==null ? false : this.getBusinessUnit_Secondary().equals(castOther.getBusinessUnit_Secondary())))
&& (this.getBusinessUnit_Primary()==castOther.getBusinessUnit_Primary()) || (this.getBusinessUnit_Primary()==null ? false : (castOther.getBusinessUnit_Primary()==null ? false : this.getBusinessUnit_Primary().equals(castOther.getBusinessUnit_Primary())))
&& (this.getLinkType()==castOther.getLinkType()) || (this.getLinkType()==null ? false : (castOther.getLinkType()==null ? false : this.getLinkType().equals(castOther.getLinkType())))
&& (this.getLinkStatus()==castOther.getLinkStatus()) || (this.getLinkStatus()==null ? false : (castOther.getLinkStatus()==null ? false : this.getLinkStatus().equals(castOther.getLinkStatus())))
&& (this.getLinkStartDate()==castOther.getLinkStartDate()) || (this.getLinkStartDate()==null ? false : (castOther.getLinkStartDate()==null ? false : this.getLinkStartDate().equals(castOther.getLinkStartDate())))
&& (this.getLinkEndDate()==castOther.getLinkEndDate()) || (this.getLinkEndDate()==null ? false : (castOther.getLinkEndDate()==null ? false : this.getLinkEndDate().equals(castOther.getLinkEndDate())))
&& (this.getVersion()==castOther.getVersion()) || (this.getVersion()==null ? false : (castOther.getVersion()==null ? false : this.getVersion().equals(castOther.getVersion())));
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getBusinessUnit_Secondary().hashCode();
result = 37 * result + this.getBusinessUnit_Primary().hashCode();
result = 37 * result + this.getLinkType().hashCode();
result = 37 * result + this.getLinkStatus().hashCode();
result = 37 * result + this.getLinkStartDate().hashCode();
result = 37 * result + this.getLinkEndDate().hashCode();
result = 37 * result + this.getVersion().hashCode();
return result;
}
}
I'm sorry if this description is a bit extensive. If at all possible please give an indication as to what I can do not to get the date lookup error.
thnx in advance
Ruan