Simple Two Table Example
clerum Oct 29, 2008 10:10 PMCan someone point me to a simple example where add something simple like a user but also tie that user to an organization.
All the tutorials and examples I have found are just creating a simple user. I've got that down but I'm having trouble seeing how to tie two objects together. My entity objects are created.
@Entity
@Name("user")
@Table(name="user")
public class User implements Serializable
{
private static final long serialVersionUID = -3128908747836433645L;
private long id;
private int version;
private String username;
private String password;
private String firstname;
private String lastname;
private Organization organization;
private Status status;
@GeneratedValue
@NotNull
@Id
public long getId() {
return id;
}
public void setId(long id)
{
this.id = id;
}
@Version
@NotNull
public int getVersion()
{
return version;
}
public void setVersion(int version)
{
this.version = version;
}
@Email
@Length(min=3,max=75)
@NotNull
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
@Length(min=64,max=64)
@NotNull
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
@Length(min=1,max=75)
@NotNull
public String getFirstname()
{
return firstname;
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
@Length(min=1,max=75)
@NotNull
public String getLastname()
{
return lastname;
}
public void setLastname(String lastname)
{
this.lastname = lastname;
}
@ManyToOne
@JoinColumn(name = "organization_id", nullable = false)
@NotNull
public Organization getOrganization() {
return organization;
}
public void setOrganization(Organization organization) {
this.organization = organization;
}
@NotNull
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
---
@Entity
@Name("organization")
@Table(name="organization")
public class Organization implements Serializable
{
private static final long serialVersionUID = -5182241903501025893L;
private long id;
private int version;
private String name;
private Set<User> users = new HashSet<User>(0);
@GeneratedValue
@NotNull
@Id
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Version
@NotNull
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
@Length(min=1,max=75)
@NotNull
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy="id")
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
---
public enum Status {
ACTIVE, DISABLED
}
But I'm having trouble grasping the best way to handle this from the action objects and the view .xhtml pages.