Results 1 to 2 of 2

Thread: JPA Inheritance

  1. #1
    videanuadrian is offline Member
    Join Date
    Dec 2010
    Location
    Bucharest Romania
    Posts
    40
    Rep Power
    0

    Default JPA Inheritance

    Hi all,
    i have some problems with JPA Inheritance.
    i have one Entity named Person

    PHP Code:
    @Entity
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name="personType",discriminatorType=DiscriminatorType.INTEGER)
    @Table(name="persons")
    public class Person implements Serializable{
    
       /* normally i should have something like 
        *private List<EmailAddress> emailAddresses;
        *private List<Phone> phones;
        *
        * setters and getters here 
        */
    
     ... bla bla bla ...
    
    
    }
    and two other entities that extends Person (Client

    PHP Code:
    @Entity
    @DiscriminatorValue("1")
    public class Client extends Person {
    ............................
    }
    and Contact

    PHP Code:
    @Entity
    @DiscriminatorValue("0")
    public class Contact extends Person {
        private static final long serialVersionUID = 1L;
    ..................................
    }
    ). Each Person is in a OneToMany relationship with other two entities: Phone and EmailAddress.

    Normally I should have inside Person class setters and getters for phone and emailAddress, but here i have encountered a problem.
    Because my entities are mapped in a bidirectional relationship with eachother, so in Phone and EmailAddress i should have a getPerson() method. That is not right, because i needed there a method as getClient() or getContact(), and due that fact i have moved phones and emailAddresses variables (with getters and setters ) from Person to Client/Contact. Yet another problem here :)
    In Phone and EmailAddress i have :
    PHP Code:
     @ManyToOne
        @JoinColumn(name="person_id")
        public Client getClient(){
            return this.client;
        }
    
        public void setClient(Client c){
            this.client = c;
        }
    
        @ManyToOne
        @JoinColumn(name="person_id")
        public Contact getContact(){
            return this.contact;
        }
    
        public void setContact(Contact c){
            this.contact = c;
        }
    and when i compile i get
    Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: xxx.vs.common.hibernate.entities.virtual.EmailAddr ess column: person_id (should be mapped with insert="false" update="false")

    I can fix this, but the question is : is this the appropriate way to achieve that ? Or i have to act somehow different ?

    Thanks in advance.

  2. #2
    videanuadrian is offline Member
    Join Date
    Dec 2010
    Location
    Bucharest Romania
    Posts
    40
    Rep Power
    0

Similar Threads

  1. Inheritance
    By terahawks in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 10:58 AM
  2. Inheritance
    By Nerijus in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 04:47 AM
  3. Inheritance
    By gpio in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 11:41 AM
  4. Inheritance example
    By kris4u4ever in forum New To Java
    Replies: 3
    Last Post: 03-21-2009, 03:53 PM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 01:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •