Hi Erick,
Please accept my apologies for the delay in[/b] replying to the forum question dated Wed Sep 07, 2005. Now a days in Sri Lanka having FOSS - 2005 conference (see
http://fosssl.org/)and we were bussy with that.
Details;
Hibernate Version : 3.0.5
Database : Mysql 4.0.18
Connection pooler : c3po
JDK version : 1.4.2_05
Tomcat verson : 4.1.24
I have posting my test classes here;
POJO classes (Group and Customer);
Code:
public class Group implements Serializable{
private String stringId = "";
private String groupName = "";
private String description = "";
private Date lastModifiedDateTime = null;
private Set customers = new HashSet();
public String getStringId() {
return stringId;
}
public void setStringId(String stringId) {
this.stringId = stringId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getLastModifiedDateTime() {
return lastModifiedDateTime;
}
public void setLastModifiedDateTime(Date lastModifiedDateTime) {
this.lastModifiedDateTime = lastModifiedDateTime;
}
public Set getCustomers() {
return customers;
}
public void setCustomers(Set customers) {
this.customers = customers;
}
public void setCustomer(Customer customer) {
customers.add(customer);
//customer.setGroup(this);
}
public void removeCustomer(Customer customer) {
customers.remove(customer);
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Group)) return false;
final Group group = (Group) o;
if (!stringId.equals(group.stringId)) return false;
return true;
}
public int hashCode() {
int result;
result = stringId.hashCode();
return result;
}
}
Code:
public class Customer implements Serializable{
private String stringId = "";
private String msisdn = "";
private String shortName = "";
private String fullName = "";
private String email = "";
private String status = "";
private Date lastModifiedDateTime = null;
private Set groups = new HashSet();
public String getStringId() {
return stringId;
}
public void setStringId(String stringId) {
this.stringId = stringId;
}
public String getMsisdn() {
return msisdn;
}
public void setMsisdn(String msisdn) {
this.msisdn = msisdn;
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Date getLastModifiedDateTime() {
return lastModifiedDateTime;
}
public void setLastModifiedDateTime(Date lastModifiedDateTime) {
this.lastModifiedDateTime = lastModifiedDateTime;
}
public Set getGroups() {
return groups;
}
public void setGroups(Set groups) {
this.groups = groups;
}
public void setGroup(Group group) {
groups.add(group);
//group.setCustomer(this);
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Customer)) return false;
final Customer customer = (Customer) o;
if (!stringId.equals(customer.stringId)) return false;
return true;
}
public int hashCode() {
int result;
result = stringId.hashCode();
return result;
}
}
DAO classes (GroupDAO and CustomerDAO);
Code:
public class GroupDAO {
public GroupDAO() {
HibernateUtil.beginTransaction();
}
public void makePersistent(Object object) throws Exception {
try {
Session session = HibernateUtil.getSession();
session.save(object);
} catch (HibernateException e) {
throw e;
}
}
public void makeTransient(Object object) throws Exception {
try {
HibernateUtil.getSession().delete(object);
HibernateUtil.getSession().flush();
} catch (HibernateException e) {
throw e;
}
}
public void addCustomersToGroup(Group group) throws Exception{
System.out.println("Add Customers To Group ["+ group.getStringId() +"]");
Session session = HibernateUtil.getSession();
Group groupLoded = (Group)session.createCriteria(Group.class)
.add(Expression.eq("id", group.getStringId()))
.uniqueResult();
groupLoded.setCustomers(group.getCustomers());
session.flush();
}
}
Code:
public class CustomerDAO {
public CustomerDAO() {
HibernateUtil.beginTransaction();
}
public void makePersistent(Object object) throws Exception {
try {
Session session = HibernateUtil.getSession();
session.save(object);
} catch (HibernateException e) {
throw e;
}
}
public void makeTransient(Object object) throws Exception {
try {
HibernateUtil.getSession().delete(object);
HibernateUtil.getSession().flush();
} catch (HibernateException e) {
throw e;
}
}
}
Test class (TestData);
Code:
public class TestData {
private Customer getCustomer(String id, String msisdn, String shortName, String fullName, String email,
String status, Date lastModified){
Customer customer = new Customer();
customer.setStringId(id);
customer.setMsisdn(msisdn);
customer.setShortName(shortName);
customer.setFullName(fullName);
customer.setEmail(email);
customer.setStatus(status);
customer.setLastModifiedDateTime(lastModified);
return customer;
}
private Group getGroup(String id, String name, String desc, Date lastModified){
Group group = new Group();
group.setStringId(id);
group.setGroupName(name);
group.setDescription(desc);
group.setLastModifiedDateTime(lastModified);
return group;
}
public static void main(String[] args) {
TestData testData = new TestData();
GroupDAO groupDAO = new GroupDAO();
CustomerDAO customerDAO = new CustomerDAO();
Group group = testData.getGroup("g1", "g1name", "g1desc", new Date());
Customer customer1 = testData.getCustomer("c1", "111", "shn1", "fn1", "em1", "Enabled", new Date());
Customer customer2 = testData.getCustomer("c2", "222", "shn2", "fn2", "em2", "Enabled", new Date());
Customer customer3 = testData.getCustomer("c3", "333", "shn3", "fn3", "em3", "Enabled", new Date());
try {
groupDAO.makePersistent(group);
customerDAO.makePersistent(customer1);
customerDAO.makePersistent(customer2);
customerDAO.makePersistent(customer3);
Set groupSet = new HashSet();
groupSet.add(group);
customer3.setGroups(groupSet);
Set customersSet = new HashSet();
customersSet.add(customer3);
group.setCustomers(customersSet);
groupDAO.addCustomersToGroup(group);
///////////// group object update with customer4 object
Customer customer4 = testData.getCustomer("c4", "444", "shn4", "fn4", "em4", "Enabled", new Date());
Set groupSetNew = new HashSet();
groupSetNew.add(group);
customer4.setGroups(groupSetNew);
Set customersSetNew = new HashSet();
customersSetNew.add(customer4);
customersSetNew.add(customer3);
group.setCustomers(customersSetNew);
customerDAO.makePersistent(customer4);
groupDAO.addCustomersToGroup(group);
} catch (Exception e) {
e.printStackTrace();
} finally {
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
}
}
}
hbm.xml mapping fiel (merchant.hbm.xml);
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hibernate.persistance">
<class name="Group" table="Groups">
<id name="stringId" column="GroupId" length="50"/>
<property name="groupName" column="GroupName" length="100"/>
<property name="description" column="Description"/>
<property name="lastModifiedDateTime" column="LastModifiedDateTime" type="timestamp"/>
<set name="customers" table="group_customer" lazy="true" >
<key>
<column name="GroupId" not-null="true" />
</key>
<many-to-many class="Customer" >
<column name="CustomerId" not-null="true" />
</many-to-many>
</set>
</class>
<class name="Customer" table="Customers">
<id name="stringId" column="CustomerId" length="50"/>
<property name="msisdn" column="MSISDN" length="20" />
<property name="shortName" column="ShortName" length="200"/>
<property name="fullName" column="FullName" length="200"/>
<property name="email" column="Email" length="100" />
<property name="status" column="Status" length="20" />
<property name="lastModifiedDateTime" column="LastModifiedDateTime" type="timestamp" />
<set name="groups" table="group_customer" inverse="true" lazy="true" >
<key>
<column name="CustomerId"/>
</key>
<many-to-many class="Group" >
<column name="GroupId"/>
</many-to-many>
</set>
</class>
</hibernate-mapping>
Note:
1) HibernateUtill class - Uses standerd class which is find in the web
2) Mysql Dialect set as - hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
3) Uncommented out - hibernate.hbm2ddl.auto create
After running the TestData class, I can find the following log;
Code:
Initializing HibernateUtil.....
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@1f66cff [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@1db05b2 [ acquireIncrement -> 2, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, idleConnectionTestPeriod -> 3000, initialPoolSize -> 2, maxIdleTime -> 5000, maxPoolSize -> 4, maxStatements -> 100, maxStatementsPerConnection -> 0, minPoolSize -> 2, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@c9d92c [ description -> null, driverClass -> null, factoryClassLocation -> null, jdbcUrl -> jdbc:mysql://192.168.0.143:3306/TestUpdate, properties -> {user=******, password=******} ] , preferredTestQuery -> null, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ] , factoryClassLocation -> null, numHelperThreads -> 3, poolOwnerIdentityToken -> 1f66cff ]
10:30:38,889 INFO SchemaExport:113 - Running hbm2ddl schema export
10:30:38,896 INFO SchemaExport:129 - exporting generated schema to database
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@90832e [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@1947496 [ acquireIncrement -> 2, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, idleConnectionTestPeriod -> 3000, initialPoolSize -> 2, maxIdleTime -> 5000, maxPoolSize -> 4, maxStatements -> 100, maxStatementsPerConnection -> 0, minPoolSize -> 2, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@8a548b [ description -> null, driverClass -> null, factoryClassLocation -> null, jdbcUrl -> jdbc:mysql://192.168.0.143:3306/TestUpdate, properties -> {user=******, password=******} ] , preferredTestQuery -> null, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ] , factoryClassLocation -> null, numHelperThreads -> 3, poolOwnerIdentityToken -> 90832e ]
10:30:39,292 DEBUG SchemaExport:143 - alter table group_customer drop foreign key FK82C9999E5312A712
10:30:39,342 DEBUG SchemaExport:149 - Unsuccessful: alter table group_customer drop foreign key FK82C9999E5312A712
10:30:39,344 DEBUG SchemaExport:150 - General error: Can't create table './TestUpdate/#sql-d9d_418.frm' (errno: 150)
10:30:39,345 DEBUG SchemaExport:143 - alter table group_customer drop foreign key FK82C9999EA26055FE
10:30:39,349 DEBUG SchemaExport:149 - Unsuccessful: alter table group_customer drop foreign key FK82C9999EA26055FE
10:30:39,350 DEBUG SchemaExport:150 - General error: Can't create table './TestUpdate/#sql-d9d_418.frm' (errno: 150)
10:30:39,351 DEBUG SchemaExport:143 - drop table if exists Customers
10:30:39,355 DEBUG SchemaExport:143 - drop table if exists Groups
10:30:39,360 DEBUG SchemaExport:143 - drop table if exists group_customer
10:30:39,365 DEBUG SchemaExport:161 - create table Customers (
CustomerId varchar(50) not null,
MSISDN varchar(20),
ShortName varchar(200),
FullName varchar(200),
Email varchar(100),
Status varchar(20),
LastModifiedDateTime datetime,
primary key (CustomerId)
) type=InnoDB
10:30:39,370 DEBUG SchemaExport:161 - create table Groups (
GroupId varchar(50) not null,
GroupName varchar(100),
Description varchar(255),
LastModifiedDateTime datetime,
primary key (GroupId)
) type=InnoDB
10:30:39,376 DEBUG SchemaExport:161 - create table group_customer (
GroupId varchar(50) not null,
CustomerId varchar(50) not null,
primary key (GroupId, CustomerId)
) type=InnoDB
10:30:39,400 DEBUG SchemaExport:161 - alter table group_customer
add index FK82C9999E5312A712 (GroupId),
add constraint FK82C9999E5312A712
foreign key (GroupId)
references Groups (GroupId)
10:30:39,411 DEBUG SchemaExport:161 - alter table group_customer
add index FK82C9999EA26055FE (CustomerId),
add constraint FK82C9999EA26055FE
foreign key (CustomerId)
references Customers (CustomerId)
10:30:39,425 INFO SchemaExport:173 - schema export complete
Connection pooler successfully initiated............
Add Customers To Group [g1]
Hibernate: insert into Groups (GroupName, Description, LastModifiedDateTime, GroupId) values (?, ?, ?, ?)
10:30:39,917 DEBUG StringType:59 - binding 'g1name' to parameter: 1
10:30:39,919 DEBUG StringType:59 - binding 'g1desc' to parameter: 2
10:30:39,923 DEBUG TimestampType:59 - binding '2005-09-09 10:30:39' to parameter: 3
10:30:39,926 DEBUG StringType:59 - binding 'g1' to parameter: 4
Hibernate: insert into Customers (MSISDN, ShortName, FullName, Email, Status, LastModifiedDateTime, CustomerId) values (?, ?, ?, ?, ?, ?, ?)
10:30:39,933 DEBUG StringType:59 - binding '111' to parameter: 1
10:30:39,935 DEBUG StringType:59 - binding 'shn1' to parameter: 2
10:30:39,936 DEBUG StringType:59 - binding 'fn1' to parameter: 3
10:30:39,941 DEBUG StringType:59 - binding 'em1' to parameter: 4
10:30:39,942 DEBUG StringType:59 - binding 'Enabled' to parameter: 5
10:30:39,944 DEBUG TimestampType:59 - binding '2005-09-09 10:30:39' to parameter: 6
10:30:39,947 DEBUG StringType:59 - binding 'c1' to parameter: 7
Hibernate: insert into Customers (MSISDN, ShortName, FullName, Email, Status, LastModifiedDateTime, CustomerId) values (?, ?, ?, ?, ?, ?, ?)
10:30:39,950 DEBUG StringType:59 - binding '222' to parameter: 1
10:30:39,952 DEBUG StringType:59 - binding 'shn2' to parameter: 2
10:30:39,956 DEBUG StringType:59 - binding 'fn2' to parameter: 3
10:30:39,957 DEBUG StringType:59 - binding 'em2' to parameter: 4
10:30:39,959 DEBUG StringType:59 - binding 'Enabled' to parameter: 5
10:30:39,960 DEBUG TimestampType:59 - binding '2005-09-09 10:30:39' to parameter: 6
10:30:39,962 DEBUG StringType:59 - binding 'c2' to parameter: 7
Hibernate: insert into Customers (MSISDN, ShortName, FullName, Email, Status, LastModifiedDateTime, CustomerId) values (?, ?, ?, ?, ?, ?, ?)
10:30:39,966 DEBUG StringType:59 - binding '333' to parameter: 1
10:30:39,968 DEBUG StringType:59 - binding 'shn3' to parameter: 2
10:30:39,969 DEBUG StringType:59 - binding 'fn3' to parameter: 3
10:30:39,974 DEBUG StringType:59 - binding 'em3' to parameter: 4
10:30:39,976 DEBUG StringType:59 - binding 'Enabled' to parameter: 5
10:30:39,977 DEBUG TimestampType:59 - binding '2005-09-09 10:30:39' to parameter: 6
10:30:39,980 DEBUG StringType:59 - binding 'c3' to parameter: 7
Hibernate: insert into group_customer (GroupId, CustomerId) values (?, ?)
10:30:39,985 DEBUG StringType:59 - binding 'g1' to parameter: 1
10:30:39,989 DEBUG StringType:59 - binding 'c3' to parameter: 2
Hibernate: select this_.GroupId as GroupId0_, this_.GroupName as GroupName0_0_, this_.Description as Descript3_0_0_, this_.LastModifiedDateTime as LastModi4_0_0_ from Groups this_ where this_.GroupId=?
10:30:40,010 DEBUG StringType:59 - binding 'g1' to parameter: 1
10:30:40,041 DEBUG StringType:86 - returning 'g1' as column: GroupId0_
Add Customers To Group [g1]
Hibernate: select this_.GroupId as GroupId0_, this_.GroupName as GroupName0_0_, this_.Description as Descript3_0_0_, this_.LastModifiedDateTime as LastModi4_0_0_ from Groups this_ where this_.GroupId=?
10:30:40,106 DEBUG StringType:59 - binding 'g1' to parameter: 1
10:30:40,118 DEBUG StringType:86 - returning 'g1' as column: GroupId0_
Hibernate: insert into Customers (MSISDN, ShortName, FullName, Email, Status, LastModifiedDateTime, CustomerId) values (?, ?, ?, ?, ?, ?, ?)
10:30:40,148 DEBUG StringType:59 - binding '444' to parameter: 1
10:30:40,157 DEBUG StringType:59 - binding 'shn4' to parameter: 2
10:30:40,159 DEBUG StringType:59 - binding 'fn4' to parameter: 3
10:30:40,174 DEBUG StringType:59 - binding 'em4' to parameter: 4
10:30:40,175 DEBUG StringType:59 - binding 'Enabled' to parameter: 5
10:30:40,177 DEBUG TimestampType:59 - binding '2005-09-09 10:30:40' to parameter: 6
10:30:40,178 DEBUG StringType:59 - binding 'c4' to parameter: 7
[b]Hibernate: delete from group_customer where GroupId=?[/b]
10:30:40,181 DEBUG StringType:59 - binding 'g1' to parameter: 1
Hibernate: insert into group_customer (GroupId, CustomerId) values (?, ?)
10:30:40,184 DEBUG StringType:59 - binding 'g1' to parameter: 1
10:30:40,185 DEBUG StringType:59 - binding 'c3' to parameter: 2
Hibernate: insert into group_customer (GroupId, CustomerId) values (?, ?)
10:30:40,187 DEBUG StringType:59 - binding 'g1' to parameter: 1
10:30:40,204 DEBUG StringType:59 - binding 'c4' to parameter: 2
Process terminated with exit code 0
I have bold the
Hibernate: delete from group_customer where GroupId=? in the logs; This means all the previous data in the group_customer table deleletd and inserts new data. Consern anout entries relatde to customer3. I don't want to delete that record from the group_customer table and i just want to insert only the customer4 in group_customer table, because customer3 already added.
But in here, Hibernate deleted customer3 records from the group_customer table and added it again.
If group_customer table contains thousands of data then, this will results the reduction of the speed as well as performance of the system when comparing with pure JDBC updates.
I thought that you understand my question clearly.
Any suggetions!!!!!!!!
Thank you
thilinaa.