使用HQL将数据从一个表复制到另一个表,无法正常工作

时间:2016-11-30 08:12:44

标签: java sql hibernate hql

我有两个表newstudent和oldstudent,两个表都有id,name,email列。在oldstudent表中存在一些数据。我想使用 HQL 将这些数据复制到newstudent表。我已经在 hibernate.cfg.xml 文件中映射了这两个表。所有的hibernate配置都运行正常但我的问题在于HQL groupBox1。它显示错误

主要

INSERT INTO NewStudent(id, name, email) SELECT o.id, o.name, o.email FROM OldStudent o

hibernate.cfg.xml中

package insert.hql;

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

public class InsertTest {

    public static void main(String[] args) {

        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");
        SessionFactory sf = cfg.buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();

        String hql = "INSERT INTO NewStudent(id, name, email) SELECT o.id, o.name, o.email FROM OldStudent o";
        Query q = s.createQuery(hql);
        int i = q.executeUpdate();
        tx.commit();

        s.close();
        sf.close();
    }
}

错误

<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>

        <mapping resource="oldstudent.hbm.xml"/>
        <mapping resource="newstudent.hbm.xml"/>

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

0 个答案:

没有答案