1

多対多である
Entity A → 中間テーブル Z → Entity C

また、1対1である
Entity B → Entity C

という構造を、Spring Data JPAで実装したとき、

Entity A

@Entity
@Table(name="a")
public class A {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id", nullable=false)
    private long id;

    @Column(name="unique_id", nullable=false)
    private String uniqueId;

    @OneToMany(fetch=FetchType.EAGER, mappedBy="a", targetEntity=Z.class)
    private List<Z> z;

    getter and setter
}

Entity B

@Entity
@Table(name="b")
public class B {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id", nullable=false)
    private long id;

    @Column(name="c_unique_id", nullable=false)
    private String cUniqueId;

    @OneToOne
    @JoinColumn(name="c_unique_id", referencedColumnName="unique_id", insertable=false, updatable=false)
    private C c;

    getter and setter
}

Entity Z

@Entity
@Table(name="z")
public class Z {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id", nullable=false)
    private long id;

    @Column(name="a_unique_id", nullable=false)
    private String aUniqueId;

    @Column(name="c_unique_id", nullable=false)
    private String cUniqueId;

    @ManyToOne
    @JoinColumn(name="a_unique_id", referencedColumnName="unique_id", insertable=false, updatable=false)
    private A a;

    @ManyToOne
    @JoinColumn(name="c_unique_id", referencedColumnName="unique_id", insertable=false, updatable=false)
    private C c;

    getter and setter
}

Entity C

@Entity
@Table(name="c")
public class C {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id", nullable=false)
    private long id;

    @Column(name="unique_id", nullable=false)
    private String uniqueId;

    @OneToMany(fetch=FetchType.EAGER, mappedBy="c", targetEntity=Z.class)
    private List<Z> z;

    @OneToOne(fetch=FetchType.EAGER, mappedBy="c", targetEntity=B.class)
    private B b;
}

となりますが、
A,Bのselect時は問題ないのですが、Cをセレクトしたときにエラーが発生します。

@Query("select c from C c where c.deleteTs is not null")
List<C> findDeleted(Pageable pageable);
java.lang.IllegalArgumentException: Can not set java.lang.String field package.c.uniqueId to java.lang.Long
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(Unknown Source)
    at java.lang.reflect.Field.get(Unknown Source)
    at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:57)
    at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:76)
    at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValues(AbstractComponentTuplizer.java:82)
    at org.hibernate.tuple.component.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:107)
    at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:435)
    at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:259)
    at org.hibernate.engine.spi.EntityUniqueKey.generateHashCode(EntityUniqueKey.java:84)
    at org.hibernate.engine.spi.EntityUniqueKey.(EntityUniqueKey.java:65)
    at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:712)
    at org.hibernate.type.EntityType.resolve(EntityType.java:492)
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170)
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1114)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
    at org.hibernate.loader.Loader.doQuery(Loader.java:920)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2551)
    at org.hibernate.loader.Loader.doList(Loader.java:2537)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2367)
    at org.hibernate.loader.Loader.list(Loader.java:2362)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:229)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1260)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:364)
    at com.sun.proxy.$Proxy163.getResultList(Unknown Source)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:114)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:78)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:100)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:91)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:413)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:391)
    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$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)
    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:207)
    at com.sun.proxy.$Proxy57.findByUserIdAndDeleted(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
    at com.sun.proxy.$Proxy58.findByUserIdAndDeleted(Unknown Source)
    at package.ArticleController.findC(ArticleController.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.multipart.support.MultipartFilter.doFilterInternal(MultipartFilter.java:118)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

unique_idフィールドにLongを入れようとしているようですがなぜでしょうか?

なお、Repositoryクラスの肥大化を懸念し、@Query()アノテーションでの結合を避けたく、
また、N+1問題の回避のため、遅延fetchも考えていません。

2 件の回答 2

1

JPAアノテーション的には、エンティティC

@OneToOne(fetch=FetchType.EAGER, mappedBy="c", targetEntity=B.class)
private B b;

を利用してエンティティBをfetchする際は、Bエンティティがフィールドc(に対応するカラムc_unique_id)に持っているCPK(@Idアノテーションが付いているフィールド)をキーとして利用しようとします。
イメージとしては次のようなSQLでしょうか。

select * from B b inner join
(select * from C where C.deleteTs is not null) c
on ( b.c_unique_id = c.id);

c.idLongなのにb.c_unique_idはそうじゃない、というエラーですね。

以下にも同様の質問がありました。


今回の場合なのですが、Cidが不要ですね。少なくとも@Idを付与する意味が無いです。
uniqueIdが名前の通りユニークでかつnullable=falseなのであれば、こちらに@Idを付けることで今回のクエリはJPA的には想定通り実行されると思います。

(ただ実際には、idフィールドを用意して@Idを付けているのはそれなりの理由があると思いますので、そちらとの兼ね合いも検討する必要があるかと。)

0

回答は yukihane さんのものが的を得ていたので承認させていただきましたが、
一点、別解があったようなので記載させていただきます。

通常、select A の際、getterでCを取得、
また、select C の際、getterでAを取得するシチュエーションがほとんどかと思いますが、
単方向結合のみでよい場合、結合される側に@OneToOneなどのディメンションリレーションシップアノテーションおよび、@JoinColumnは不要なようです。

以下、質問内容の定義での例です。

Cをセレクト時はZ及びBの結合はしないものとします。

Class A

@Entity
@Table(name="a")
public class A {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id", nullable=false)
    private long id;

    @Column(name="unique_id", nullable=false)
    private String uniqueId;

    @OneToMany(fetch=FetchType.EAGER, mappedBy="a", targetEntity=Z.class)
    private List<Z> z;

    getter and setter
}

Class Z

@Entity
@Table(name="z")
public class Z {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id", nullable=false)
    private long id;

    @Column(name="a_unique_id", nullable=false)
    private String aUniqueId;

    @Column(name="c_unique_id", nullable=false)
    private String cUniqueId;

    @ManyToOne
    @JoinColumn(name="a_unique_id", referencedColumnName="unique_id", insertable=false, updatable=false)
    private A a;

    @ManyToOne
    @JoinColumn(name="c_unique_id", referencedColumnName="unique_id", insertable=false, updatable=false)
    private C c;

    getter and setter
}

CLass C

@Entity
@Table(name="c")
public class C {

    private long id;
    private String uniqueId;
    private List<Z> z;
    private B b;
}

Class B

@Entity
@Table(name="b")
public class B {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="id", nullable=false)
    private long id;

    @Column(name="c_unique_id", nullable=false)
    private String cUniqueId;

    @OneToOne
    @JoinColumn(name="c_unique_id", referencedColumnName="unique_id", insertable=false, updatable=false)
    private C c;

    getter and setter
}

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。