I'm trying to model a many-to-one bi-directional association using JPA. The join uses a formula. I've tried it a couple of ways, as shown below. Once with just JoinFormula and another with JoinColumnsOrFormulas.
public class JobOperation
{
private Operation operation;
#ManyToOne
// #JoinFormula("CASE WHEN attribute7 IS NULL OR TO_NUMBER(attribute7) = 0 THEN standard_operation_id ELSE TO_NUMBER(attribute7) END")
#JoinColumnsOrFormulas(
{
#JoinColumnOrFormula(formula = #JoinFormula(//
value = "(CASE WHEN this_.attribute7 IS NULL OR TO_NUMBER(this_.attribute7) = 0 THEN this_.standard_operation_id ELSE TO_NUMBER(this_.attribute7) END)", //
referencedColumnName = "standard_operation_id"))
})
#Fetch(FetchMode.SELECT)
#NotFound(action = NotFoundAction.IGNORE)
public Operation getOperation()
{
return this.operation;
}
}
I originally was using Hibernate 4.3.9 and then tried with Hibernate 5.1.0. Both throw the same exception:
15:55:21,408 DEBUG [org.hibernate.cfg.annotations.TableBinder] Retrieving property com.icumed.ifactory3.dto.wip.JobOperation.operation
15:55:21,409 DEBUG [org.hibernate.jpa.HibernatePersistenceProvider] Unable to build entity manager factory
java.lang.ClassCastException: org.hibernate.mapping.Formula cannot be cast to org.hibernate.mapping.Column
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:584)
Nothing in Hibernate's TableBinder class refers to a formula. Does Hibernate just not support this or am I using the wrong annotations, or is there something else going on?
The root cause of the problem seems to be on the other side of the association. I originally had this
public class Operation extends AbstractOperation
{
#OneToMany(mappedBy="operation")
public Set<JobOperation> getJobOperations()
{
return this.jobOperations;
}
}
and when I changed it to the following, it worked.
public class Operation extends AbstractOperation
{
#OneToMany
#JoinColumn(name="STANDARD_OPERATION_ID")
public Set<JobOperation> getJobOperations()
{
return this.jobOperations;
}
}
Related
I'm currently playing around on Spring boot 1.4.2 in which I've pulled in Spring-boot-starter-web and Spring-boot-starter-jpa.
My main issue is that when I save a new entity it works fine (all cool).
However if I save a new product entity with the same id (eg a duplicate entry), it does not throw an exception. I was expecting ConstrintViolationException or something similar.
Given the following set up:
Application.java
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
ProductRepository.java
#Repository
public interface ProductRepository extends JpaRepository<Product, String> {}
JpaConfig.java
#Configuration
#EnableJpaRepositories(basePackages = "com.verric.jpa.repository" )
#EntityScan(basePackageClasses ="com.verric.jpa")
#EnableTransactionManagement
public class JpaConfig {
#Bean
JpaTransactionManager transactionManager() {
return new JpaTransactionManager();
}
}
Note JpaConfig.java and Application.java are in the same package.
ProductController.java
#RestController
#RequestMapping(path = "/product")
public class ProductController {
#Autowired
ProductRepository productRepository;
#PostMapping("createProduct")
public void handle(#RequestBody #Valid CreateProductRequest request) {
Product product = new Product(request.getId(), request.getName(), request.getPrice(), request.isTaxable());
try {
productRepository.save(product);
} catch (DataAccessException ex) {
System.out.println(ex.getCause().getMessage());
}
}
}
and finally Product.java
#Entity(name = "product")
#Getter
#Setter
#AllArgsConstructor
#EqualsAndHashCode(of = "id")
public class Product {
protected Product() { /* jpa constructor*/ }
#Id
private String id;
#Column
private String name;
#Column
private Long price;
#Column
private Boolean taxable;
}
The getter, setter and equalsHashcode.. are lombok annotations.
Miscellaneous:
Spring boot : 1.4.2
Hibernate ORM: 5.2.2.FINAL
This issue happens regardless if I annotate the controller with or without #Transactional
The underlying db shows the exception clearly
2016-11-15 18:03:49 AEDT [40794-1] verric#stuff ERROR: duplicate key value violates unique constraint "product_pkey"
2016-11-15 18:03:49 AEDT [40794-2] verric#stuff DETAIL: Key (id)=(test001) already exists
I know that is better (more common) to break the data access stuff into its own service layer instead of dumping it in the controller
The semantics of the controller aren't ReST
Things I've tried:
Spring CrudRepository exceptions
I've tried implementing the answer from this question, unfortunately my code never ever hits the DataAccesException exception
Does Spring JPA throw an error if save function is unsuccessful?
Again similar response to the question above.
http://www.baeldung.com/spring-dataIntegrityviolationexception
I tried adding the bean to my JPAconfig.java class that is:
#Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
But nothing seemed to happen.
Sorry for long post, ty in advance
My solution is a lot cleaner. Spring Data already provides a nice way for us to define how an entity is considered to be new. This can easily be done by implementing Persistable on our entities, as documented in the reference.
In my case, as is the OP's, the IDs come from an external source and cannot be auto generated. So the default logic used by Spring Data to consider an entity as new if the ID is null wouldn't have worked.
#Entity
public class MyEntity implements Persistable<UUID> {
#Id
private UUID id;
#Transient
private boolean update;
#Override
public UUID getId() {
return this.id;
}
public void setId(UUID id) {
this.id = id;
}
public boolean isUpdate() {
return this.update;
}
public void setUpdate(boolean update) {
this.update = update;
}
#Override
public boolean isNew() {
return !this.update;
}
#PrePersist
#PostLoad
void markUpdated() {
this.update = true;
}
}
Here, I have provided a mechanism for the entity to express whether it considers itself new or not by means of another transient boolean property called update. As the default value of update will be false, all entities of this type are considered new and will result in a DataIntegrityViolationException being thrown when you attempt to call repository.save(entity) with the same ID.
If you do wish to perform a merge, you can always set the update property to true before attempting a save. Of course, if your use case never requires you to update entities, you can always return true from the isNew method and get rid of the update field.
The advantages of this approach over checking whether an entity with the same ID already exists in the database before saving are many:
Avoids an extra round trip to the database
We cannot guarantee that by the time one thread has determined that this entity doesn't exist and is about to persist, another thread doesn't attempt to do the same and result in inconsistent data.
Better performance as a result of 1 and having to avoid expensive locking mechanisms.
Atomic
Simple
EDIT: Don't forget to implement a method using JPA callbacks that sets the correct state of the update boolean field just before persisting and just after loading from the database. If you forget to do this, calling deleteAll on the JPA repository will have no effect as I painfully found out. This is because the Spring Data implementation of deleteAll now checks if the entity is new before performing the delete. If your isNew method returns true, the entity will never be considered for deletion.
I think you are aware of CrudRepository.save() is used for both insert and update. If an Id is non existing then it will considered an insert if Id is existing it will be considered update. You may get an Exception if your send the Id as null.
Since you don't have any other annotations apart from #Id on your id variable, The Unique Id generation must be handled by your code Or else you need to make use of #GeneratedValue annotation.
To build upon Shazins answer and to clarify. the CrudRepositroy.save() or JpaRespository.saveAndFlush() both delegate to the following method
SimpleJpaRepository.java
#Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
Hence if a user tries to create a new entity that so happens to have the same id as an existing entity Spring data will just update that entity.
To achieve what I originally wanted the only thing I could find was to drop back down to JPA solely, that is
#Transactional
#PostMapping("/createProduct")
public Product createProduct(#RequestBody #Valid Product product) {
try {
entityManager.persist(product);
entityManager.flush();
}catch (RuntimeException ex) {
System.err.println(ex.getCause().getMessage());
}
return product;
}
Here if we try to persist and new entity with an id already existing in the database it will throw will throw the constraint violation exception as we originally wanted.
Note that there are 3 scenarios here:
1. Setting ID manually
If there is no choice(like the OP), i.e if you are setting your own id "manually", Spring Data JPA is assuming that you want to check if there are duplicates(hence the SELECT), so it will do a "(i)SELECT + (ii)INSERT" if there is no existing record or a "(i)SELECT + (ii)UPDATE" if there is already an existing record.
In short, 2 SQLs!
2. Use an ID Generator
Cleaner & better, for example:
#Id
#GeneratedValue(generator = "my-uuid")
#GenericGenerator(name = "my-uuid", strategy = "uuid2")
private UUID id;
Result: there is ALWAYS only 1 INSERT statement.
3. Implement Persistable and isNew()
This has already been brilliantly answered by #adarshr, but is also more painful, i.e to implement Persistable(instead of Serializable), and implement the isNew() method.
Result: Also, 1 INSERT statement.
According to Spring Data documentation Spring persists an entity if does not exists or merge, this means update, the existing one:
Saving an entity can be performed via the CrudRepository.save(…)-Method. It will persist or merge the given entity using the underlying JPA EntityManager. If the entity has not been persisted yet Spring Data JPA will save the entity via a call to the entityManager.persist(…)-Method, otherwise the entityManager.merge(…)-Method will be called.
I am working on a Spring-MVC appplication in which I have 3 classes, GroupCanvas, GroupSection, GroupNotes. GroupCanvas has one-to-many mapping with GroupSection and GroupSection has one-to-many mapping with GroupNotes. I am trying to retrieve notes based upon GroupCanvas's primary key, but I am getting a Hibernate Lazy Initialization Exception. I tried out the recommendations on net, mostly SO, but none of them seem to help. Here is code.
DAO Method throwing error :
#Override
public List<GroupNotes> searchNotesByDays(int days, int mcanvasid) {
Session session = this.sessionFactory.getCurrentSession();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_YEAR, -days);
long daysAgo = cal.getTimeInMillis();
Timestamp nowMinusDaysAsTimestamp = new Timestamp(daysAgo);
Query query = session.createQuery("from GroupSection as n where n.currentcanvas.mcanvasid=:mcanvasid");
query.setParameter("mcanvasid", mcanvasid);
List<GroupSection> sectionList = query.list();
List<GroupNotes> notesList = new ArrayList<GroupNotes>();
for (GroupSection e : sectionList) {
Query query1 = session.createQuery("from GroupNotes as n where n.ownednotes.msectionid=:msectionid and n.noteCreationTime >:limit");
query1.setParameter("limit", nowMinusDaysAsTimestamp);
query1.setParameter("msectionid",e.getMsectionid());
notesList.addAll(query1.list());
}
return notesList;
}
GroupCanvas model :
#Entity
#Table(name = "membercanvas")
public class GroupCanvas{
variables, getters, setters ignored
#OneToMany(mappedBy = "currentcanvas",fetch=FetchType.LAZY, cascade = CascadeType.REMOVE)
#JsonIgnore
private Set<GroupSection> ownedsection = new HashSet<>();
public Set<GroupSection> getOwnedsection() {
return this.ownedsection;
}
public void setOwnedsection(Set<GroupSection> ownedsection) {
this.ownedsection = ownedsection;
}
}
GroupSection model class :
#Entity
#Table(name = "membersection")
public class GroupSection {
#ManyToOne
#JoinColumn(name = "groupcanvasid",nullable = false)
#JsonIgnore
private GroupCanvas currentcanvas;
public GroupCanvas getCurrentcanvas() {
return this.currentcanvas;
}
public void setCurrentcanvas(GroupCanvas currentcanvas) {
this.currentcanvas = currentcanvas;
}
public int getCurrentCanvasId(){
return this.currentcanvas.getMcanvasid();
}
#OneToMany(mappedBy = "ownednotes", fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
#JsonIgnore
private Set<GroupNotes> sectionsnotes = new HashSet<>();
public Set<GroupNotes> getSectionsnotes(){
return this.sectionsnotes;
}
public void setSectionsnotes(Set<GroupNotes> sectionsnotes){
this.sectionsnotes=sectionsnotes;
}
}
GroupNotes :
#Entity
#Table(name="groupnotes")
public class GroupNotes{
#ManyToOne
#JoinColumn(name = "msectionid")
#JsonIgnore
private GroupSection ownednotes;
public GroupSection getOwnednotes(){return this.ownednotes;}
public void setOwnednotes(GroupSection ownednotes){this.ownednotes=ownednotes;}
}
Error log :
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.journaldev.spring.model.GroupCanvas.ownedsection, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.journaldev.spring.model.GroupNotes["ownednotes"]->com.journaldev.spring.model.GroupSection["currentcanvas"]->com.journaldev.spring.model.GroupCanvas["ownedsection"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.journaldev.spring.model.GroupCanvas.ownedsection, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.journaldev.spring.model.GroupNotes["ownednotes"]->com.journaldev.spring.model.GroupSection["currentcanvas"]->com.journaldev.spring.model.GroupCanvas["ownedsection"])
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:256)
What am I doing wrong, kindly let me know. If there is any more information required, kindly put a comment.
Your JSON converter is executed after the Hibernate session is completed. The JSON converter is blindly accessing all the getters and setters, even the lazy ones. So when Hibernate tries to initialize GroupCanvas#ownedSection, there is no session available and hence this exception is thrown.
Possible solutions:
Do not directly execute JSON converter on the Hibernate managed objects. Create DTO objects to do this job. DTO objects have no logic and are pure java beans and fit this role well. But the drawback is you have to maintain another class hierarchy. The benefits do outweigh the drawbacks. The following post can help with this approach:
DTO pattern : Best way to copy properties between two Objects
Use annotations to mark certain fields as not serializable. For example, JsonIgnore. The drawback with this is that if this field is ever needed in a different API, then you cannot use this.
If one of the back-ref can be eliminated from your model (notes->section/section->canvas), then that makes it "friendlier" to serialization. In other works, JSON does not work well with cyclic references, so the lesser the amount of bi-directional/loop constructs the better it is. If it were not for the possibility of a cyclic reference, then you could initialize all the data necessary for serialization including GroupCanvas.
I faced with a very strange behavior in my web app with spring 3 and hibernate-core 3.5.1-Final.
For simplicity i provide my code..
if(ripid!=null){ //Parameter
Appuntamento apDaRip = appuntamentoService.findById(ripid);
if(apDaRip.getIdpadre()!=null){
apDaRip.setNota("RIPROGRAMMATO n."+ripid.toString()+"\n"+apDaRip.getNota());
apDaRip.setIdpadre(apDaRip.getIdpadre());
}else{
apDaRip.setNota("RIPROGRAMMATO n."+ripid.toString()+"\n"+apDaRip.getNota());
apDaRip.setIdpadre(ripid);
}
try{
apDaRip.setOrarioinizio(null);
apDaRip.setDurata(null);
//apDaRip.setIdappuntamento(null);
}catch(Exception e){e.printStackTrace();}
map.put("appuntamento", apDaRip);
}
di = datiintranetService.findById(DatiintranetService.PASS_X_INTERVENTI);
map.put("passinterventi", di.getBoolean());
The idea behind is to use some data of an object "Appuntamento" for produce a new one.
So i'm going to change some value and before send the object to my view (jsp) i fetch other data by calling findbyid. This cause an update to the Appuntamento object... Off course i don't want this behavior. Someone can have an explanation of this?
Edit-1
Here's the Dao
#Transactional
public class DatiintranetService {
private DatiintranetDAO datiintranetDAO;
public void setDatiintranetDAO(DatiintranetDAO datiintranetDAO) {
this.datiintranetDAO = datiintranetDAO;
}
public DatiintranetDAO getDatiintranetDAO() {
return datiintranetDAO;
}
public Datiintranet findById(Integer id) {
return datiintranetDAO.findById(id);
}
}
and For Appuntamento class I provide to you a snapshot
#Entity
#Table(name = "appuntamento", schema = "public")
public class Appuntamento implements java.io.Serializable {
#Id
#SequenceGenerator(name="appuntamentoID", sequenceName="appuntamento_idappuntamento_seq",allocationSize =1)
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="appuntamentoID")
#Column(name = "idappuntamento", unique = true, nullable = false)
public Integer getIdappuntamento() {
return this.idappuntamento;
}
}
Edit-2
IF i move thoese two row above the if statement no update occur.
di = datiintranetService.findById(DatiintranetService.PASS_X_INTERVENTI);
map.put("passinterventi", di.getBoolean());
If you query for an entity and change the entity, the default behavior is to persist those changes via an update to the database. This is usually what you want to happen, but obviously not in all cases.
If you want to avoid the update, you need to detach the entity by calling session.evict(apDaRip) where session is a reference to the hibernate session (see Session.evict()). You probably want to evict the entity right after you get it (immediately following the call to findById).
I was having problems today with lazy loading not working when using a mapped by collection. I found this excellent article that seems to fix the problem
http://justonjava.blogspot.co.uk/2010/09/lazy-one-to-one-and-one-to-many.html
One thing I do not understand is how the workaround using FieldHandled works. Can anyone help me understand this? The code in question is below (copied from the example on the link):
#Entity
public class Animal implements FieldHandled {
private Person owner;
private FieldHandler fieldHandler;
#OneToOne(fetch = FetchType.LAZY, optional = true, mappedBy = "animal")
#LazyToOne(LazyToOneOption.NO_PROXY)
public Person getOwner() {
if (fieldHandler != null) {
return (Person) fieldHandler.readObject(this, "owner", owner);
}
return owner;
}
public void setOwner(Person owner) {
if (fieldHandler != null) {
this.owner = fieldHandler.writeObject(this, "owner", this.owner, owner);
return;
}
this.owner = owner;
}
public FieldHandler getFieldHandler() {
return fieldHandler;
}
public void setFieldHandler(FieldHandler fieldHandler) {
this.fieldHandler = fieldHandler;
}
}
What am I missing? Perhaps I dont know enough about hibernate's lifecycle here? Im happy to investigate but can anyone give me some pointers.
Thanks in advance.
EDIT
I pushed through a lot of changes so a lot of my entities implemented FieldHandled but then discovered some of my tests were failing. I pumped out the SQL and got some weird things where the SQLs were happening in different orders if this interface was implemented with just these methods set.
public FieldHandler getFieldHandler() {
return fieldHandler;
}
public void setFieldHandler(FieldHandler fieldHandler) {
this.fieldHandler = fieldHandler;
}
This was causing tests to fail as things were not quite in the correct state when I was asserting. This adds to my mis-understanding of this FieldHandler variable.
The following code tells Hibernate to use interception handler instead of proxy.
#LazyToOne(LazyToOneOption.NO_PROXY)
From javadoc:
give back the real object loaded when a reference is requested (Bytecode enhancement is mandatory for this option, fall back to PROXY if the class is not enhanced)
As can be seen, it's required to instrument the bytecode before using it. 'Persisted class is enhanced' after its 'bytecode is instrumented'.
The idea is to fool Hibernate that the entity class which we want to use has been already instrumented
Instrumentation task is called after code is compiled. Instrumented entity extends FieldHandled. FieldHandled is an 'Interface introduced to the enhanced class'
Hibernate verifies entity at a run time and comes to a conclusion that class was enhanced that's why it uses the real object instead of proxy and isn't loading related entity object as it normally did.
Edit:
Lets take a look under the hood:
AnnotationBinder handles NO_PROXY option
if ( lazy != null ) {
toOne.setLazy( !( lazy.value() == LazyToOneOption.FALSE ) );
toOne.setUnwrapProxy( ( lazy.value() == LazyToOneOption.NO_PROXY ) );
}
Both org.hibernate.mapping.ManyToOne and org.hibernate.mapping.OneToOne are subclasses of org.hibernate.mapping.ToOne. ToOne#isUnwrapProxy() only usage is in #getType:
getMappings().getTypeResolver().getTypeFactory().oneToOne(
Both ManyToOneType and OneToOneType are subclasses of EntityType and only usage of 'EntityType#unwrapProxy' is in EntityType#resolveIdentifier(Serializable, SessionImplementor)
boolean isProxyUnwrapEnabled = unwrapProxy &&
session.getFactory()
.getEntityPersister( getAssociatedEntityName() )
.isInstrumented();
Here's Tentative call hierarchy: AbstractEntityPersister#isInstrumented() ->EntityMetamodel#isInstrumented() ->EntityInstrumentationMetadata#isInstrumented() -> etc. and finally BytecodeProviderImpl.EntityInstrumentationMetadataImpl.EntityInstrumentationMetadataImpl()
this.isInstrumented = FieldHandled.class.isAssignableFrom( entityClass );
That's why it's required either instrument the code (e.g with InstrumentTask) or implement FieldHandled.
In order to make long story short you may take a look at EntityType#resolveIdentifier(Serializable, SessionImplementor). That's the reason why second object is not loaded even if it's nullable.
The FieldHandled interface has been replaced with the PersistentAttributeInterceptable interface in Hibernate 5. You can achieve the same result by implementing this new interface:
#Entity
public class Animal implements PersistentAttributeInterceptable {
private Person owner;
private PersistentAttributeInterceptor interceptor;
#OneToOne(fetch = FetchType.LAZY, optional = true, mappedBy = "animal")
#LazyToOne(LazyToOneOption.NO_PROXY)
public Person getOwner() {
if (interceptor != null) {
return (Person) interceptor.readObject(this, "owner", owner);
}
return owner;
}
public void setOwner(Person owner) {
if (interceptor != null) {
this.owner = interceptor.writeObject(this, "owner", this.owner, owner);
return;
}
this.owner = owner;
}
#Override
public PersistentAttributeInterceptor $$_hibernate_getInterceptor() {
return interceptor;
}
#Override
public void $$_hibernate_setInterceptor(PersistentAttributeInterceptor interceptor) {
this.interceptor = interceptor;
}
}
I have the following two classes: Claim (parent) and ClaimInsurance (child). They are as follows:
public class Claim {
private SortedSet<ClaimInsurance> claimInsurances = new TreeSet<ClaimInsurance>();
#OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="claim", orphanRemoval=true)
#Sort(type=SortType.NATURAL)
public SortedSet<ClaimInsurance> getClaimInsurances() {
return this.claimInsurances;
}
public void setClaimInsurances(SortedSet<ClaimInsurance> claimInsurances) {
this.claimInsurances = claimInsurances;
}
}
And:
public class ClaimInsurance implements java.io.Serializable, Comparable<ClaimInsurance> {
private Claim claim;
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="ClaimId", nullable=false)
public Claim getClaim() {
return this.claim;
}
public void setClaim(Claim claim) {
this.claim = claim;
}
}
When I try to delete the Claim it gives following Exception
org.hibernate.exception.ConstraintViolationException: could not delete: [com.omnimd.pms.beans.Claim#201]
...
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The DELETE statement conflicted with the REFERENCE constraint "FK_RCMSClaimInsuranceTable_RCMSClaimTable". The conflict occurred in database "Omnimdv12", table "dbo.RCMSClaimInsuranceTable", column 'ClaimId'.
When I change the claimInsurances mapping in the Claim class as follows, everything works fine:
private Set<ClaimInsurance> claimInsurances = new HashSet<ClaimInsurance>();
#OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="claim", orphanRemoval=true)
public Set<ClaimInsurance> getClaimInsurances() {
return this.claimInsurances;
}
public void setClaimInsurances(Set<ClaimInsurance> claimInsurances) {
this.claimInsurances = claimInsurances;
}
It seems that the problem is when I use Set (HashSet) in the mapping it works, but if I instead use SortedSet (TreeSet) it gives an error.
What could be the actual problem? What am I missing?
OK. The problem is resolved now. with the help of #JB Nizet
For same Claim i was having several ClaimInsurance whose compareTo() giving same result.
I changed the compareTo() in ClaimInsurance such that it will return different values for same Claim..that's it, its working now.