Hibernate version: 3.2.6
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field" default-lazy="false">
<class name="collective.md.image.BaseVersionedAsset" abstract="true" optimistic-lock="version">
<id name="id" unsaved-value="null">
<generator class="hilo" />
</id>
<version name="ver" />
<union-subclass name="collective.md.image.Asset" table="Asset" >
<property name="filename" not-null="true" index="filename"/>
<property name="filenameOnDisk" not-null="true" index="filenameOnDisk"/>
<property name="releaseDate" index="idx_releaseDate" />
<property name="expirationDate" index="idx_expirationDate" />
<property name="dateDeleted"/>
<set name="assetGroups" table="AssetGroupAssignments" lazy="true">
<key column="assetId" />
<many-to-many class="collective.md.image.group.AssetGroup" column="groupId" />
</set>
<set name="assetVersions" lazy="true" access="field" cascade="all">
<key column="parentAsset"/>
<one-to-many class="collective.md.image.AssetVersion"/>
</set>
<many-to-one name="currentVersion" class="collective.md.image.AssetVersion" access="field"/>
<property name="conversionStatus" type="collective.dataaccess.enumtypes.AssetConversionStatusType" access="field" />
<property name="updateLinkSystem" access="field" />
<set name="categories" access="field" table="AssetCategoryAssignments" lazy="true" inverse="true" >
<key column="asset_id"/>
<many-to-many class="collective.search.category.AssetCategory" column="category_id" />
</set>
<many-to-one name="assetProducer" class="collective.md.image.AssetProducer" access="field" />
<property name="starred" access="field" />
<property name="locked" access="field" />
</union-subclass>
<union-subclass name="collective.md.image.ProofAsset" table="ProofAsset">
<property name="filename" not-null="true" index="filename"/>
<property name="filenameOnDisk" not-null="true" index="filenameOnDisk"/>
<property name="releaseDate" index="idx_releaseDate" />
<property name="expirationDate" index="idx_expirationDate" />
<property name="dateDeleted"/>
<set name="assetGroups" table="AssetGroupAssignments" lazy="true">
<key column="assetId" />
<many-to-many class="collective.md.image.group.AssetGroup" column="groupId" />
</set>
<set name="assetVersions" lazy="true" access="field" cascade="all">
<key column="parentProofAsset"/>
<one-to-many class="collective.md.image.AssetVersion"/>
</set>
<many-to-one name="currentVersion" class="collective.md.image.AssetVersion" access="field"/>
<property name="conversionStatus" type="collective.dataaccess.enumtypes.AssetConversionStatusType" access="field" />
<property name="updateLinkSystem" access="field" />
</union-subclass>
<union-subclass name="collective.md.image.PhotoAsset" table="PhotoAsset">
<property name="filename" not-null="true" index="filename"/>
<property name="filenameOnDisk" not-null="true" index="filenameOnDisk"/>
<property name="releaseDate" index="idx_releaseDate" />
<property name="expirationDate" index="idx_expirationDate" />
<property name="dateDeleted"/>
<set name="assetGroups" table="AssetGroupAssignments" lazy="true" >
<key column="assetId" />
<many-to-many class="collective.md.image.group.AssetGroup" column="groupId" />
</set>
<set name="assetVersions" lazy="true" access="field" cascade="all">
<key column="parentPhotoAsset"/>
<one-to-many class="collective.md.image.AssetVersion"/>
</set>
<many-to-one name="currentVersion" class="collective.md.image.AssetVersion" access="field"/>
<property name="conversionStatus" type="collective.dataaccess.enumtypes.AssetConversionStatusType" access="field" />
<property name="updateLinkSystem" access="field" />
</union-subclass>
<filter name="excludeDeleted" condition="dateDeleted is null"/>
</class>
<filter-def name="excludeDeleted"/>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping default-access="field" default-lazy="false">
<class name="collective.md.image.AssetVersion" table="AssetVersion" optimistic-lock="version">
<id name="id" unsaved-value="null">
<column name="id" index="idx_assetid" />
<column name="id" index="idx_assetid_deleted" />
<generator class="native" />
</id>
<version name="ver" />
<many-to-one name="fileOnDisk" cascade="save-update" fetch="join" />
<many-to-one name="metadata" class="collective.asset.Metadata" column="metadataId" cascade="save-update,lock" fetch="select" />
<property name="legacyId" index="legacyAssetIdx" />
<list name="assetHistory" cascade="all" lazy="true">
<key column="assetVersion_id"/>
<list-index column="i"/>
<one-to-many class="collective.md.image.AssetHistory"/>
</list>
<property name="previewCreationAttempted">
<column name="previewCreationAttempted" index="idx_previewCreationAttempted" />
</property>
<set name="views" cascade="all" inverse="true" lazy="true">
<key column="assetVersionId"/>
<one-to-many class="collective.md.image.AssetView"/>
</set>
<many-to-one name="defaultView" class="collective.md.image.AssetView" access="field" />
<property name="dateAdded" />
<property name="dateUpdated" />
<many-to-one name="xmpMetadata" column="XmpID" class="collective.md.image.XMPMetadata" cascade="all" />
<property name="originalFilename" />
<many-to-one name="parentAsset" insert="false" update="false" class="collective.md.image.BaseVersionedAsset" />
<property name="archived" />
<property name="versionNumber" access="field" />
<list name="ratings" cascade="all" lazy="true">
<key column="assetVersion_id"/>
<list-index column="idx"/>
<one-to-many class="collective.md.image.Rating"/>
</list>
<property name="autoRelease"/>
<property name="billable"/>
<property name="autoResolve"/>
<property name="dateDeleted">
<column name="dateDeleted" index="idx_deleted" />
<column name="dateDeleted" index="idx_assetid_deleted" />
</property>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field" default-lazy="false">
<class name="collective.md.image.group.AssetGroup" table="AssetGroups" >
<id name="id" unsaved-value="null">
<generator class="native" />
</id>
<many-to-one name="flag" class="collective.md.image.group.Flag" column="flagId" cascade="save-update" fetch="select" />
<many-to-one name="metadata" class="collective.asset.Metadata" column="metadataId" cascade="save-update" fetch="select" />
<set name="assets" table="AssetGroupAssignments" inverse="true" lazy="true">
<key>
<column name="groupId" index="idx_assetgroupid_assetid" />
<column name="groupId" index="idx_assetgroupid" />
</key>
<many-to-many class="collective.md.image.BaseVersionedAsset">
<column name="assetId" index="idx_assetid" />
<column name="assetId" index="idx_assetgroupid_assetid" />
</many-to-many>
</set>
<set name="preEntryAssets" table="PreEntryAssetCategoryAssignments" inverse="true" lazy="true">
<key>
<column name="groupId" index="idx_assetgroupid_assetid" />
<column name="groupId" index="idx_assetgroupid" />
</key>
<many-to-many class="collective.md.image.PreEntryAsset">
<column name="preEntryAssetId" index="idx_assetid" />
<column name="preEntryAssetId" index="idx_assetgroupid_assetid" />
</many-to-many>
</set>
<many-to-one name="permissionSet" not-null="true" cascade="save-update" fetch="select" />
<property name="displayName" not-null="true" access="field"/>
<property name="dateDeleted" />
</class>
</hibernate-mapping>
Full stack trace of any exception that occurs:[java] 08-05-19 11:02:27 ERROR (AbstractFlushingEventListener.java:301) - Could not synchronize database state with session
[java] org.hibernate.exception.ConstraintViolationException: could not insert collection rows: [collective.md.image.ProofAsset.assetGroups#1]
[java] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
[java] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
[java] at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1394)
[java] at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:66)
[java] at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
[java] org.hibernate.exception.ConstraintViolationException: could not insert collection rows: [collective.md.image.ProofAsset.assetGroups#1]
[java] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
[java] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
[java] at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1394)
[java] at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:66)
[java] at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:170)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:170)
[java] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
[java] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
[java] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
[java] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
[java] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
[java] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
[java] at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
[java] at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
[java] at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
[java] at collective.dataaccess.util.SessionUtil.commit(SessionUtil.java:338)
[java] at collective.dataaccess.util.SessionUtil.commit(SessionUtil.java:314)
[java] at collective.dataaccess.util.SessionUtil.commitOrRollbackContinue(SessionUtil.java:417)
[java] at collective.dataaccess.util.helper.DaoTemplate.saveImmediately(DaoTemplate.java:97)
[java] at collective.dataaccess.util.helper.AccessHelpers.saveImmediately(AccessHelpers.java:51)
[java] at collective.PersistedObject.saveImmediately(PersistedObject.java:69)
[java] at collective.instrumentation.listeners.director.AssetListener.process(AssetListener.java:112)
[java] at collective.instrumentation.Instrument.parseElement(Instrument.java:205)
[java] at collective.instrumentation.Instrument.parseElement(Instrument.java:221)
[java] at collective.instrumentation.Instrument.beginParsing(Instrument.java:178)
[java] at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
[java] at collective.dataaccess.util.SessionUtil.commit(SessionUtil.java:338)
[java] at collective.dataaccess.util.SessionUtil.commit(SessionUtil.java:314)
[java] at collective.dataaccess.util.SessionUtil.commitOrRollbackContinue(SessionUtil.java:417)
[java] at collective.instrumentation.Instrument.parseFile(Instrument.java:154)
[java] at collective.dataaccess.util.helper.DaoTemplate.saveImmediately(DaoTemplate.java:97)
[java] at collective.dataaccess.util.helper.AccessHelpers.saveImmediately(AccessHelpers.java:51)
[java] at collective.instrumentation.Instrument.run(Instrument.java:106)
[java] at collective.instrumentation.Instrument.main(Instrument.java:307)
[java] Caused by: com.jnetdirect.jsql.JSQLException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK6C37EE37277B4B73". The conflict occurred in database "collective-branch", table "dbo.PhotoAsset", column 'id'.
[java] at com.jnetdirect.jsql.JSQLException.makeFromDatabaseError(JSQLException.java:85)
[java] at com.jnetdirect.jsql.IOBuffer.processPackets(IOBuffer.java:548)
[java] at com.jnetdirect.jsql.JSQLStatement.getNextResult(JSQLStatement.java:2646)
[java] at com.jnetdirect.jsql.JSQLStatement.sendExecute(JSQLStatement.java:2441)
[java] at com.jnetdirect.jsql.JSQLStatement.doExecuteUpdate(JSQLStatement.java:1780)
[java] at com.jnetdirect.jsql.JSQLPreparedStatement.executeUpdate(JSQLPreparedStatement.java:586)
[java] at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
[java] at collective.PersistedObject.saveImmediately(PersistedObject.java:69)
[java] at collective.instrumentation.listeners.director.AssetListener.process(AssetListener.java:112)
[java] at collective.instrumentation.Instrument.parseElement(Instrument.java:205)
[java] at collective.instrumentation.Instrument.parseElement(Instrument.java:221)
[java] at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
[java] at collective.instrumentation.Instrument.beginParsing(Instrument.java:178)
[java] at collective.instrumentation.Instrument.parseFile(Instrument.java:154)
[java] at collective.instrumentation.Instrument.run(Instrument.java:106)
[java] at collective.instrumentation.Instrument.main(Instrument.java:307)
[java] at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1367)
[java] ... 22 more
[java] Caused by: com.jnetdirect.jsql.JSQLException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK6C37EE37277B4B73". The conflict occurred in database "collective-branch", table "dbo.PhotoAsset", column 'id'.
[java] at com.jnetdirect.jsql.JSQLException.makeFromDatabaseError(JSQLException.java:85)
[java] at com.jnetdirect.jsql.IOBuffer.processPackets(IOBuffer.java:548)
[java] at com.jnetdirect.jsql.JSQLStatement.getNextResult(JSQLStatement.java:2646)
[java] at com.jnetdirect.jsql.JSQLStatement.sendExecute(JSQLStatement.java:2441)
[java] at com.jnetdirect.jsql.JSQLStatement.doExecuteUpdate(JSQLStatement.java:1780)
[java] at com.jnetdirect.jsql.JSQLPreparedStatement.executeUpdate(JSQLPreparedStatement.java:586)
[java] at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
[java] at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
[java] at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1367)
[java] ... 22 more
[java] 05/19/2008 11:02:27 ERROR Exception caught parsing instrument file.
[java] java.lang.RuntimeException: trans commit threw exception and trans was null
[java] at collective.dataaccess.util.SessionUtil.commit(SessionUtil.java:357)
[java] at collective.dataaccess.util.SessionUtil.commit(SessionUtil.java:314)
[java] at collective.dataaccess.util.SessionUtil.commitOrRollbackContinue(SessionUtil.java:417)
[java] at collective.dataaccess.util.helper.DaoTemplate.saveImmediately(DaoTemplate.java:97)
[java] at collective.dataaccess.util.helper.AccessHelpers.saveImmediately(AccessHelpers.java:51)
[java] at collective.PersistedObject.saveImmediately(PersistedObject.java:69)
[java] at collective.instrumentation.listeners.director.AssetListener.process(AssetListener.java:112)
[java] at collective.instrumentation.Instrument.parseElement(Instrument.java:205)
[java] at collective.instrumentation.Instrument.parseElement(Instrument.java:221)
[java] at collective.instrumentation.Instrument.beginParsing(Instrument.java:178)
[java] at collective.instrumentation.Instrument.parseFile(Instrument.java:154)
[java] at collective.instrumentation.Instrument.run(Instrument.java:106)
[java] at collective.instrumentation.Instrument.main(Instrument.java:307)
[java] Caused by: org.hibernate.exception.ConstraintViolationException: could not insert collection rows: [collective.md.image.ProofAsset.assetGroups#1]
[java] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
[java] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
[java] at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1394)
[java] at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:66)
[java] at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:170)
[java] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
[java] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
[java] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
[java] at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
[java] at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
[java] at collective.dataaccess.util.SessionUtil.commit(SessionUtil.java:338)
[java] ... 12 more
[java] Caused by: com.jnetdirect.jsql.JSQLException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK6C37EE37277B4B73". The conflict occurred in database "collective-branch", table "dbo.PhotoAsset", column 'id'.
[java] at com.jnetdirect.jsql.JSQLException.makeFromDatabaseError(JSQLException.java:85)
[java] at com.jnetdirect.jsql.IOBuffer.processPackets(IOBuffer.java:548)
[java] at com.jnetdirect.jsql.JSQLStatement.getNextResult(JSQLStatement.java:2646)
[java] at com.jnetdirect.jsql.JSQLStatement.sendExecute(JSQLStatement.java:2441)
[java] at com.jnetdirect.jsql.JSQLStatement.doExecuteUpdate(JSQLStatement.java:1780)
[java] at com.jnetdirect.jsql.JSQLPreparedStatement.executeUpdate(JSQLPreparedStatement.java:586)
[java] at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
[java] at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
[java] at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1367)
Name and version of the database you are using:Microsoft SQL Server Express 9.00.3042.00
Class stubs of BaseVersionedAsset, Asset, ProofAsset, PhotoAsset, AssetGroup, and AssetVersion
Code:
public abstract class BaseVersionedAsset extends BaseAsset
{
public static final int DO_NOT_UPDATE = 0;
public static final int UPDATE_REBUILD_LINKS = 1;
public static final int FORCE_REBUILD_LINKS = 2;
protected Set<AssetVersion> assetVersions = new HashSet<AssetVersion>();
protected AssetVersion currentVersion;
private AssetConversionStatus conversionStatus = null;
private AssetNotificationAgent notificationAgent;
private int updateLinkSystem;
public class Asset extends BaseVersionedAsset implements BuilderAsset, Comparable<Asset>, IMetadataContainer
{
protected Set<AssetCategory> categories = new HashSet<AssetCategory>();
private AssetProducer assetProducer;
private boolean locked = false;
private boolean starred = false;
public class PhotoAsset extends BaseVersionedAsset
{
public class ProofAsset extends BaseVersionedAsset
{
public class AssetGroup extends PermissionedObject implements Serializable, IMetadataContainer, Comparable, HibernateRefreshable
{
private Flag flag = new Flag();
private Metadata metadata = new Metadata();
@SearchableParent(cascade = Cascade.ALL)
private Set<BaseVersionedAsset> assets = new HashSet<BaseVersionedAsset>();
public class AssetVersion extends UndyingPersistedObject implements Serializable, IMetadataContainer, Previewable, Comparable<AssetVersion>
{
private static final long serialVersionUID = 666;
private static final double DEFAULT_IMAGE_RESOLUTION = 300.0;
private FileOnDisk fileOnDisk;
private Metadata metadata = new Metadata();
private Long legacyId;
private List<AssetHistory> assetHistory = new ArrayList<AssetHistory>();
private boolean previewCreationAttempted;
private Set<AssetView> views = new HashSet();
private AssetView defaultView;
private Date dateAdded = new Date();
private Date dateUpdated = new Date();
private XMPMetadata xmpMetadata;
private String originalFilename;
private BaseVersionedAsset parentAsset;
private boolean archived = false;
private int versionNumber = 1;
private List<Rating> ratings = new ArrayList<Rating>();
We're attempting a major refactor of our code, and would like the three types of assets in their own table. However, we run into a foreign key constraint with the assetgroup or assetversion mapping. We ran into something similar to this 5 years ago, and came up with a hackish work around with putting an intermediary class that contains a Set (So, AssetGroupSet would contain a collection of AssetGroups, Asset would have a AssetGroupSet).
I'd like a better way to do it than this. I did notice the <any>mapping element, but was told this wouldn't work because of the lack of the foreign key association.
Any suggestions/elegant work arounds? Have we missed something?
Thanks for your time.