users@glassfish.java.net

How can I implement this relationship

From: <glassfish_at_javadesktop.org>
Date: Fri, 19 Dec 2008 08:56:53 PST

First of all apologies for my poor English, I'm from Galicia.
I have a table called 'phones' like this
[code]
CREATE TABLE `phones`
(
        `id` integer (10) NOT NULL AUTO_INCREMENT ,
        `entidad` varchar (50),
        `idEntidad` integer (10),
        `tipo` varchar (50),
        `numero` varchar (200),
        PRIMARY KEY (`id`)
)
[/code]

where I save all telephones of all my entities : Customers, Employee, Users....
To get the phones of a Customer I write : Select * from phones where entidad = 'CUSTOMER' AND idEntidad = 'CustomerId'
To get the phones of a Employee I write : Select * from phones where entidad = 'EMPLOYEE' AND idEntidad = 'EmployeId'

My question is:
Is there any way to implement this relationship with Java Persistence Api(JPA) or Hibernate.
Similir to
[code]
@Entity
@Table(name = "customers")
public class Customer implements Serializable {
    
    @OneToMany(mappedBy="Customer",
               targetEntity=Phone.class,
               fetch=FetchType.LAZY,
               cascade = CascadeType.ALL
               )
               
    private List<Phone> phones;
}

@Entity
@Table(name = "phones")
public class Phone implements Serializable {
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="idEntidad" AND "entidad = 'CUSTOMER' " )
    private Customer customer;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="idEntidad" AND "entidad = 'EMPLOYEE' " )
    private Employee employee;

}
[/code]

Thanks.
[Message sent by forum member 'tonhinbm' (tonhinbm)]

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