日期:2014-05-19  浏览次数:20804 次

Hibernate中使用Annotation方式产生联合主键
Family.java
Java code
package com.chk.model;

import javax.persistence.Id;
import javax.persistence.IdClass;

@IdClass(value = FamilyPK.class)
public class Family {
    private String father;
    private String mother;
    private String name;
    private int count;

    @Id
    public String getFather() {
        return father;
    }

    public void setFather(String father) {
        this.father = father;
    }

    @Id
    public String getMother() {
        return mother;
    }

    public void setMother(String Mother) {
        this.mother = Mother;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

}


FamilyPK.java
Java code
package com.chk.model;

import java.io.Serializable;

public class FamilyPK implements Serializable {
    private String father;
    private String mother;

    public String getFather() {
        return father;
    }

    public void setFather(String father) {
        this.father = father;
    }

    public String getMother() {
        return mother;
    }

    public void setMother(String Mother) {
        this.mother = Mother;
    }

    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        
        return this.mother.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        if(obj instanceof FamilyPK){
            FamilyPK pk= (FamilyPK)obj;
            if(this.father.equals(pk.getFather()) && this.mother.equals(pk.getMother())){
                return true;
            }
        }
        return false;
    }

}


FamilyPKTest.java
Java code
package com.chk.model.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.chk.model.Family;

public class FamilyPKTest {

    @Test
    public void test() {

        Family f = new Family();
        f.setFather("chengke");
        f.setMother("jiajia");
        f.setName("family");
        f.setCount(3);

        Configuration cfg = new Configuration();
        SessionFactory sf = cfg.configure().buildSessionFactory();
        Session session = sf.openSession();
        session.beginTransaction();
        session.save(f);
        session.beginTransaction().commit();
        session.close();
        sf.close();
    }

}



出现的错误提示:
XML code
org.hibernate.PropertyNotFoundException: Could not find a getter for pk in class com.chk.model.Family
    at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:326)
    at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:320)
    at org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:191)
    at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:67)
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:135)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:485)