在hibernate中使用@idclass的复合键

时间:2014-11-08 14:46:07

标签: java hibernate

我尝试使用@idclass创建一个复合键,但坚持使用它 solocomposition类     包装solomonadvanced;     import java.io.Serializable;

import javax.persistence.Entity;
@Entity
public class Solocompositema implements Serializable {


    private static final long serialVersionUID = 1L;

    Solocompositema(){}
    public int getId() {
        return id;
    }

    public int getRoolno() {
        return roolno;
    }

    private int id;
    private int roolno;

下一堂课Solocomb

package solomonadvanced;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
@Entity
@Table(name="composite")
@IdClass(Solocompositema.class)
public class Solocomb implements Serializable{
    private static final long serialVersionUID = 1L;

@Id
private int id;
@Id
private int roolno;
@Column(name="name")
private String name;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public int getRoolno() {
    return roolno;
}
public void setRoolno(int roolno) {
    this.roolno = roolno;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}












}

        and the implementclass
package solomonadvanced;

import org.hibernate.Session;

public class Solocombimp {

    public static void main(String[] args) {
    Session sn=Util.getSessionFactory().openSession();
    sn.beginTransaction();
    Solocomb cmb=new Solocomb();
    cmb.setName("solomon");
    cmb.setId(5175);
    cmb.setRoolno(21);
    sn.save(sn);
    sn.getTransaction().commit();
    sn.close();
    System.out.println("hidasuccessfulda");


    }

}

错误详情

2014年11月8日下午8:13:23

org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Nov 08, 2014 8:13:23 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Nov 08, 2014 8:13:23 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 08, 2014 8:13:23 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 08, 2014 8:13:23 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Nov 08, 2014 8:13:23 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Nov 08, 2014 8:13:23 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Nov 08, 2014 8:13:23 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Nov 08, 2014 8:13:23 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Nov 08, 2014 8:13:23 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/solomon]
Nov 08, 2014 8:13:23 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root}
Nov 08, 2014 8:13:23 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Nov 08, 2014 8:13:23 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 15 (min=1)
Nov 08, 2014 8:13:23 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Enitial SessionFactory creation failedorg.hibernate.AnnotationException: No identifier specified for entity: solomonadvanced.Solocompositema
Exception in thread "main" java.lang.ExceptionInInitializerError
    at solomonadvanced.Util.<clinit>(Util.java:22)
    at solomonadvanced.Solocombimp.main(Solocombimp.java:8)
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: solomonadvanced.Solocompositema
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:775)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at solomonadvanced.Util.<clinit>(Util.java:19)
    ... 1 more

它给出了一个错误,说类没有id,但我不知道如何

1 个答案:

答案 0 :(得分:1)

您使用Solocompositema为您的ID类(@Entity)添加了注释。它不是一个实体。只有一个类包含Solocomb实体的ID的两个成员。

@Entity删除Solocompositema注释。