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.