Joined: Wed May 30, 2007 6:47 am Posts: 5
|
Ich versuche drei Objecte in DB zu Speichern, abe ich krige immer Exception. Was machi ich falsch?
Hibernate version: 3.3
Mapping documents:
Code: @Entity @Table(name="USER") public class User {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id;
@Column(name="USERNAME") private String username;
@Column(name="PASSWORD") private String password;
@OneToMany(mappedBy="user") private Set<UserRole> userRoles;
}
@Table(name="ROLE") @Entity public class Role {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id;
@Column(name="NAME") private String name;
@OneToMany(mappedBy="role") private Set<UserRole> userRoles; }
@Entity @Table(name="USER_ROLE") @IdClass(UserRolePK.class) public class UserRole {
@Id @Column(name="USER_ID",insertable=false, updatable=false) private Long userId;
@Id @Column(name="ROLE_ID",insertable=false, updatable=false) private Long roleId;
@ManyToOne @JoinColumn(name="USER_ID") private User user;
@ManyToOne @JoinColumn(name="ROLE_ID") private Role role;
@Column(name="ENABLED") private boolean enabled; }
public class UserRolePK {
private Long userId; private Long roleId; } Code between sessionFactory.openSession() and session.close():Code: emf2 = Persistence.createEntityManagerFactory("hacon_w"); entityManager = (Session) emf2.createEntityManager().getDelegate(); tx2 = entityManager.getTransaction(); tx2.begin(); User u = new User(); u.setUsername("a"); u.setPassword("a"); Role r = new Role(); r.setName("b"); UserRole ur = new UserRole(u,r,true); entityManager.save(u); entityManager.save(r); entityManager.save(ur); tx2.commit(); entityManager.close();
Full stack trace of any exception that occurs:17:45:03,388 DEBUG AbstractFlushingEventListener:154 - dirty checking collections 17:45:03,388 DEBUG AbstractFlushingEventListener:171 - Flushing entities and processing referenced collections 17:45:03,390 DEBUG AbstractFlushingEventListener:210 - Processing unreferenced collections 17:45:03,390 DEBUG AbstractFlushingEventListener:224 - Scheduling collection removes/(re)creates/updates 17:45:03,390 DEBUG AbstractFlushingEventListener:85 - Flushed: 1 insertions, 0 updates, 0 deletions to 3 objects 17:45:03,390 DEBUG AbstractFlushingEventListener:91 - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections 17:45:03,391 DEBUG Printer:83 - listing entities: 17:45:03,391 DEBUG Printer:90 - de.hacon.warehouse.Role{id=1, userRoles=null, name=b} 17:45:03,391 DEBUG Printer:90 - de.hacon.warehouse.User{id=1, username=a, userRoles=null, password=a} 17:45:03,391 DEBUG Printer:90 - de.hacon.warehouse.UserRole{enabled=true, role=de.hacon.warehouse.Role#1, user=de.hacon.warehouse.User#1} 17:45:03,391 DEBUG AbstractFlushingEventListener:290 - executing flush 17:45:03,392 DEBUG ConnectionManager:467 - registering flush begin 17:45:03,392 DEBUG AbstractEntityPersister:2200 - Inserting entity: [de.hacon.warehouse.UserRole#component[userId,roleId]{userId=null, roleId=null}] 17:45:03,395 DEBUG AbstractBatcher:358 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 17:45:03,395 DEBUG SQL:393 - insert into USER_ROLE (USER_ID, ROLE_ID, ENABLED, userId, roleId) values (?, ?, ?, ?, ?) Hibernate: insert into USER_ROLE (USER_ID, ROLE_ID, ENABLED, userId, roleId) values (?, ?, ?, ?, ?) 17:45:03,395 DEBUG AbstractBatcher:476 - preparing statement 17:45:03,396 DEBUG AbstractEntityPersister:1988 - Dehydrating entity: [de.hacon.warehouse.UserRole#component[userId,roleId]{userId=null, roleId=null}] 17:45:03,397 DEBUG AbstractBatcher:44 - Executing batch size: 1 17:45:03,398 DEBUG AbstractBatcher:366 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 17:45:03,398 DEBUG AbstractBatcher:525 - closing statement 17:45:03,399 DEBUG JDBCExceptionReporter:69 - Could not execute JDBC batch update [insert into USER_ROLE (USER_ID, ROLE_ID, ENABLED, userId, roleId) values (?, ?, ?, ?, ?)] java.sql.BatchUpdateException: Column 'userId' cannot be null at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at de.hacon.contoller.Controller.proba(Controller.java:584) at de.hacon.main.ParserStart.main(ParserStart.java:23) 17:45:03,400 WARN JDBCExceptionReporter:77 - SQL Error: 1048, SQLState: 23000 17:45:03,400 ERROR JDBCExceptionReporter:78 - Column 'userId' cannot be null 17:45:03,400 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at de.hacon.contoller.Controller.proba(Controller.java:584) at de.hacon.main.ParserStart.main(ParserStart.java:23) Caused by: java.sql.BatchUpdateException: Column 'userId' cannot be null at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242) ... 9 more Exception in thread "main" 17:45:03,402 DEBUG ConnectionManager:476 - registering flush end org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at de.hacon.contoller.Controller.proba(Controller.java:584) at de.hacon.main.ParserStart.main(ParserStart.java:23) Caused by: java.sql.BatchUpdateException: Column 'userId' cannot be null at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242) ... 9 more Name and version of the database you are using:MySQL 5 The generated SQL (show_sql=true):[list=] CREATE TABLE USER ( ID BIGINT AUTO_INCREMENT NOT NULL, USERNAME VARCHAR(255), PASSWORD VARCHAR(255), PRIMARY KEY (ID)) CREATE TABLE ROLE ( ID BIGINT AUTO_INCREMENT NOT NULL, NAME VARCHAR(255), PRIMARY KEY (ID)) CREATE TABLE USER_ROLE ( USER_ID BIGINT NOT NULL, ROLE_ID BIGINT NOT NULL, PRIMARY KEY (USER_ID, ROLE_ID), ENABLED TINYINT(1) default 0) ALTER TABLE USER_ROLE ADD CONSTRAINT FK_USER_ROLE_USER_ID FOREIGN KEY (USER_ID) REFERENCES USER (ID) ALTER TABLE USER_ROLE ADD CONSTRAINT FK_USER_ROLE_ROLE_ID FOREIGN KEY (ROLE_ID) REFERENCES ROLE (ID)[/list]
|
|