users@glassfish.java.net

Re: How can I implement this relationship

From: <glassfish_at_javadesktop.org>
Date: Sat, 20 Dec 2008 11:06:27 PST

Some related suggestions (OR skip to #E ):

#A. I might add that, I would make ('entidad', 'idEntidad`) unique otherwise you might have:

id entidad', 'idEntidad`
1 Customer Jones
2 Employee James
3 Customer Jones

#B. What happens when you delete the Customer or Employee table? Your design says their phone details will continue to exist. On the other hand, if you 'references' the tables as in:

`idEntidad` integer (10) references Customer on delete cascade,

Then when you delete the Customer row, the customer phone row will also be deleted. I noticed that in your JPA Customer class, you already have:
               
cascade = CascadeType.ALL
               
Meaning, you want rows deleted in the phone table when the customer [that owns the phone details] is deleted. Granted, in your phone table, you would need reference to the Customer table.
   
#C. I have had similar design issues in the past i.e., attempting to use one table (say) phone to hold phone details of different objects simply because it is called “phone”. In OO design, you could have one super class (say User) for your Customer, Employee, and in Postgresql, you can use 'inherit' in your schema , or in MySql, you can implements inheritance with foreign key and references. And the you might something like:

`idEntidad` integer (10) not null, references User on delete cascade,

Since User is now the super class of both Customer and Employee.

#D Alternatively, you could create table each for User, Customer and Employee as follows:
customer_phone
employee_phone
user_phone

And [for example] in the customer_phone table, you might have:

`idEntidad` integer (10) not null, references Customer on delete cascade,

This way, you are simply splitting the bloated phone table into specialized tables that is a better database design and implementation.

#E: Here is perhaps the answer you want:

@Entity
@Table(name = "phones")
public class Phone implements Serializable {
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name="idEntidad", referencedColumnName="ID"),
        @JoinColumn(name=""entidad", referencedColumnName="ID")
   })
    private Customer customer;

I am assuming that your Customer table primary key is 'id', hence the use of “ID” above.[i][/i]
[Message sent by forum member 'nuffsaidx' (nuffsaidx)]

http://forums.java.net/jive/thread.jspa?messageID=322782