-->
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: [resolved]Hard Mapping between 4 tables
PostPosted: Wed Sep 30, 2009 4:52 am 
Beginner
Beginner

Joined: Mon Sep 26, 2005 8:22 am
Posts: 24
Hello everyone,

Hibernate core : 3.3.0.SP1
Hibernate Annotations : 3.4.0.GA

I have a nice (sic) app to rewrite from PHP to Java, and I can't modify the DB schema. Il would be ok if the schema was ok. Therefor my issues:
Here is an abstract of the schema:
Code:
                          [Role]
                              ^
                              |
                              |
                              |
                              V
[User]<--------------->[UserEntity]<--------------->[Entity]
                              (char (1) mail)


I then have a join table between the three others, so I know the role of a user on that entity. But the table also tells me if the user might receive a mail on certain circumstances.

Then I have 4 fields in Dans la table UserEntity, three beeing part of the key.

Hibernate code generation gave me a object with a complex id :
Code:
@Entity
public class UserEntity implements java.io.Serializable {

   /**
    *
    */
   private static final long serialVersionUID = 9105783703237347058L;

   @EmbeddedId
   private UserEntityId id;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "FK_ENTITY", nullable = false, insertable = false, updatable = false)
   private Entity entity;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "FK_IDENT", nullable = false, insertable = false, updatable = false)
   private User user;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "FK_ROLE", nullable = false, insertable = false, updatable = false)
   private Role role;

   @Column(name = "F_MAIL", nullable = false, precision = 1, scale = 0)
   private boolean mail;
}

@Embeddable
public class UserEntityId implements java.io.Serializable {

   /**
    *
    */
   private static final long serialVersionUID = 3065532416096191064L;

   @ManyToOne(fetch = FetchType.EAGER,optional=false)
   @JoinColumn(name = "FK_IDENT", insertable=false, updatable=false, nullable=false)
   private User user;


   @ManyToOne(fetch = FetchType.EAGER,optional=false)
   @JoinColumn(name = "FK_ENTITY", insertable=false, updatable=false, nullable=false)
   private Entity entity;

   @ManyToOne(fetch = FetchType.EAGER,optional=false)
   @JoinColumn(name = "FK_ROLE", insertable=false, updatable=false, nullable=false, unique=false)
   private Role role;
}


Here goes Entity mapping:
Code:
@Entity
public class Entity {

   @OneToMany(fetch = FetchType.LAZY)
   @JoinColumn(name="FK_ENTITY")
   @SQLDeleteAll(sql="DELETE FROM UserEntity WHERE fk_entity = ?", check = ResultCheckStyle.COUNT)
   @SQLInsert(sql="INSERT INTO UserEntity (FK_entity, FK_IDENT, FK_ROLE, F_MAIL) VALUES (?, ?, ?, ?)", check = ResultCheckStyle.COUNT)
//   @SQLDelete(sql="DELETE FROM UserEntity WHERE fk_entity = ? AND FK_IDENT = ? AND fk_entity = ? AND FK_ROLE = ?", check = ResultCheckStyle.COUNT)
//   @SQLUpdate(sql="UPDATE UserEntity SET FK_entity = ?, FK_IDENT = ?, FK_ROLE = ?, F_MAIL = ? WHERE FK_entity = ?, FK_IDENT = ?, FK_ROLE = ?", check = ResultCheckStyle.COUNT)
   private Set<UserEntity> userEntities = new HashSet<UserEntity>(0);
}


With that, if I try to update userEntities list in an entity, hibernate generate that SQL:
Code:
update userEntity set FK_entity=null where FK_entity=? and FK_IDENT=? and FK_entity=? and FK_ROLE=?

with
Code:
2009-09-30 10:03:18 TRACE [org.hibernate.type.IntegerType nullSafeSet(l:151)] binding '140381' to parameter: 1
2009-09-30 10:03:18 TRACE [org.hibernate.type.StringType nullSafeSet(l:151)] binding 'IFBU6444' to parameter: 2
2009-09-30 10:03:18 TRACE [org.hibernate.type.IntegerType nullSafeSet(l:151)] binding '140381' to parameter: 3
2009-09-30 10:03:18 TRACE [org.hibernate.type.IntegerType nullSafeSet(l:151)] binding '1' to parameter: 4


So:
Why does it try to set NULL, when I have a true value to set?
Wht do I have entity twice in the query?

The mapping sucks, there's no point arguing. Feel free to help me improve it.


Last edited by brisssou on Wed Sep 30, 2009 9:39 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Hard Mapping between 4 tables
PostPosted: Wed Sep 30, 2009 9:36 am 
Beginner
Beginner

Joined: Mon Sep 26, 2005 8:22 am
Posts: 24
The answer:
the mapping really DO suck.

That one do work, but can't say it's better:
Code:
@Entity
@Table(name = "user_entity")
@IdClass(entityAdministratorId.class)
public class UserEntity implements java.io.Serializable {


   @Id
   @Column(name = "FK_ENTITY")
   private Integer entityId;
   @Id
   @Column(name = "FK_IDENT")
   private String userId;
   @Id
   @Column(name = "FK_ROLE")
   private Integer roleId;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "FK_entity", nullable = false, insertable = false, updatable = false)
   private Entity entity;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "FK_IDENT", nullable = false, insertable = false, updatable = false)
   private User user;

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "FK_ROLE", nullable = false, insertable = false, updatable = false)
   private Role role;

   @Column(name = "F_MAIL", nullable = false, precision = 1, scale = 0)
   private boolean mail;
}


@Embeddable
public class UserEntityId implements java.io.Serializable {

   @Column(name = "FK_entity")
   private Integer poolId;

   @Column(name = "FK_IDENT")
   private String userId;

   @Column(name = "FK_ROLE")
   private Integer roleId;
}

@Entity
public class Pool {
   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @Cascade(org.hibernate.annotations.CascadeType.ALL)
   @JoinColumn(name="FK_entity")
   @SQLDelete(sql="DELETE FROM USER_entity WHERE fk_entity = ? AND FK_IDENT = ? AND fk_pool = ? AND FK_ROLE = ?", check = ResultCheckStyle.COUNT)
   private Set<UserEntity> poolAdministrators = new HashSet<UserEntity>(0);
}


et voilĂ !


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.