-->
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: Composite key mapping (FK) issue
PostPosted: Mon Dec 10, 2007 10:40 pm 
Newbie

Joined: Wed Oct 12, 2005 9:43 am
Posts: 8
Hello everyone , i have not very regular situation with mapping. Hovever someone knew how to solve it.

It will be really great.

We have four table involved in this process

Code:
___________           ---------------          -------   
| USER     |          | USER_GROUPS |          |GROUP|
___________           ---------------          -------
|id        |  *-->1   |user_id      |          | id  |
|username  |          |group_id     |  1<--*   |name |
|locale_id |          |   more info |           -------
___________|          |      id     | 
                      ---------------


So nothing irregullar before this point.

Table USER connected with a big set of localization tables thru locale_id and as result we have following view (it's already has all necessarry trigers to insert/delete etc ...) :

Code:
--------------------
|USER_VIEW         |
--------------------
|id                |
|language          |
|username          |
|paswword          |
|    ........      |
|l_first_name      | localizated first name (for ex in EN or JP or RU)
|l_second_name     |
|..................|
--------------------


In this view primary key composite : id + language

Now we have mapping as follow :
User class : (it's mapped to view USER_VIEW)


Code:

@Entity
@Table(name = "USER_VIEW")
@javax.persistence.SequenceGenerator(name = "SEQ_USER", sequenceName = "U_USER_SEQ")

@IdClass(model.ContentId.class)

public class User extends BaseObject implements Serializable, UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_USER")
    @Column(name = "id")
    private Long id;

    @Id
    @Column(name = "LANGUAGE")
    private String language;

    @Column(nullable = false, length = 50, unique = true, name = "username")
    private String username;

    @Column(nullable = false, name = "password")
    private String password;

    @Column(name = "u_first_name")
    private String firstName;

.....................................................

}


Composite key object.

Code:
@Embeddable
public class ContentId extends BaseObject {
    private Long id;
    private String language;


class TimeLimetedRole (actually mapped to table USER_GROUP) represent role assigned to user that can expire



Code:

@Entity
@Table(name = " USER_GROUPS ")
@javax.persistence.SequenceGenerator(name = "SEQ_LINK_USR_PRIV", sequenceName = "SEQ_LINK_USR_PRIV")
public class TimeLimitedRole extends BaseObject implements GrantedAuthority, Comparable<TimeLimitedRole> {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_LINK_USR_PRIV")
    @Column(name = "id")
    private Long id;

    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "user_id", nullable = false, updatable = false)
    private User user;

The main problem actually here . See explanation above..
Code:



    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "group_id", nullable = false, updatable = false)
    private Privilege role;

    @Column(name = "time_life", nullable = false, updatable = true)
    private Date expirationDate;
............
}



and last class looks also very simple (Group)


Code:

@Table(name = "p_privileges")
@Entity
public class Privilege extends BaseObject  {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;
............
}


Problem:

Originally user mapped with table user_group using non-composite PK-FK.

But due transformation with localization primary key for user is : id + lang.

So when I am trying to run - of course i got this exception
Code:
Caused by: org.hibernate.AnnotationException: A Foreign key refering User from TimeLimitedRole has the wrong number of column. should be 2
   at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:276)


So it's expects from me to add language field to user_group table and map it using compisite FK.
Of course it's against business rules and we have the same privilege set for user and it doesn't matter which locale he use right now....

Doe anyboy have idea for solution or workaround that i missed ?


Great thank you !!!.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 1:12 am 
Newbie

Joined: Wed Oct 12, 2005 9:43 am
Posts: 8
I've founded some infor related to simillar problem here : http://opensource.atlassian.com/projects/hibernate/browse/ANN-210... but it's still not resolved yet


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.