-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How can implement this relationship
PostPosted: Fri Dec 19, 2008 12:42 pm 
Newbie

Joined: Fri Dec 19, 2008 12:32 pm
Posts: 1
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`)
)


where I save all telephones of all my entities : Customers, Employees, 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.
Similar 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;
   
    ...

}


Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


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

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.