[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - How to handle several collections, which store the same enti

toni do-not-reply at jboss.com
Tue Jan 16 11:55:14 EST 2007


Hi,

I have an EmailMessage Entity, which contains four collections, which all reference the same type of entity bean, which in my case represents an EmailAddress.

So I have an EmailMessage and inside 4 collections, all of which in turn store EmailAddress Entities for the email fields to, cc, bcc and replyTo.

I looked in the database and the table for the relationship looks like this:

---------  emailmessage_emailaddress ----------------
emailmessage_id bigint 	NOT NULL
	
replyto_id bigint NOT NULL
bcc_id 	bigint 	NOT NULL
cc_id 	bigint 	NOT NULL
to_id 	bigint 	NOT NULL
-------------------------------------------------------------

If I create an EmailMessage and add one EmailAddress entity bean to one of the collections, then I get a contraint exception, because the "NOT NULL" constraint is violated.

How, can I set this up, so that this works? Is this possible at all?

Here are the 2 entity beans: 

-------------------------------------------------

@Entity
@Name("emailMessage")

public class EmailMessage implements Serializable
{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  long id;

  @OneToOne(cascade = {CascadeType.ALL})
  EmailAddress from;

  @OneToMany(cascade = {CascadeType.ALL})
  Collection to = new ArrayList();

  @Temporal(TemporalType.TIMESTAMP)
  Date sentDate;

  @OneToMany(cascade = {CascadeType.ALL})
  Collection cc = new ArrayList();

  @OneToMany(cascade = {CascadeType.ALL})
  Collection bcc = new ArrayList();

  @OneToMany(cascade = {CascadeType.ALL})
  Collection replyTo = new ArrayList();

...
}

------------------------------------------------------------

@Entity
@Name("emailAddress")

public class EmailAddress implements Serializable
{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  long id;
  
  String emailAddress;
  String name;
...
}



View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4002349#4002349

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4002349



More information about the jboss-user mailing list