lucifer
lucifer

Reputation: 2337

Could not execute JDBC batch update How to solve this

I am doing a basic operation in Hibernate .I have an entity class with annotations and have a main class from where i am running the application.But its showing Could not execute JDBC batch update. this is my code..

Entity Class

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

this is 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>

This is main class

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

}

Upvotes: 0

Views: 340

Answers (1)

flyinrhyno
flyinrhyno

Reputation: 159

Can you post the error message. The code is working.. Only part edited was

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

instead of

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

in hibernate.cfg.xml. Post error message if any

Upvotes: 1

Related Questions