Hibernate在删除之前更新@ManyToOne字段

时间:2016-04-09 15:22:13

标签: java hibernate jpa many-to-one

我在删除实体方面面临一些小问题。 在删除实体之前,hibernate不需要更新它的文件。

这是我要删除的实体:

@Entity(name = "links")
public class Link {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "links_seq")
  @SequenceGenerator(name = "links_seq", sequenceName = "links_seq",      initialValue = 1, allocationSize = 1)
  private Long id;

  @ManyToOne
  @JoinColumn(name="asset1_id")
  private Asset asset1;

  @ManyToOne
  @JoinColumn(name="asset2_id")
  private Asset asset2;
}

这是资产实体:

Entity(name = "assets")
@TypeDefs({@TypeDef(name = "JsonObject", typeClass = AssetData.class)})
public class Asset {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "assets_seq")
  @SequenceGenerator(name = "assets_seq", sequenceName = "assets_seq", initialValue = 1, allocationSize = 1)
  private long id;

  @Column(nullable = false)
  @Type(type = "JsonObject")
  private AssetData data;

  @ManyToOne
  @JoinColumn(name="assettype_id")
  private AssetType assetType;
}

通过LinkRepository删除链接

public interface LinkRepository extends PagingAndSortingRepository<Link, Long> {}

这是控制台日志

Hibernate: select assettype0_.id as id1_0_0_, assettype0_.accessType as  accessTy2_0_0_, assettype0_.assetCategory as assetCat3_0_0_, assettype0_.name as name4_0_0_, attributes1_.asset_types_id as asset_ty1_1_1_, attributet2_.id as attribut2_1_1_, attributet2_.id as id1_3_2_, attributet2_.dataType as dataType2_3_2_, attributet2_.linkabel as linkabel3_3_2_, attributet2_.name as name4_3_2_, attributet2_.required as required5_3_2_ from asset_types assettype0_ left outer join asset_types_attribute_types attributes1_ on assettype0_.id=attributes1_.asset_types_id left outer join attribute_types attributet2_ on attributes1_.attributes_id=attributet2_.id where assettype0_.id=?
Hibernate: update assets set assettype_id=?, data=? where id=?
Hibernate: update assets set assettype_id=?, data=? where id=?
Hibernate: delete from links where id=?

问题是:为什么hibernate更新资产表?外键位于链接表中,命名为asset1_id和asset2_id。这两个更新没有做任何事情。

更新1 删除网络方法

@Override
@Transactional
public void delete(Long linkId) {
  if (linkId==null) {
    throw new IllegalArgumentException("Id is null");
  }
  Link existingLink = linkRepository.findOne(linkId);
  if (existingLink == null) {
    throw new NotFoundException("Link is not present in system");
  }
  linkRepository.delete(existingLink);

}

调用delete后的Stacktrace:

at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:69)
at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:56)
at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:924)
at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:863)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:1179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-1)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:294)
at com.sun.proxy.$Proxy80.remove(Unknown Source:-1)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.delete(SimpleJpaRepository.java:162)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-1)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:483)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:468)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:131)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy85.delete(Unknown Source:-1)
at com.test.levelup.services.impl.LinkServiceImpl.delete(LinkServiceImpl.java:119)

0 个答案:

没有答案