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 + languageNow 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 !!!.