[jboss-user] [JBoss Seam] - Re: EntityManger-per-user-session...
bkyrlach
do-not-reply at jboss.com
Tue Nov 7 13:54:35 EST 2006
Not really. I'm using that now, and I'm still ending up doing merging and funky stuff to make everything "Just Work"(tm)(c)(pp).
Here's some (poorly written, tbh) code to illustrate...
I have a user...
@Entity
@Name("user")
@Scope(ScopeType.SESSION)
@Role(name="newUser", scope=ScopeType.EVENT)
public class User implements Serializable
{
private Long id;
private Long version;
private Long index;
private String username;
private String password;
private List activities;
private Team team;
public User(Long id, Long version, Long index, String username, String password, List activities, Team team)
{
this.id = id;
this.version = version;
this.index = index;
this.username = username;
this.password = password;
this.activities = activities;
this.team = team;
}
public User() {}
@Id @GeneratedValue
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
@Version
public Long getVersion()
{
return version;
}
public void setVersion(Long version)
{
this.version = version;
}
public Long getIndex()
{
return index;
}
public void setIndex(Long index)
{
this.index = index;
}
@OneToMany(fetch=FetchType.EAGER, mappedBy="user")
@OrderBy("name")
@IndexColumn(name="id")
public List getActivities()
{
return activities;
}
public void setActivities(List activities)
{
this.activities = activities;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
@ManyToOne
public Team getTeam()
{
return team;
}
public void setTeam(Team team)
{
this.team = team;
}
public String toString()
{
return this.getClass().getName() + "@" + (id == null ? "null" : id);
}
public boolean equals(Object o)
{
if(o.getClass().equals(this.getClass()))
{
return (o.toString().equals(this.toString()));
}
return false;
}
public int hashCode()
{
return this.toString().hashCode();
}
}
And an activity...
@Entity
@Name("activity")
@Scope(ScopeType.CONVERSATION)
@CausesRefresh(objectsToRefresh="user")
public class Activity implements Serializable
{
private Long id;
private String name;
private User user;
private Date date;
private Long version;
private Long index;
public Activity(Long id, Long index, String name, User user, Date date, Long version)
{
// TODO Auto-generated constructor stub
this.name = name;
this.user = user;
this.date = date;
this.version = version;
}
public Activity() {}
@Id @GeneratedValue
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public Long getIndex()
{
return index;
}
public void setIndex(Long index)
{
this.index = index;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@ManyToOne
public User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
@Version
public Long getVersion()
{
return version;
}
public void setVersion(Long version)
{
this.version = version;
}
public String toString()
{
return this.getClass().getName() + "@" + (id == null ? "null" : id) + "[" + "date" + "=" + (date == null ? "null" : date.toString()) + "]";
}
public boolean equals(Object o)
{
if(o.getClass().equals(this.getClass()))
{
return (o.toString().equals(this.toString()));
}
return false;
}
public int hashCode()
{
return this.toString().hashCode();
}
}
Now, when I go to edit an activity I have to do the following (this is with pc-per-conversation)...
List userActivities = user.getActivities();
for(Activity a: userActivities)
{
if(a.getId().equals(activity.getId()))
{
userActivities.set(userActivities.indexOf(a), activity);
}
}
user.setActivities(userActivities);
Create is even worse, since I have to make sure I'm inserting the activity into the user's activities collection as per the ordering specified by the annotations on the get/setActivities...
Now, a second way to do it is to...
public String preEditActivity()
{
...
...
...
em.merge(user);
...
...
...
}
and in save...
public String saveActivity()
{
...
...
...
em.flush();
em.refresh(user);
}
Which also seems unessecarily ugly. That's because the user is getting pulled from the database in the UserActions SLSB, so the extended PC for ActivityActions SFSB (conversation scoped) doesn't contain an attached reference for the user bean. Even the merge/refresh wayof doing it in the second psuedo-code set is still kind of ugly... especially if the EM decides to do a full database query. Imagine if user.getActivities() had 3000 elements in it.
Sorry for the rambling. I'm just trying to understand.
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3983881#3983881
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3983881
More information about the jboss-user
mailing list