2017-02-18 21 views
0

У меня есть два домена пользователя и полномочия. Эти два домена имеют одностороннее отношение One to Many, используя третью таблицу.Объект Hibernate ссылается на несохраненный экземпляр переходного процесса с отношением один к другому

пользователя Домен:

@Entity 
@Table(name = "USER") 
@ToString 
@EqualsAndHashCode(callSuper=false) 
@Data 
public class User extends AbstractEntity implements UserDetails { 

    private static final long serialVersionUID = 2507177602107639240L; 

    @OneToMany(fetch = FetchType.EAGER) 
    @JoinTable 
    List<Authority> authorities; 

    @Column(nullable = false) 
    String password; 

    @Column(nullable = false, unique = true) 
    String username; 

    boolean accountNonExpired = true; 

    boolean accountNonLocked = true; 

    boolean credentialsNonExpired = true; 

    boolean enabled = false; 

    @OneToOne(cascade = CascadeType.ALL) 
    UserDetail userDetail; 

    public User(String username, String password) { 
     this.username = username; 
     this.password = password; 
    } 

    public User() { 

    } 

} 

Authority Домен:

@Entity 
@EqualsAndHashCode(callSuper=false) 
@ToString 
@Data 
public class Authority extends AbstractEntity implements GrantedAuthority { 

    private static final long serialVersionUID = -3506805573570762491L; 

    @Setter(AccessLevel.NONE) 
    @Column(unique = true, nullable = false) 
    String authority; 

    @SuppressWarnings("unused") 
    private Authority(){} 

    public Authority(String authority) { 
     this.authority = authority; 
    } 

    public Authority(Role role) { 
     this(role.toString()); 
    } 
} 

На старте приложения я упорствовал все имеющиеся власти в дб.

mysql> select * from authority; 
+----+------------+--------------+--------------+------------+------------------+ 
| id | created_by | date_created | last_updated | updated_by | authority  | 
+----+------------+--------------+--------------+------------+------------------+ 
| 1 | NULL  | NULL   | NULL   | NULL  | ROLE_CUSTOMER | 
| 2 | NULL  | NULL   | NULL   | NULL  | ROLE_ADMIN_READ | 
| 3 | NULL  | NULL   | NULL   | NULL  | ROLE_ADMIN_WRITE | 
+----+------------+--------------+--------------+------------+------------------+ 
3 rows in set (0.00 sec) 

Теперь я пытаюсь сохранить пользователя с полномочиями.

@Transactional 
private void createAdminUser(){ 
    String username = "admin"; 
    User user = userRepository.findByUsername(username); 
    if(user == null){ 
     List<Authority> authorities = authorityRepository.findAllByAuthority(Role.ROLE_ADMIN_WRITE.toString()); 
     user = new User(username, "admin"); 
     user.setAuthorities(authorities); 
     userRepository.save(user); 
    } 
} 

Теперь я получаю исключение, так как полномочия не сохраняются.

Журналы ошибок:

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.legacybuy.model.Authority; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.legacybuy.model.Authority 
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.11.0.RELEASE.jar:na] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.0.RELEASE.jar:na] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at com.sun.proxy.$Proxy99.save(Unknown Source) ~[na:na] 
    at com.legacybuy.config.BootStrap.createAdminUser(BootStrap.java:79) ~[classes/:na] 
    at com.legacybuy.config.BootStrap.onApplicationEvent(BootStrap.java:38) ~[classes/:na] 
    at com.legacybuy.config.BootStrap.onApplicationEvent(BootStrap.java:1) ~[classes/:na] 
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337) ~[spring-context-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.boot.context.event.EventPublishingRunListener.finished(EventPublishingRunListener.java:100) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.SpringApplicationRunListeners.callFinishedListener(SpringApplicationRunListeners.java:79) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.SpringApplicationRunListeners.finished(SpringApplicationRunListeners.java:72) ~[spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
    at com.legacybuy.Application.main(Application.java:12) [classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92] 
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92] 
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.1.RELEASE.jar:1.5.1.RELEASE] 
Caused by: java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.legacybuy.model.Authority 
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1689) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.jpa.internal.EntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(EntityManagerImpl.java:235) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2967) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2339) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:147) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:61) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final] 
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517) ~[spring-orm-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    ... 35 common frames omitted 
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.legacybuy.model.Authority 
    at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:279) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:462) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:144) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:894) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1313) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:50) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:582) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:456) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:465) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2963) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final] 
    ... 43 common frames omitted 
+0

Я смог воспроизвести ошибку. См. Мой ответ ниже. – raminr

+0

Невозможно найти отношение n-m в коде. –

+0

@PeterRader Я думаю, что это \ @OneToMany (fetch = FetchType.EAGER) \ @JoinTable Список власти; –

ответ

0

Это, вероятно, потому, что ваш findByAuthority вызова участвует в его собственной транзакции, возвращая отдельностоящий/переходную Authority экземпляра и вы позже делегирование в хранилище вашего пользователя и он терпит неудачу, потому что Authority является переходным.

Лучший способ справиться с этим - убедиться, что граница транзакции происходит раньше в стеке вызовов метода, так что обе операции репозитория происходят в одном и том же контексте транзакции.

@Transactional 
public void createAdminUser(String userName) { 
    // your logic here 
} 

Другой взломать решение здесь, в этом случае было бы изменить отображение для List<Authority> собственности так, чтобы он каскады операции от User к Authority.

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
@JoinTable 
private List<Authority> authorities; 

Очевидно, что лучший подход, чтобы убедиться, что все хранилища вызовов участвовать в более широких рамках сделки, так что ни один субъект не отрывается как часть бизнес-прецедента.

+0

Я добавил @Transactional annototion, но он не работает. –

+0

cascade = CascadeType.ALL не работает для меня, поскольку ROLE_ADMIN_WRITE уже существует. –

0

Я полагаю, что в вашем User constructor вы вызываете AuthorityUtil и он возвращает список отдельных элементов управления.

public User(String username, String password, Authority authority) { 
    this.username = username; 
    this.password = password; 
    this.authorities = AuthorityUtil.getAsList(authority); 
} 

Убедитесь, что метод getAsList не создает новые классы полномочий.

+0

Я удалил AuthorityUtil, но это все еще не работает. Посмотрите обновленный один метод createAdminUser(). –

+0

В этом случае я согласен с @Naros. Это связано с тем, что объекты Органа получаются на другом сеансе, то есть на отдельных объектах. Просто потому, что вы комментируете метод службы с помощью Transactional, не означает, что вы получаете один и тот же сеанс для каждого вызова, если только контекст контекста сеанса не является JTA. Какова область вашей сессии? Вы устанавливаете hibernate.current_session_context_class? Вы используете JTA? – raminr