I have a simple ternary association I would like to persist and query
involving 3 persitable objects - Image, Author and Contribution. I
also have a type to represent an association, my AuthorContribution
type.
I don't really care if I use a Set, Map, composite-element or anything
else to represent this relationship.
I have a table that represents the association between the three
object (IMAGE_AUTHOR_CONTRIBUTION).
The problem is the association table (IMAGE_AUTHOR_CONTRIBUTION) is never updated althought each of the classes (IMAGE, AUTHOR, CONTRIBUTION) write to the database successfully!
Also, I was also expecting that during the creation of the
IMAGE_AUTHOR_CONTRIBUTION table that a primary key of (IMAGE_ID,
AUTHOR_ID, CONTRIBUTION_ID) would have been created and instead there
was no primary key.
Any help would be GREATLY appreciated.
I am using Hibernate 2.1, hsqldb v1.7.1
Here is the code I use to persist my data:
Code:
Image image = new Image( "Mona Lisa" );
Author author = new Author( "Leonardo De Vinci" );
Contribution contribution = new Contribution( "Painter" );
AuthorContribution ac = new AuthorContribution( author, contribution );
Set acs = new HashSet();
acs.add( ac );
image.setAuthorContributions( acs );
Session session = sessions.openSession();
session.save( contribution );
session.save( author );
session.save( image );
session.close();
Here are my mapping files:
--- (
Image.hbm.xml)
Code:
<class name="Image" table="IMAGE">
<id name="id" column="IMAGE_ID" type="java.lang.Integer" unsaved-value="-99">
<generator class="native"/>
</id>
<set name="authorContributions" table="IMAGE_AUTHOR_CONTRIBUTION" inverse="false" cascade="all">
<key column="IMAGE_ID" />
<composite-element class="AuthorContribution">
<many-to-one name="author" class="Author" column="AUTHOR_ID"/>
<many-to-one name="contribution" class="Contribution" column="CONTRIBUTION_ID"/>
</composite-element>
</set>
<property name="name" type="java.lang.String" column="NAME" not-null="true"/>
</class>
--- (
Author.hbm.xml)
Code:
<class name="Author" table="AUTHOR">
<id name="id" column="AUTHOR_ID" type="java.lang.Integer" unsaved-value="-99">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String" column="NAME" not-null="true"/>
</class>
--- (
Contribution.hbm.xml)
Code:
<class name="Contribution" table="CONTRIBUTION">
<id name="id" column="CONTRIBUTION_ID" type="java.lang.Integer" unsaved-value="-99" >
<generator class="native"/>
</id>
<property name="name" type="java.lang.String" column="NAME" not-null="true" />
</class>
Here are my java files:
--- (
Image.java)
Code:
public class Image
{
protected Integer id = new Integer( 0 );
protected String name;
protected Set authorContributions;
private Image() { }
public Image( String name ) { this.name = name; }
public Set getAuthorContributions() { return authorContributions; }
public void setAuthorContributions( Set authorContributions ) { this.authorContributions = authorContributions; }
public Integer getId() { return id; }
public void setId( Integer id ) { this.id = id; }
public String getName() { return name; }
public void setName( String name ) { this.name = name; }
public boolean equals( Object o )
{
if( this == o ) return true;
if( !( o instanceof Image ) ) return false;
final Image image = ( Image ) o;
if( !authorContributions.equals( image.authorContributions ) ) return false;
if( !name.equals( image.name ) ) return false;
return true;
}
public int hashCode()
{
int result;
result = name.hashCode();
result = 29 * result + authorContributions.hashCode();
return result;
}
}
--- (
Author.java)
Code:
import java.util.Set;
public class Author
{
protected Integer id = new Integer( 0 );
protected String name;
private Author(){}
public Author( String name ) { this.name = name; }
public Integer getId() { return id; }
public void setId( Integer id ) { this.id = id; }
public String getName() { return name; }
public void setName( String name ) { this.name = name; }
public boolean equals( Object o )
{
if( this == o ) return true;
if( !( o instanceof Author ) ) return false;
final Author author = ( Author ) o;
if( !name.equals( author.name ) ) return false;
return true;
}
public int hashCode() { return name.hashCode(); }
}
--- (
Contribution.java)
Code:
import java.util.Set;
public class Contribution
{
protected Integer id = new Integer( 0 );
protected String name;
private Contribution(){}
public Contribution( String name ) { this.name = name; }
public Integer getId() { return id; }
public void setId( Integer id ) { this.id = id; }
public String getName() { return name; }
public void setName( String name ) { this.name = name; }
public boolean equals( Object o )
{
if( this == o ) return true;
if( !( o instanceof Contribution ) ) return false;
final Contribution author = ( Contribution ) o;
if( !name.equals( author.name ) ) return false;
return true;
}
public int hashCode() { return name.hashCode(); }
}
--- (
AuthorContribution.java)
Code:
public class AuthorContribution
{
protected Author author;
protected Contribution contribution;
private AuthorContribution() { }
public AuthorContribution( Author author, Contribution contribution )
{
this.author = author;
this.contribution = contribution;
}
public Author getAuthor() { return author; }
public void setAuthor( Author author ) { this.author = author; }
public Contribution getContribution() { return contribution; }
public void setContribution( Contribution contribution ) { this.contribution = contribution; }
public boolean equals( Object o )
{
if( this == o ) return true;
if( !( o instanceof AuthorContribution ) ) return false;
final AuthorContribution authorContribution = ( AuthorContribution ) o;
if( !author.equals( authorContribution.author ) ) return false;
if( !contribution.equals( authorContribution.contribution ) ) return false;
return true;
}
public int hashCode()
{
int result;
result = author.hashCode();
result = 29 * result + contribution.hashCode();
return result;
}
}