如何在Hibernate中插入具有多个对象的arraylist?

时间:2017-04-20 10:28:16

标签: hibernate arraylist

我对Struts2& amp; Hibernate和我有以下问题:

在执行期间捕获的异常期间未知实体java.util.arraylist

虽然我创造了DAO& Pojo使用Hibernate逆向工程。所以我没有对Pojo做任何改变

1 个答案:

答案 0 :(得分:0)

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

}