简体   繁体   中英

Many-to-Many mapping Hibernate getting ConstraintViolationException

This is the concerned Java code

Role.Java

@Entity
@Table(name = "role")
public class Role extends BasicModel {


private static final long serialVersionUID = 1L;

@Id
@Column(name = "id")
private int id;

@Column(name = "role_name")
private String roleName;

@OneToMany(mappedBy = "pk.role", cascade=CascadeType.ALL)
private Set<UserRoles> userRoles = new HashSet<UserRoles>();
//Getters and Setters
}

User.Java

@Entity
@Table(name = "user")
public class User extends BasicModel{

@Id
@Column(name = "id")
private int id;

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

@Column(name = "email")
private String email;

@Column(name = "password")
private String password;

@OneToMany(mappedBy = "pk.user", cascade=CascadeType.ALL)
private Set<UserRoles> userRoles = new HashSet<UserRoles>();
//getters and setters
}

UserRoles.Java

@Entity
@Table(name = "user_roles")
@AssociationOverrides({
@AssociationOverride(name = "pk.user", 
    joinColumns = @JoinColumn(name = "user_id")),
@AssociationOverride(name = "pk.role", 
    joinColumns = @JoinColumn(name = "role_id")) })
public class UserRoles extends BasicModel {


private static final long serialVersionUID = 1L;

@Id
@Column(name = "id")
private int id;

@EmbeddedId
private UserRoleId pk = new UserRoleId();
//Getters and Setters
}

**UserRoleId.java**
public class UserRoleId {

@ManyToOne
private User user;

@ManyToOne
private Role role;
//Getters and Setters
}

This is the code I am using to insert

tx = session.beginTransaction();
User user = new User();

user.setName("admin");
user.setPassword("admin");
user.setEmail("admin@admin.com");

Role role = new Role();

role.setRoleName("admin");

session.save(role);

UserRoles userRole = new UserRoles();
userRole.setUser(user);
userRole.setRole(role);

user.getUserRoles().add(userRole);

session.save(user);

tx.commit();

In Role and User class, if I set the cascadetype as ALL like

@OneToMany(mappedBy = "pk.role", cascade=CascadeType.ALL)
private Set<UserRoles> userRoles = new HashSet<UserRoles>();

I am getting the following error

org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:72)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
at com.testworks.test.dao.impl.UserDAOImpl.insertUser(UserDAOImpl.java:68)
at com.testworks.test.dao.impl.UserDAOImpl.main(UserDAOImpl.java:82)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: 

Cannot add or update a child row: a foreign key constraint fails (`test`.`user_roles`, CONSTRAINT `FK_user_roles_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
... 14 more

If I set the CascadeType to other values, user table and role table gets updated and link table user_roles not getting updated.

This is the table structure in mysql

role

DROP TABLE IF EXISTS `test`.`role`;
CREATE TABLE  `test`.`role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`role_name` varchar(100) DEFAULT NULL,
`created_user` varchar(45) NOT NULL,
`created_date` datetime NOT NULL,
`updated_user` varchar(45) NOT NULL,
`updated_date` datetime NOT NULL,
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

user

DROP TABLE IF EXISTS `test`.`user`;
CREATE TABLE  `test`.`user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`password` varchar(64) DEFAULT NULL,
`created_user` varchar(45) NOT NULL,
`created_date` datetime NOT NULL,
`updated_user` varchar(45) NOT NULL,
`updated_date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

user_roles

DROP TABLE IF EXISTS `test`.`user_roles`;
CREATE TABLE  `test`.`user_roles` (
`id` int(10) NOT NULL,
`role_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`created_user` varchar(45) NOT NULL,
`created_date` datetime NOT NULL,
`updated_user` varchar(45) NOT NULL,
`updated_date` datetime NOT NULL,
 PRIMARY KEY (`id`),
 KEY `FK_user_roles_user` (`user_id`),
 KEY `FK_user_roles_roles` (`role_id`),
 CONSTRAINT `FK_user_roles_roles` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`),
 CONSTRAINT `FK_user_roles_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Please help.

I went through this link

http://developmentality.wordpress.com/2011/05/23/hibernate-mysql-mac-foreign-key-nightmares-a-painless-solution-to-a-painful-problem/

and executed this command

show engine innodb status

in mysql command line and found, the query generated like this

insert into user_roles (created_date, created_user, updated_date, updated_user, role_id, user_id, id) values ('2014-11-01 11:57:03', 'test', '2014-11-01 11:57:03', 'test', 0, 0, 0);

which is because

the value of user_id and role_id is not getting incremented.

Now I added this line

@GeneratedValue(strategy = GenerationType.IDENTITY)

to auto generate id in both user and role tables and the issue is now resolved.

Thanks !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM