Reputation: 13002
I used Spring Roo to reverse engineer a MySQL database, a db to which I only have read access.
One of the entities called Transaction has a field called originalTransaction, in the DB this is set that it can be null, but that the default value is 0.
@RooJavaBean
@RooToString
@SuppressWarnings("serial")
@RooDbManaged(automaticallyDelete = true)
@RooJpaActiveRecord(versionField = "", table = "TRANSACTIONS")
public class Transaction implements Serializable {
....
@Fetch(value = FetchMode.SELECT)
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "ORIG_TRANS_ID")
private Transaction originalTransaction;
....
}
FetchMode.Select and FetchType.Lazy I added as workarounds for other problems.
Now when I insert a transaction and I leave originalTransaction null, the DB changes it to 0.
When I do an entity().find(12345678), I get the following error:
Unable to find Transaction with id 0
javax.persistence.EntityNotFoundException: Unable to find Transaction with id 0
at org.hibernate.ejb.Ejb3Configuration$Ejb3EntityNotFoundDelegate.handleEntityNotFound(Ejb3Configuration.java:157)
at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:261)
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:175)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:285)
So obviously (which wasn't so obvious initially), it's trying to find the originalTransaction and since the DB changes the null to 0, it's trying to find Transaction with id=0 which doesn't exist - this causes entity.find(12345678) to return null even though the transaction is in the database.
Is there any workaround to stop Hibernate / JPA from bailing the search and returning null if the originalTransaction's id in the DB doesn't exist? I'd preferably force it to null if it doesn't exist.
Upvotes: 2
Views: 746
Reputation: 1337
If you use Hibernate you can try
@NotFound(action = NotFoundAction.IGNORE)
Otherwise you have to clean up your database (0 is not null
). If there is no parent, the ORIG_TRANS_ID
should be null
Upvotes: 1