In the last post, I think I have put the question wrongly. Hence I am rephrasing my question again with the application code. Following is what I want to achieve:--
Problem: - I have a batch which I want to assign it to a user after creation. [Note: - a batch can be assigned even after creation.] For allocation, I have a screen where batch a presented and a list of users is shown in a drop down box. User can assign a batch to any user by selecting the user from the drop down and press allocate button. Following is the database schema.
Code:
CREATE TABLE CTSBATCHOW
(
BATCHID VARCHAR2(6 BYTE) NULL,
ALLOCATEDTO VARCHAR2(12 BYTE) NULL
)
ALTER TABLE CTSLATEST.CTSBATCHOW ADD (
CONSTRAINT FK5AB41E406998C22C FOREIGN KEY (ALLOCATEDTO)
REFERENCES CTSLATEST.CTSUSER (USERCODE));
{Note that it’s a nullable foreign key}
CREATE TABLE CTSLATEST.CTSUSER
(
USERCODE VARCHAR2(12 BYTE) NOT NULL,
USERNAME VARCHAR2(35 BYTE) NOT NULL,
}
When I get a batch which is not allocated to any user, Hibernate session.get() method returns a batch pojo which has a null user object shown below. Code:
CtsbatchDAO ctsbatchDAO = new CtsbatchDAO();
Ctsbatch ctsBatch = ctsbatchDAO.findByPK(batchatchId);
public class Ctsbatch implements Serializable {
private String batchid;
private Ctsuser ctsuserByAllocatedto; // this will be a null .
}
I am using jsf in the front end and following is my formbean. I am using Ctsbatch POJO directly instead of keeping one more copy of the same variables in the bean. Code:
public class BatchEntryBean {
/**
* Place where all the data of the batch will b stored. ie POJO class
* outward batch details.
*/
private Ctsbatchow ctsBatchow;
public String getAllocatedUserCode () {
return this.getCtsBatch().getCtsuser().getUsercode();
}
public void setAllocatedUserCode (String userCode) {
this.getCtsBatch().getCtsuser().setUsercode(userCode);
}
}
In the jsp page I have done something like this :-
Batch Name: - label #{batchEntryBean.batchid }
Allocate to: - dropdown with the list of users #{batchEntryBean. AllocatedUserCode} and code for drop down.
Allocate button.
Now having said that, when I allocate a batch to the user and press allocate button. Since Usercode object is null, Jsf is not able to set the Ctsuser.allocateUserCode properly and throws a null pointer exception.
How can I solve the problem ? any pointers will be highly appreciated.
Things which I tried :--
1) When I get the Batch object after findby pk, I checked if the user is null then I set new user object and show the jsp page. Jsf now allows me to populate the filed as ctsuser is not null. But hibernate throwz an exception.
Code:
2:57:29,140 INFO [STDOUT] Caused by: org.hibernate.TransientObjectException: com.anm.cts.dao.Ctsuser
12:57:29,140 INFO [STDOUT] at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
12:57:29,140 INFO [STDOUT] at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
12:57:29,140 INFO [STDOUT] at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:221)
12:57:29,140 INFO [STDOUT] at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:476)
But I can update the data successfully.
I would be very happy if somehow I can get the ctsbatch object which has a new ctsuser object inside (which doesn’t contain anything) when I say findbyPK. Is there any way by which I can achieve that ?
Sachin