-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: mapping class with composite key
PostPosted: Wed Dec 16, 2009 5:57 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
Hi,

I have a class with a composite primary key that I want to map in a manytomany association. Before I had only a single primary key and everything was fine. Now I get a MappingException:

Code:
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: Area, for columns: [org.hibernate.mapping.Column(belongsTo)]
   at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:269)
   at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
   at org.hibernate.mapping.Property.isValid(Property.java:185)
   at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:435)
   at org.hibernate.mapping.JoinedSubclass.validate(JoinedSubclass.java:40)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:1112)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1297)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:854)
   at de.iac_leipzig.alkis.persistence.HibernateInheritanceTest.main(HibernateInheritanceTest.java:66)


Area extends the class Versioned which includes the composite primary key VersionedPK:
Code:
@MappedSuperclass
public abstract class Versioned implements JDBCPersistable {
   
    @Embeddable
    public static class VersionedPK implements Serializable {

        private static final long serialVersionUID = 1L;

        protected String guid = GUIDGenerator.newGUID();

        protected String branch_ID;

        protected Date valid_from;

        @Override
        public String toString() {
            return guid + branch_ID + valid_from.toString();
        }


        public String getBranch_ID() {
            return branch_ID;
        }


        void setBranch_ID( String branch_ID ) {
            this.branch_ID = branch_ID;
        }


        public Date getValid_from() {
            return valid_from;
        }


        public void setValid_from( Date valid_from ) {
            this.valid_from = valid_from;
        }


        public boolean equals( Object o ) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;

            VersionedPK that = (VersionedPK) o;

            if (valid_from != null ? !valid_from.equals( that.valid_from )
                    : that.valid_from != null)
                return false;
            if (branch_ID != null ? !branch_ID.equals( that.branch_ID )
                    : that.branch_ID != null)
                return false;
            if (this.guid != null ? !this.guid.equals( that.guid )
                    : that.guid != null)
                return false;

            return true;
        }


        public int hashCode() {
            int result;
            result = (valid_from != null ? valid_from.hashCode() : 0);
            result = 31 * result
                    + (branch_ID != null ? branch_ID.hashCode() : 0);
            result = 17 * result + (guid != null ? guid.hashCode() : 0);
            return result;
        }

    }

    public static final String PROP_ID = "id";
   
    @EmbeddedId
    VersionedPK id = new VersionedPK();

    @Transient
    public String getId() {
        return id.toString();
    }
   
    public void setId( VersionedPK id ) {
        this.id = id;
    }
   
    public void setId( String id ) {
        this.id.guid = id;
    }

    private Date valid_until;

    private String author_from;

    private String author_until;
   
    private String externalId = null;


    public Date getValid_until() {
        return valid_until;
    }


    public void setValid_until( Date valid_until ) {
        this.valid_until = valid_until;
    }


    public String getAuthor_from() {
        return author_from;
    }


    public void setAuthor_from( String author_from ) {
        this.author_from = author_from;
    }


    public String getAuthor_until() {
        return author_until;
    }


    public void setAuthor_until( String author_until ) {
        this.author_until = author_until;
    }


    public void insert( JDBCPersister persister ) throws Exception {
    }


    public String getExternalId() {
        return externalId;
    }


    public void setExternalId( String externalId ) {
        this.externalId = externalId;
    }
}


The offending class "Area" is similar to this:
Code:
@Entity
public class Area

extends Versioned

implements java.io.Serializable

{

   private static final long serialVersionUID = 1L;

   public static final String PROP_BELONGS_TO = "belongsTo";

   private java.util.Set<Area> belongsTo = new java.util.HashSet<Area>();

   @ManyToMany(fetch = FetchType.LAZY)
   @JoinTable(name = "Area_belongsTo")
   public java.util.Set<Area> getBelongsTo() {
      return belongsTo;
   }


   public void setBelongsTo(
         java.util.Set<Area> belongsTo) {
      this.belongsTo = belongsTo;
   }


Thans a lot for any comments


Top
 Profile  
 
 Post subject: Re: mapping class with composite key
PostPosted: Wed Dec 16, 2009 8:39 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
I've tried specifying the join columns explicitly on the association as described in the documentation (http://docs.jboss.org/hibernate/stable/ ... e/#d0e1700), but it didn't change the error message.

Code:
   @ManyToMany(fetch = FetchType.LAZY)
   @JoinTable(
           name = "Area_belongsTo",
            joinColumns={
                @JoinColumn(name="versionedArea1Id", referencedColumnName = "GUID"),
                @JoinColumn(name="versionedArea1BranchId", referencedColumnName = "BRANCH_ID"),
                @JoinColumn(name="versionedArea1ValidFrom", referencedColumnName = "VALID_FROM")
           },
           inverseJoinColumns={
                   @JoinColumn(name="versionedArea2Id", referencedColumnName = "GUID"),
                   @JoinColumn(name="versionedArea2BranchId", referencedColumnName = "BRANCH_ID"),
                   @JoinColumn(name="versionedArea2ValidFrom", referencedColumnName = "VALID_FROM")
           }
   )


Top
 Profile  
 
 Post subject: Re: mapping class with composite key
PostPosted: Wed Dec 16, 2009 9:19 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
Ok, I'm answering my own question. Instead of annotating the getter you have to annotate the property. No idea why either worked before, but my problem disappeared. So the example would look like:

Code:
@Entity
public class Area

extends Versioned

implements java.io.Serializable

{

   private static final long serialVersionUID = 1L;

   public static final String PROP_BELONGS_TO = "belongsTo";

   @ManyToMany(fetch = FetchType.LAZY)
   @JoinTable(name = "Area_belongsTo")
   private java.util.Set<Area> belongsTo = new java.util.HashSet<Area>();

   public java.util.Set<Area> getBelongsTo() {
      return belongsTo;
   }


   public void setBelongsTo(
         java.util.Set<Area> belongsTo) {
      this.belongsTo = belongsTo;
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.