-->
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.  [ 1 post ] 
Author Message
 Post subject: Composite Key Using an Entity
PostPosted: Thu Sep 10, 2009 2:19 pm 
Newbie

Joined: Thu Sep 10, 2009 1:42 pm
Posts: 1
I have a table with a composite key made up of a foreign key (bar_id) and a value (value). I'll call this table 'Foo'.

The foreign key (bar_id) serves as the @Id on its own entity, which I will call 'Bar'.

I have created an @Embeddable class (FooPk) to serve as the primary key for the Foo class. In that class is a variable of type Bar and another of type BigInteger. That would seemingly resolve to the bar_id and value that make up the composite key.

The problem is that the compiler is complaining about being unable to determine the type of the Bar variable in FooPk. The error reads:
Code:
org.hibernate.MappingException: Could not determine type for: com.jabekb.data.Bar, for columns: [org.hibernate.mapping.Column(bar)]


My current assumption is that this error stems from the fact that I'm asking Hibernate to walk Bar to determine how to get the identifier to pair with value for FooPk. Can this be done?

Here is the composite key class
Code:
@Embeddable
public class FooPk implements Serializable {
   
    private static final long serialVersionUID = -1524952877179756052L;
   
    private Bar bar;
    private BigInteger value;
   
    public FooPk(Bar bar, BigInteger value) {
       this.bar = bar;
       this.value = value;
    }
   
    public void setBar(Bar bar) {
        this.bar = bar;
    }
    public Bar getBar() {
        return bar;
    }
   
    public void setValue(BigInteger value) {
        this.value = value;
    }
    public BigInteger getValue() {
        return value;
    }
   
    @Override
    public boolean equals(Object that) {
      
        if(this == that) {
            return true;
        }
      
        if(that == null) {
            return false;
        }
      
        if(!(that instanceof FooPk)) {
            return false;
        }
      
        FooPk thatFooPk = (FooPk) that;
      
        if(bar.equals(thatFooPk.getBar()) && value.equals(thatFooPk.getValue())) {
                return true;
        }
      
        return false;

    }

    @Override
    public int hashCode() {
        return (bar.hashCode() ^ value.hashCode());
    }
}


Here is the important part of Foo.
Code:
@Entity
public class Foo{
   
    @Id
    private FooPk id;
   
    private BigInteger value;

    @ManyToOne
    @JoinColumn(name="bar_id")
    private Bar bar;

}


Here is the important part of Bar.
Code:
@Entity
public class Bar {
   
    @Id
    @Column(name="bar_id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer barId;
}


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

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.