如何在hibernate中通过ArrayList保存多个对象?

时间:2016-08-12 17:01:27

标签: java hibernate arraylist

我有一个学生实体。我的想法是在ArrayList中收集多个学生对象,并将该列表中的所有对象保存到数据库中。什么时候使用@ElementCollection注释?它适用于这种情况吗?

学生

package basic;

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

@Entity
public class Student {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;

    public Student() {

    }

    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;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + "]";
    }

    public Student(String name) {
        this.name = name;
    }

}

转轮

package basic;

import java.util.ArrayList;
import java.util.List;

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

public class Runner {

    public static void main(String[] args) {

        SessionFactory sessionFactory = new Configuration().configure("/basic/hibernate.cfg.xml").buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        List<Student> students = new ArrayList<>();

        students.add(new Student("Michael"));
        students.add(new Student("Dave"));
        students.add(new Student("Tom"));
        students.add(new Student("Dinesh"));
        students.add(new Student("Lakshman"));
        students.add(new Student("Cruise"));

        session.save(students);

        session.getTransaction().commit();
        session.close();
    }

}

错误

Exception in thread "main" org.hibernate.MappingException: Unknown entity: java.util.ArrayList
    at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1596)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:668)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:660)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:655)
    at basic.Runner.main(Runner.java:27)

2 个答案:

答案 0 :(得分:14)

你必须做这样的事情:

for(Student student : students) {
    session.save(student);
}

如果要保存实体,则应映射它。的ArrayList&LT;&GT;不是映射实体。学生有映射,所以你应该单独保存。

答案 1 :(得分:4)

@ElementCollection你应该用来定义对象之间的关系 - 这里你有很好的explenation https://en.wikibooks.org/wiki/Java_Persistence/ElementCollection

要保存对象列表,您需要按对象进行迭代,如下所示 - &gt; How to insert multiple rows into database using hibernate?