2013-06-05 81 views
0

我對hibernate和annotations的概念很陌生。目前我正在製作hibernate集合的程序。通過註釋定義休眠集合

其實下面是我的程序代碼

person.class

package com; 

import java.util.HashSet; 
import java.util.Set; 

import javax.persistence.ElementCollection; 
import javax.persistence.Embedded; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

import javax.persistence.Table; 

@Entity 
@Table(name = "PesonSubjects") 
public class Person { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private int id; 
    private String name; 
    @ElementCollection 
    private Set<Subjects> subjectList = new HashSet<Subjects>(); 

    public Set<Subjects> getSubjectList() { 
     return subjectList; 
    } 

    public void setSubjectList(Set<Subjects> subjectList) { 
     this.subjectList = subjectList; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

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

} 

主類

package com; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.AnnotationConfiguration; 

public class Personmain { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     SessionFactory sessionfactory = new AnnotationConfiguration() 
       .configure().buildSessionFactory(); 
     Session session = sessionfactory.openSession(); 
     session.beginTransaction(); 
     Person person = new Person(); 
     person.setName("vikram"); 
     Subjects subjects1 = new Subjects(); 

     subjects1.setAuthor("xxxxxxxx"); 
     subjects1.setISBN(10111); 
     subjects1.setName("mein kampf"); 
     subjects1.setPublicationHouse("tmh"); 

     person.getSubjectList().add(subjects1); 
     Subjects subjects2 = new Subjects(); 
     subjects2.setAuthor("bbbbb"); 
     subjects2.setISBN(10112); 
     subjects2.setName("harry porter"); 
     subjects2.setPublicationHouse("William"); 

     person.getSubjectList().add(subjects2); 
     session.save(person); 
     session.getTransaction().commit(); 
     session.close(); 

    } 
} 

學科類

package com; 

import javax.persistence.Embeddable; 

@Embeddable 
public class Subjects { 
    private int ISBN; 
    private String name; 
    private String Author; 
    private String publicationHouse; 

    public int getISBN() { 
     return ISBN; 
    } 

    public void setISBN(int iSBN) { 
     ISBN = iSBN; 
    } 

    public String getName() { 
     return name; 
    } 

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

    public String getAuthor() { 
     return Author; 
    } 

    public void setAuthor(String author) { 
     Author = author; 
    } 

    public String getPublicationHouse() { 
     return publicationHouse; 
    } 

    public void setPublicationHouse(String publicationHouse) { 
     this.publicationHouse = publicationHouse; 
    } 

} 

我的配置文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost:3306/AnnotationCollections</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">cccccccc</property> 
     <property name="hbm2ddl.auto">create</property> 


     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 


     <mapping class="com.Person" /> 

    </session-factory> 
</hibernate-configuration> 

現在通過節目我想的對象集合,進入到一個表(我不是註釋subjectList,因爲我希望它是收集和不以表格形式) 即應存放在收集fomat值

但是我收到以下錯誤

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version). 
log4j:WARN Please initialize the log4j system properly. 
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.Set, for columns: [org.hibernate.mapping.Column(subjectList)] 
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:266) 
    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:410) 
    at org.hibernate.mapping.RootClass.validate(RootClass.java:192) 
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1099) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1284) 
    at com.Personmain.main(Personmain.java:14) 

有別的定義?

感謝

+0

你使用的是什麼版本的hibernate? – acdcjunior

+0

嗨,我正在使用4.3.0 –

回答

1

爲了安全起見,保持映射兩類:

<mapping class="com.Person" /> 
<mapping class="com.Subjects" /> 

除此之外,你的代碼似乎推斷錯誤的訪問類型。

嘗試增加:

@javax.persistence.Access(javax.persistence.AccessType.FIELD) 

Person。像:

@Entity 
@Table(name = "PesonSubjects") 
@javax.persistence.Access(javax.persistence.AccessType.FIELD) 
public class Person { 

如果不工作,嘗試添加到兩個PersonSubjects