1

私はSpring Data JPAとSpring Data RESTでSpring Bootを使用しています。双方向SpringデータJPAとSpring RESTの@ManyToManyと@OneToOneの関係を正しく設定できません。

私はTestExectionというクラスを持っていますが、これは複数のTestResultsを持つクラス設定です。 TestExecutionが作成されると、それに設定される結果は作成されません。

後でTestExecutionを実行すると、TestResultオブジェクトが作成されます。

これらのTestResultオブジェクトは、TestExecutionと@OneToOneの関係を持ち、TestResultを保存する前にTestExecutionオブジェクトがTestResultに設定され、save(TestResult)が呼び出されます。

// Create RESULT object 
    TestResult testResult = new TestResult(someTestExection......); 
    save(testResult) 

私はtestResults/1/testExecutionを見てREST経由で電話をかける場合は、私がtestResultに関連付けられているtestExecutionを見ることができます。私がtestExecutions/1/testResultsに対して同じ呼び出しを行うと、空の[]が返されます。

2質問:

1)私はexpclicitly TestExecutionsのTestResultセットに新しく作成されたTestResultオブジェクトを設定する必要がありますか?

testExecution.getTestResults().add(testResult); 
    save(testExecution) 
  • 私はそれが付属スタックトレースが発生し、これをしようとします。

    Hibernate: update test_execution set description=?, owner=?, version=? where id=? and version=? 
    [WARNING] 
    java.lang.reflect.InvocationTargetException 
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
        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.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478) 
        at java.lang.Thread.run(Thread.java:745) 
    Caused by: java.lang.StackOverflowError 
        at com.miw.mcb.server.model.TestExecution.hashCode(TestExecution.java:32) 
        at com.miw.mcb.server.model.TestResult.hashCode(TestResult.java:30) 
        at java.util.AbstractSet.hashCode(AbstractSet.java:126) 
        at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:448) 
        at com.miw.mcb.server.model.TestExecution.hashCode(TestExecution.java:32) 
        at com.miw.mcb.server.model.TestResult.hashCode(TestResult.java:30) 
        at java.util.AbstractSet.hashCode(AbstractSet.java:126) 
        at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:448) 
    

OR

2)TestResultはTestExecutionを設定したときに、それがリンクされ、そのリストに追加することができるように関係を持つことのセットアップを持っているいくつかの方法がありますか?

TestExecution

