Implementing Domain Model
damendra Sep 24, 2009 9:45 PMHi,
I'm a newbie to seam and have the following question (using seam version 2.1.2, JBoss 5.0, MySQL 5).
The following is my domain model User has a Profile, a Profile has a Contact and Profile has Statement. Code as follows:
@SuppressWarnings("serial")
@Entity
@Name("user")
@Scope(ScopeType.PAGE)
public class User implements Serializable {
private Long id;
private Profile profile;
//..getters/setters
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
}
@SuppressWarnings("serial")
@Entity
@Scope(ScopeType.PAGE)
@Name("profile")
public class Profile implements Serializable {
private Long id;
private User user;
private Contact contact;
private Statement statement;
//...
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Statement getStatement() {
return statement;
}
public void setStatement(Statement statement) {
this.statement = statement;
}
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
@OneToOne(mappedBy="profile", cascade = CascadeType.ALL)
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
@SuppressWarnings("serial")
@Entity
@Name("contact")
@Scope(ScopeType.PAGE)
public class Contact implements Serializable {
private Long id;
private Profile profile;
//...
@OneToOne(mappedBy="contact", cascade=CascadeType.ALL)
public Profile getProfile() {
return profile;
}
//..setter
}
@SuppressWarnings("serial")
@Entity
@Name("statement")
@Scope(ScopeType.PAGE)
public class Statement implements Serializable {
private Long id;
private String statement;
private Profile profile;
//....
@OneToOne(mappedBy="statement", cascade=CascadeType.ALL)
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
}Persisting is done through Stateful Session Beans scoped in the Conversation Scope.
When i persist either the Contact or Statement i retrieve the User, from which i try and fetch the Profile. hence,
User loggedInUser =
(User) entityManager
.createQuery("select u from User u where u.username=#{credentials.username}").getSingleResult();
Now when i persist Contact, i attach the Contact to the Profile and pass to em.persist()
if(loggedInUser.getProfile() != null) {
Profile userProfile = loggedInUser.getProfile();
userProfile.setContact(contact);
contact.setProfile(userProfile);
entityManager.persist(contact);
} - persists successfully. When i want to persist the Statement, i retrieve the profile added as above but it returns null.
Profile profile = loggedInUser.getProfile() //--returns null profile.setStatement(statement); statement.setProfile(profile);
Not sure why this is behaving as is. Can anyone provide some guidance please?
Regards