Hi All,
We are using Hibernate 3.1 beta 1. We are using the custom usertype for persisting audit fields. We are using the AuditInfoType, a custom usertype based solution in WIKI. Here's how my POJO looks:
Code:
@Table(name="SAP_MODULE")
Entity(/*access=AccessType.PROPERTY*/)
@javax.persistence.SequenceGenerator (name="SEQ_STORE", sequenceName="SAP_MODULE_SEQ")
public class SapModule extends BaseAdminEntity {
private Set<ProductSapModule> productSapModule = new HashSet<ProductSapModule>();
@OneToMany(cascade={CascadeType.ALL}/*, fetch=FetchType.LAZY*/, mappedBy="sapModule"/*, targetEntity=com.apple.ist.espresso.admin.model.ProductSapModule.class*/)
//@BatchSize(size=5)
public Set<ProductSapModule> getProductSapModule() {
return productSapModule;
}
public void setProductSapModule(Set <ProductSapModule>productSapModule) {
this.productSapModule = productSapModule;
}
}
Code:
@EmbeddableSuperclass
public abstract class BaseAdminEntity extends BaseAuditEntity {
/**
* Business Name of the Entity
*/
protected String name;
/**
* Business description of the Entity
*/
protected String description;
/**
* True if this entry is active. False otherwise.
*/
private boolean isActive;
/**
* Primary Key for the Entity
*/
private Long id;
// private Integer version;
@Id(generate=GeneratorType.SEQUENCE, generator="SEQ_STORE")
public Long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(Long id) {
this.id = id;
}
@Column(name="NAME", nullable=false)
@Length(max=64) @NotNull
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
@Column(name="DESCRIPTION",nullable=false)
@Length(max=256) @NotNull
public String getDescription() {
return description;
}
/**
* @param description The description to set.
*/
public void setDescription(String description) {
this.description = description;
}
@Column(name="IS_ACTIVE", nullable=false)
public boolean isActive() {
return isActive;
}
/**
* @param isDeleted The isDeleted to set.
*/
public void setActive(boolean isActive) {
this.isActive = isActive;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return this.getClass()+" : " + "name = "+this.getName() + " , description = " + this.getDescription() + " , Active = " + this.isActive();
}
When i run the following JUnit test case,
Code:
public class SapModuleDAOTest extends DAOTestCase {
//private String tableName = "SAPMODULE";
private BaseAdminEntityDAO sapModuleDao = null;
// private Long id=new Long("1");
public static void main(String[] args) {
junit.textui.TestRunner.run(SapModuleDAOTest.class);
}
public SapModuleDAOTest(String name) {
super(name);
sapModuleDao = (BaseAdminEntityDAO) ctx.getBean("baseAdminEntityDAO");
}
public final void testGetById() throws Exception {
Long id;
String name = "sapModule"+System.currentTimeMillis();
String description = "sapModule desc";
boolean active = true;
//create a POJO
SapModule sapModule = new SapModule();
sapModule.setName(name);
sapModule.setDescription(description);
sapModule.setActive(active);
//Save
sapModuleDao.save(sapModule);
//Is the Local POJO fine ?
id=sapModule.getId();
SapModule sapModule1 = (SapModule) sapModuleDao.getById(SapModule.class, id);
Assert.assertNotNull("SapModule retrieved correctly", sapModule1);
}
public final void testGetByInvalidId() throws Exception {
Long testID = new Long("-1998");
SapModule sapModule = null;
try {
sapModule = (SapModule) sapModuleDao.getById(SapModule.class, testID);
fail("Invalid behavior");
} catch (ObjectRetrievalFailureException ex) {}
}
public final void testGetAllActive() {
Collection sapModules = sapModuleDao.getAllActive(SapModule.class);
Iterator sapModulesIterator = sapModules.iterator();
while (sapModulesIterator.hasNext() == true) {
SapModule sapModule = (SapModule) sapModulesIterator.next();
Assert.assertTrue("Active verification: ", sapModule.isActive());
}
}
Here's how my debug output looks:
Code:
[junit] Hibernate: select sapmodule0_.id as id, sapmodule0_.CREATED_DATE as CREATED2_49_, sapmodule0_.CREATED_BY_ID as CREATED3_49_, sapmodule0_.CREATED_BY_NAME as CREATED4_49_, sapmodule0_.MODIFIED_DATE as MODIFIED5_49_, sapmodule0_.MODIFIED_BY_ID as MODIFIED6_49_, sapmodule0_.MODIFIED_BY_NAME as MODIFIED7_49_, sapmodule0_.NAME as NAME49_, sapmodule0_.IS_ACTIVE as IS9_49_, sapmodule0_.DESCRIPTION as DESCRIP10_49_ from SAP_MODULE sapmodule0_ where sapmodule0_.IS_ACTIVE=?
[junit] Total number of sap modules: 53
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
[junit] Hibernate: update SAP_MODULE set CREATED_DATE=?, CREATED_BY_ID=?, CREATED_BY_NAME=?, MODIFIED_DATE=?, MODIFIED_BY_ID=?, MODIFIED_BY_NAME=? where id=?
As you can see, for each instance returned by the session.find query, i see an update statement.
The update statement does update only for the audit fields.
Here's how the BaseAuditEntity looks:
Code:
@EmbeddableSuperclass
public abstract class BaseAuditEntity implements Auditable {
private AuditInfo auditInfo;
@Type(type = "com.apple.ist.espresso.persistance.audit.AuditInfoType")
@Columns(columns = {
@Column(name="CREATED_DATE"/*, nullable=false*/),
@Column(name="CREATED_BY_ID"/*, nullable=false, scale=12*/),
@Column(name="CREATED_BY_NAME"/*, nullable=false*/, length=64),
@Column(name="MODIFIED_DATE"/*, nullable=false*/),
@Column(name="MODIFIED_BY_ID"/*, nullable=false, scale=12*/),
@Column(name="MODIFIED_BY_NAME"/*, nullable=false*/, length=64)})
public AuditInfo getAuditInfo() {
if(auditInfo == null) auditInfo = new AuditInfo();
return auditInfo;
}
public void setAuditInfo(AuditInfo auditInfo) {
this.auditInfo = auditInfo;
}
}
I spent almost the entire day debugging the problem with no luck. Could someone please tell me why do i see these many updates?
Thanks for any input in advance!