NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
torinwalker
Joined: 01 Nov 2008 Posts: 2
|
Posted: Sat Nov 01, 2008 1:11 am Post subject: How would you design a trivial email system using EJB 3.0?? |
|
|
I'm new to EJB 3.0, but not a novice. I'm looking for input on how you, anyone, might design a simple mail system.
I have an Account entity whose primary key must be the foreign key to a Mailbox entity. The Mailbox entity must have four Folder entities that are mapped by name: Inbox, Outbox, Drafts, Trash. Each of these Folder entities must hold Message objects which are the messages themselves.
For this, I created the usual Account object and set it's mapping (relative to the Mailbox object) as:
| Code: |
@Entity
public class Account implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", nullable = false)
private Long id;
.
.
.
@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@PrimaryKeyJoinColumn
private Mailbox mailbox;
|
The Mailbox object, in turn, has the following:
| Code: |
public class Mailbox implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@PrimaryKeyJoinColumn
@MapKey(name="name")
private Map<String,Folder> folders = new HashMap();
|
Finally, the Folder object looks like this:
| Code: | public class Folder implements Serializable {
private static final long serialVersionUID = 1L;
public static final String INBOX = "Inbox";
public static final String DRAFTS = "Drafts";
public static final String OUTBOX = "Outbox";
public static final String TRASH = "Trash";
@Id
@Column(name = "ID", nullable = false)
private Long id;
@Id
@Column(name = "NAME", nullable = false)
private String name;
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@JoinTable(
name = "FOLDER_MESSAGE",
joinColumns = {
@JoinColumn(name="FOLDER_ID", referencedColumnName="ID"),
@JoinColumn(name="FOLDER_NAME", referencedColumnName="NAME")
},
inverseJoinColumns = @JoinColumn(name="MESSAGE_ID", referencedColumnName="ID")
)
private List<Message> messages = new ArrayList<Message>();
|
Unfortunately, Toplink is creating three tables: A MAILBOX table with a single (useless) column, 'ID', a MAILBOX_FOLDER table containing MAILBOX_ID, ID, and NAME, and a FOLDER table containing ID, NAME.
Ideally, I would like the Mailbox object to see the Folder object using its own table, using the ID of the Mailbox object as the foreign key to the Folder object, but also its MapKey (name) as the index into the Compound Key of the Folder object.
I've tried using a JoinTable on the Mailbox object, forcing it to use the FOLDER table as the join table, but encounter duplicate Folder insertions using the same key.
I hope someone out there has tried (and succeeded) to implement something just as crazy, and might provide some help.
One other possible alternative I've been considering is the use of a Mailbox object that is a tuple of ACCOUNT_ID, FOLDER_ID, and MESSAGE_ID, where multiple messages are stored such that (if ACCOUNT_ID = 1, and INBOX = 1, and MESSAGES { 1, 2, 3 }, the rows will look like:
ACCOUNT_ID, FOLDER_ID, MESSAGE_ID
1, 1, 1
1, 1, 2
1, 1, 3
But, I don't want to express the Mailbox in my Account object as a OneToMany relationship.
Ugh. Can anyone make a suggestion?
Torin... |
|
| Back to top |
|
 |
John Yeary Posted via mailing list.
|
Posted: Sat Nov 01, 2008 12:51 pm Post subject: How would you design a trivial email system using EJB 3.0?? |
|
|
I would suggest doing the various EJB tutorials first. I am not sure you have a solid understanding here.
As an alternative to your plan, I would suggest you figure out your database layout first. Once you have this in place, you can create entities from them using the wizards in NetBeans.
Another choice, consider restful web services.
On Fri, Oct 31, 2008 at 9:11 PM, torinwalker <address-removed ([email]address-removed[/email])> wrote:
| Quote: | I'm new to EJB 3.0, but not a novice. I'm looking for input on how you, anyone, might design a simple mail system.
I have an Account entity whose primary key must be the foreign key to a Mailbox entity. The Mailbox entity must have four Folder entities that are mapped by name: Inbox, Outbox, Drafts, Trash. Each of these Folder entities must hold Message objects which are the messages themselves.
For this, I created the usual Account object and set it's mapping (relative to the Mailbox object) as:
Code:
@Entity
public class Account implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", nullable = false)
private Long id;
.
.
.
@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@PrimaryKeyJoinColumn
private Mailbox mailbox;
The Mailbox object, in turn, has the following:
Code:
public class Mailbox implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@PrimaryKeyJoinColumn
@MapKey(name="name")
private Map<String,Folder> folders = new HashMap();
Finally, the Folder object looks like this:
Code:
public class Folder implements Serializable {
private static final long serialVersionUID = 1L;
public static final String INBOX = "Inbox";
public static final String DRAFTS = "Drafts";
public static final String OUTBOX = "Outbox";
public static final String TRASH = "Trash";
@Id
@Column(name = "ID", nullable = false)
private Long id;
@Id
@Column(name = "NAME", nullable = false)
private String name;
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@JoinTable(
name = "FOLDER_MESSAGE",
joinColumns = {
@JoinColumn(name="FOLDER_ID", referencedColumnName="ID"),
@JoinColumn(name="FOLDER_NAME", referencedColumnName="NAME")
},
inverseJoinColumns = @JoinColumn(name="MESSAGE_ID", referencedColumnName="ID")
)
private List<Message> messages = new ArrayList<Message>();
Unfortunately, Toplink is creating three tables: A MAILBOX table with a single (useless) column, 'ID', a MAILBOX_FOLDER table containing MAILBOX_ID, ID, and NAME, and a FOLDER table containing ID, NAME.
Ideally, I would like the Mailbox object to see the Folder object using its own table, using the ID of the Mailbox object as the foreign key to the Folder object, but also its MapKey (name) as the index into the Compound Key of the Folder object.
I've tried using a JoinTable on the Mailbox object, forcing it to use the FOLDER table as the join table, but encounter duplicate Folder insertions using the same key.
I hope someone out there has tried (and succeeded) to implement something just as crazy, and might provide some help.
One other possible alternative I've been considering is the use of a Mailbox object that is a tuple of ACCOUNT_ID, FOLDER_ID, and MESSAGE_ID, where multiple messages are stored such that (if ACCOUNT_ID = 1, and INBOX = 1, and MESSAGES { 1, 2, 3 }, the rows will look like:
ACCOUNT_ID, FOLDER_ID, MESSAGE_ID
1, 1, 1
1, 1, 2
1, 1, 3
But, I don't want to express the Mailbox in my Account object as a OneToMany relationship.
Ugh. Can anyone make a suggestion?
Torin...
|
--
John Yeary
--
http://javaevangelist.blogspot.com
"Far better it is to dare might things, to win glorious triumphs, even though checkered by failure,
than to take rank with those poor spirits who neither enjoy much nor suffer much, because they live in the grey
twilight that knows not victory nor defeat." -- Theodore Roosevelt |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|