-->
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: Id for a OneToOne or ManyToOne (bi-directional)
PostPosted: Sat Mar 31, 2012 3:48 pm 
Newbie

Joined: Sat Mar 31, 2012 3:30 pm
Posts: 1
WikiBooks has a great page, Java Persistence/Identity and Sequencing that covers defining an Id for a OneToOne or ManyToOne in JPA 2.0. Works great. But the relationship is uni-directional. Is it possible to make it bi-directional?

It would be really useful to call Employee.phones.

Code:
@Entity
@IdClass(PhonePK.class)
public class Phone {

    @Id
    private String type;

    @ManyToOne
    @Id
    @JoinColumn(name="OWNER_ID", referencedColumnName="EMP_ID")
    private Employee owner;

    ...
}


Code:
public class PhonePK {

    private String type;
    private long owner;

    public PhonePK() {}

    public PhonePK(String type, long owner) {
        this.type = type;
        this.owner = owner;
    }

    public boolean equals(Object object) {
        if (object instanceof PhonePK) {
            PhonePK pk = (PhonePK)object;
            return type.equals(pk.type) && owner == pk.owner;
        } else {
            return false;
        }
    }

    public int hashCode() {
        return type.hashCode() + owner;
    }
}


Code:
@Entity
public class Employee {

    @Id
    @Column(name = "EMP_ID")
    private Integer id;

    private String firstName;
    private String lastName;

    @OneToMany(mappedBy="owner")
    private List<Phone> phones;

    ...
}


Thanks for your help.

UPDATE: This appears to work. Not sure what my issue was.


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.