Hi i am try to batch insert in JPA but i couldnt do,but i did everything that i read from books or forums.
This is reader Class
Code:
@Entity
public class Readers implements Serializable
{
@Id
@GeneratedValue
@Column(name="readerId",columnDefinition = "int(11)")
private int id;
@Column(name="name",columnDefinition = "varchar(255)")
private String name;
@Column(name="surname",columnDefinition = "varchar(255)")
private String surname;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getSurname()
{
return surname;
}
public void setSurname(String surname)
{
this.surname = surname;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
}
this is persistence.xml file
Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="NewsManager" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.archive.autodetection" value="class,hbm"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/Example?useUnicode=true"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="123123"/>
<property name="hibernate.connection.username" value="newuser"/>
<property name="hibernate.c3p0.min_size" value="2"/>
<property name="hibernate.c3p0.max_size" value="7"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="3000"/>
<property name="hibernate.connection.autocommit" value="false"/>
<!-- <property name="hibernate.show_sql" value="true"/> -->
<property name="hibernate.format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>
<property name="dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
</properties>
</persistence-unit>
</persistence>
and also this id my main class
Code:
EntityManagerFactory EMF = JpaUtils.getEntityManagerFactory();
EntityManager EM = EMF.createEntityManager();
EM.setFlushMode(FlushModeType.COMMIT);
EM.getTransaction().begin();
Readers reader;
for (int i = 1; i <= 100; i++)
{
try
{
reader = new Readers();
reader.setName("Example:" + i);
reader.setSurname("EExample" + i);
EM.persist(reader);
}
catch (Exception ex)
{
System.out.println(ex.getMessage() + i);
}
finally
{
if (EM.getTransaction().isActive())
{
EM.getTransaction().rollback();
}
else
{
EM.getTransaction().begin();
}
}
}
EM.getTransaction().commit();
Now when i try to go step by step on the main class code,after each persist(),i am looking at the DB and no of records is increased by one but in batch process i must see the increase after commit() statement.i couldnt understand this trouble.
did u have any suggestion ?