java - 通过注解在hibernate中定义集合

标签 java hibernate collections annotations

我对 hibernate 和注释的概念很陌生。目前我正在 hibernate 中的集合上编写程序。

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

人.类

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)

还有什么要定义的吗?

谢谢

最佳答案

为了安全起见,请保留这两个类的映射:

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

除此之外,您的代码似乎推断出错误的访问类型。

尝试添加:

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

。喜欢:

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

如果这不起作用,请尝试添加到 PersonSubjects

关于java - 通过注解在hibernate中定义集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932047/

相关文章:

.net - ICollection<T>.IsReadOnly 的契约(Contract)

java - 从泛型参数的构造函数中获取对象类型?

java - 不幸的是,您的应用程序已停止工作

java - 当我调用类方法名称之前的括号中的参数(?或前缀)时,它会做什么?

java - hibernate 违规错误

java - hibernate onetomany 注释 fetch = FetchType.LAZY 没有工作

Java - 寻找比 PriorityQueue 更快的东西

java - 为什么我从 SimpleDateFormat 收到错误 "Unparseable date"?

java - Tomcat 8 似乎开始但不在 Eclipse Neon 上

java - 可以在 hibernate 状态 "and"中使用 "case when...then...else...end"吗?