-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to specify a constant value for a JoinColumn?
PostPosted: Mon May 19, 2008 3:02 pm 
Newbie

Joined: Mon Apr 14, 2008 1:13 pm
Posts: 11
This seems like a pretty common thing to want to do but I haven't seen any example of how to accomplish this:

Basically, I want to have a JoinColumns annotation where I specify a constant value for one of the columns.

Here's a simple example to illustrate... basically I want to bind a Person object's homeAddress value to the Address table where Address.person_id = Person.person_id AND Address.addr_type = 1.

It seems like maybe JPA doesn't solve this itself but I'm hoping Hibernate Annotations provides a solution???

Code:
@Entity
public class Person {
    @Id
    @Column(name="person_id")
    private Long id;

    @JoinColumns({
        @Column(name="person_id"),
        @Column(??? - join to address record of type 1 ???)
    })
    private Address homeAddress;

    @JoinColumns({
        @Column(name="person_id"),
        @Column(??? - join to address record of type 2 ???)
    })
    private Address longAddress;

    ...
}

@Entity
@IdClass(Address.Key.class)
public class Address {
    @Id
    private Person resident;

    @Id
    private int addressType;

    ...

    public static class Key {
        @OneToOne
        @JoinColumn(name="person_id")
        private Person resident;
       
        @Id
        @Column(name="addr_type")
        private int addressType;

        ...
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 5:36 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
yes that about a constant value is often a requirement;
I usually have an enum type for the Address
(homeAddress, workAddress, ....) or you may prefer an Integer, so you can later add more types to your DB without changing the code.

The solution is to use a Map binding:

Code:
@Entity
public class Person {
    @Id
    private Long id;

    @OneToMany(mappedBy="ofPerson")
    @MapKey(name="addressType")
    private Map<AddressType,Address> addresses;
   ...
}
@Entity @IdClass(AddressPk.class)
public class Address {
   
    @Id @Enumerated
    @Column(name = "address_type_id", nullable = false, updatable = false )
    private AddressType addressType;

    @Id @ManyToOne
    @JoinColumn(name = "person_id", nullable = false, updatable = false )
    private Person ofPerson
   ...
}



You should add an appropriate AddressPk and use the same column names; if you get it right you get a very nice double key in Address: PK (type,person_id FK)

_________________
Sanne
http://in.relation.to/


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

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.