0

我在 Hibernate 中做一个基本操作。我有一个带有注释的实体类,并且有一个我正在运行应用程序的主类。但它显示无法执行 JDBC 批量更新。这是我的代码..

实体类

import javax.persistence.Entity;
import javax.persistence.Id;




@Entity

public class UserDetails {
@Id
private int userId;
private String userName;

public int getUserId() {
    return userId;
}
public void setUserId(int userId) {
    this.userId = userId;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
}

这是hibernate.cfg

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database Connection setting -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property    name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">2</property>
<property name="hibernate.current_session_context_class">thread</property>
<property  name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>

<property name="hbm2ddl">Create</property>
<mapping class="org.jeet.dto.UserDetails"/>
</session-factory>


</hibernate-configuration>

这是主要课程

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.jeet.dto.UserDetails;

public class HibernateTest {

/**
 * @param args
 */
public static void main(String[] args) {
    UserDetails user = new UserDetails();
    user.setUserId(1);
    user.setUserName("First user");

    SessionFactory sessionFactory = new Configuration().configure()
            .buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
}

}
4

1 回答 1

1

你能发布错误信息。代码正在运行..只有部分编辑是

  <property name="hibernate.hbm2ddl.auto">update</property>

代替

  <property name="hbm2ddl">Create</property>

在 hibernate.cfg.xml 中。发布错误消息(如果有)

于 2013-12-06T14:04:39.403 回答