Hiberante ManyToMany через OneToMany и ManyToOne?
От: unkis  
Дата: 29.05.07 15:51
Оценка:
Пытаюсь запустить этот пример <span class='lineQuote level3'>&gt;&gt;&gt;здесь&lt;&lt;&lt;</span>

Добавил ещё конструкторы и get-setMethods.

дальше делаю так.

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);


в итоге сохраняется всё кроме таблицы UserRole, почему так происходит, и как мне сохранит UserRole ?

и второе, это всё происходит без транзакции, а вот как только я хочу сделать это всё через транзакцию то выскакивает ошибка.


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();



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


Ребята помогите пожалуйста, очень надо.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.