如何在没有额外存储库的情况下级联修改@onetomany

时间:2014-03-27 10:28:49

标签: java spring hibernate jpa spring-data-jpa

我正在使用Spring3.1和Spring-data-jpa:1.4.3

我有以下父实体:

@Table(name = "users", schema = "", catalog = "cis")
@Entity
public class UserEntity {
...
  @OneToMany(fetch = FetchType.EAGER, mappedBy = "userEntity", cascade = CascadeType.ALL)
    public Set<UserPermissionsEntity> getUserPermissionEntities() {
        return userPermissionEntities;
    }

    public void setUserPermissionEntities(Set<UserPermissionsEntity> userPermissionEntities) {
        this.userPermissionEntities = userPermissionEntities;
    }
...
}

和孩子:

@javax.persistence.Table(name = "user_permissions", schema = "", catalog = "cis")
@Entity
public class UserPermissionsEntity {
..
   @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id")
    @JsonIgnore
    public UserEntity getUserEntity() {
        return userEntity;
    }

    public void setUserEntity(UserEntity userEntity) {
        this.userEntity = userEntity;
    }
..}

现在我尝试通过UserEntity修改用户权限:

   @Override
    public void modifyInternalUserEntity(UserEntity modifiedUserEntity) {
        UserEntity oldUserEntity = usersRepository.findByUsername(modifiedUserEntity.getUsername());
        oldUserEntity.setUserPermissionEntities(modifiedUserEntity.getUserPermissionEntities());
    }

我收到此错误:

Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing: com.cellebrite.cismanager.model.UserPermissionsEntity.userEntity -> com.cellebrite.cismanager.model.UserEntity
    at org.hibernate.engine.spi.CascadingAction$8.noCascade(CascadingAction.java:381)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:176)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:423)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:264)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:136)
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
    at org.hibernate.internal.SessionImpl.firePersistOnFlush(SessionImpl.java:783)
    at org.hibernate.internal.SessionImpl.persistOnFlush(SessionImpl.java:776)
    at org.hibernate.engine.spi.CascadingAction$8.cascade(CascadingAction.java:347)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:409)
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:350)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:326)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
    at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:160)
    at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:151)
    at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:88)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1127)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:325)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73)
    ... 74 more

知道为什么吗?

感谢, 射线。

0 个答案:

没有答案