Hi all.
I have the following code:
Code:
@Entity
@Table(name = "users", uniqueConstraints = @UniqueConstraint(columnNames = { "username" }))
public class User extends BaseEntity {
@Column(name = "username", length = 50, nullable = false)
private String username;
@Column(name = "password", length = 50, nullable = false)
private String password;
@Column(name = "enabled", columnDefinition = "TINYINT")
private boolean enabled;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private Set<Authority> authorities;
Code:
@Entity
@Table(name = "authorities", uniqueConstraints = @UniqueConstraint(columnNames= {"username", "authority"}))
public class Authority extends BaseEntity {
@ManyToOne(optional = false)
@JoinColumn(name = "username", referencedColumnName = "username", updatable = true)
private User user;
@Column(name = "authority", length = 50, nullable = false)
private String authority;
and
Code:
public static void main(String[] args) {
EntityManagerFactory emf = new HibernateUtils().getEMF();
EntityManager entityManager = (EntityManager) emf.createEntityManager();
entityManager.getTransaction().begin();
User u = new User();
u.setUsername("asd");
u.setPassword("asd");
save(u, entityManager);
User u1 = entityManager.find(User.class, 1l);
u1.setUsername("asda");
save(u1, entityManager);
HibernateUtils.commit(entityManager);
}
private static void save(User entity, EntityManager entityManager) {
Set<Authority> authorities = entity.getAuthorities();
System.out.println(authorities);
if (null != authorities) {
entity.setAuthorities(null);
for (Authority authority : authorities) {
entityManager.remove(authority);
}
}
Authority auth = new Authority();
auth.setUser(entity);
boolean isAdmin = true;
auth.setAuthority(isAdmin ? "ROLE_ADMIN" : "ROLE_USER");
entity.addAuthority(auth);
entity.setEnabled(true);
if (entity.isNew()) {
entityManager.persist(entity);
} else {
entityManager.merge(entity);
}
}
After execution :
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`test`.`authorities`, CONSTRAINT `FK2B0F1321148B353B` FOREIGN KEY (`username`) REFERENCES `users` (`username`))
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1169)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:693)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1404)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1318)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1303)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:72)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
... 31 more
Any ideas why? And how to avoid it?
Thanks for the help.