@Data 
    @Entity 
    @Table(name = "test_execution") 
    //@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") 
    public class TestExecution { 

     private @Id @GeneratedValue Long id; 

     // TODO set back to lazy 
     @ManyToMany(fetch = FetchType.EAGER) 
     //@JsonBackReference 
     private Set<TestResult> testResults; 

     // TODO set back to lazy 
     @ManyToMany(fetch = FetchType.EAGER) 
     private Set<TestSuite> testSuites; 

TestResult

@Data 
    @Entity 
    @Table(name = "test_result") 
    //@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") 
    public class TestResult { 

     private @Id @GeneratedValue Long id; 

     // TODO switch back to lazy 
     @OneToOne(fetch = FetchType.EAGER) 
     //@JsonManagedReference 
     private TestExecution testExecution; 

     // TODO switch back to lazy 
     @OneToOne(fetch = FetchType.EAGER) 
     private TestSuite testSuite; 

両方のリポジトリが設定されている同じ

TestExecutionRepository

@Repository 
@Transactional 
public interface TestExecutionRepository extends PagingAndSortingRepository<TestExecution, Long> { 

TestResultRepository

@Repository 
@Transactional 
public interface TestResultRepository extends PagingAndSortingRepository<TestResult, Long> { 

のAppConfig

@Configuration 
@ComponentScan({ "com.miw.mcb.server.dao.repository", "com.miw.mcb.server.model" }) 
public class AppConfig { 

} 

SpringBootApp(ReactAndSpringDataRestApplication)

@SpringBootApplication 
public class ReactAndSpringDataRestApplication { 

public static void main(String[] args) { 
    SpringApplication.run(ReactAndSpringDataRestApplication.class, args); 
} 
} 

UPDATE:私は@ManyToOneと@OneToManyの更新をしたが、TestResultsがあるかの問題がまだあります伝播先TestExecution

ManyToOneとOneToManyの説明をありがとうございます。私はマッピングが正しくないと思った。

実行しているときに、私は次のスタックトレースを得た。その後:result.setTestExecution(実行)用の更新**

testExecution.getTestResults().add(testResult); 
save(testExecution) 

。 execute.getResults()。add(result); repo.save(実行); **私はロンボク島でのテスト結果のためhasCode()関数を実装する必要がありました。この問題を解決するにはresult最初**

java.lang.StackOverflowError: null 
    at com.miw.mcb.server.model.TestExecution.hashCode(TestExecution.java:26) 
    at com.miw.mcb.server.model.TestResult.hashCode(TestResult.java:35) 
    at java.util.AbstractSet.hashCode(AbstractSet.java:126) 
    at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:448) 
    at com.miw.mcb.server.model.TestExecution.hashCode(TestExecution.java:26) 
    at com.miw.mcb.server.model.TestResult.hashCode(TestResult.java:35) 
    at java.util.AbstractSet.hashCode(AbstractSet.java:126) 
    at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:448) 
    at com.miw.mcb.server.model.TestExecution.hashCode(TestExecution.java:26) 
    at com.miw.mcb.server.model.TestResult.hashCode(TestResult.java:35) 
    at java.util.AbstractSet.hashCode(AbstractSet.java:126)... 

**

@EqualsAndHashCode(exclude={"id", "testExecution", "device", "testCase"}) 
+0

。持続するときは、要件に応じてカスケードを設定する必要があります。 – Vaelyr

答えて

2

を保存せずに、私はあなたが間違った選択を作っていると思いますJPAアノテーションさらに、私はこれがどのように機能するか理解していません。実際に

@ManyToMany関係は、エンティティのすべての要素が他のエンティティから誰に関連することができる関係の種類を表します。 thatの中間テーブルが必要です。これは、単一のTestResultが多くの実行とリンクできることを意味します。

もう一方の側では、@OneToOneマッピングを提供しています。これは逆の方法で、このオブジェクトの反対側から1つのエンティティインスタンスのみを提供します。

あなたがそれを管理する方法は、何を持ってしたいことTestExecutionは、テストの実行は複数の結果を持つことができることを意味し、testResultsコレクションを制御することができるようになる、@OneToManyですが、それぞれの結果は一つだけの実行に所属するようになります。 。

TestExecution: TestResult

@OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="testExecution") 
private Set<TestResult> testResults; 

:あなたはTextExecutionはそのTestResultコレクションを管理でき

@ManyToOne(fetch=FetchType.EAGER) 
@JoinColumn(name="id_test_result") 
private TestExecution testExecution; 

この方法では、(TestExecutionオブジェクトを保存するときCascadeType.ALLを使用して、コレクションに変更を伝播) 。

も参照してください:あなたは `mappedBy`属性を提供することにより、関係の所有者側を設定する必要があり

+0

ManyToOneとOneToManyについての説明をありがとうございます。私はマッピングが正しくないと思った。 私はまだ質問がありますが、私は今でも同じ結果を得ています。 'TestResult'はどのように' TestExecution'オブジェクトに伝播されますか? 'TestResult'を保存してからこのオブジェクトを取り出し、' TestExecution'セットに追加する必要がありますか?あるいは 'TestResult'オブジェクトに' TestExecution'をセットして 'TestResult'を保存するときにこれを行うべきでしょうか? – ALM

+0

例:保存は 'TestExecution'は' TestResult'はまだ「TestExecution」 (testResult)の保存に設定された空白の結果を残して上の設定で、私はその後、呼び出しは 'TestExecution' testExecution.getTestResults(にこれを追加する作りますか).add(testResult); save(testExecution) – ALM

+0

重複を停止するために自分のhashCode()を実装する必要があるようです。私は基本的なものを試しましたが、うまくいきませんでしたので、私はそれを検討します。 は ' は、あなたの助けをありがとうございました、私はセットアップにequalsAndHashCode' @EqualsAndHashCodeロンボクを伝える使用するように更新(除外= {「ID」、「testExecution」、「デバイス」、「テストケース」})あなた – ALM

関連する問題