0

我对休眠和注释的概念很陌生。目前我正在为休眠中的集合制作程序。

实际上以下是我的程序的代码

人.类

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,因为我希望它是集合而不是表格格式),即它应该将值存储在集合格式中

但是我收到以下错误

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)

还有什么要定义的吗??

谢谢

4

1 回答 1

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

于 2013-06-05T05:52:04.073 回答