Spring Data JPA中的一对多关系

时间:2017-11-02 00:01:48

标签: java spring hibernate jpa spring-data

我希望在两个实体,消费者和政策之间建立一对多的关系。一个消费者应该有几个政策。

这是我想要的Consumer JSON对象的一个​​例子:

<Icon.ToolbarAndroid
              titleColor={'#000000'}
              style={{
                 height: 56,
                 alignSelf: 'stretch',
                 width: 20
               }}
              iconColor={"#fff"}
              iconSize={26}
              rtl={true}
              navIconName={"more-vert"}
              contentInsetStart={50}
              actions={[{title: 'Settings', show: 'always'}, {title: 'Settings 2', show: 'always'}]} />

这是我到目前为止所做的:

政策实体

{
     id : 1,
     name : "Peter",
     endpoint: "123.456.778",
     policies: [
                    {
                       id : 1,
                       name: "policy 01"
                    },
                    {
                       id : 2,
                       name: "policy 02"
                    }
             ]
}

消费者实体

@Entity
public class Policy {
        @Id
        @GeneratedValue
        @Column(name = "id")
        private Integer id;

        @Column(name = "name")
        private String name;

        //getters and setters
    }

我认为这并不难,但我现在尝试几个小时并且无法完成它。我是Spring的新手,所以如果有人能够帮助我,我会非常感激!

2 个答案:

答案 0 :(得分:1)

@Entity
public class Consumer {

    @OneToMany(mappedBy = "consumer")
    private List<Policy> policies;

}

@Entity
public class Policy {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn("consumer_id")
    private Consumer consumer;

}

fetch = FetchType.LAZY不是必需的,但是可取的。

我在这里提供了一些基础知识

what is @JoinColumn and how it is used in Hibernate

如果您想要Policy没有Consumer

您可以使用联接表

@Entity
public class Consumer {

    @OneToMany
    private List<Policy> policies;

}

@Entity
public class Policy {

}

单向关系(Policy表格会有consumer_id列,但Policy类没有Consumer

@Entity
public class Consumer {

    @OneToMany
    @JoinColumn("consumer_id")
    private List<Policy> policies;

}

@Entity
public class Policy {

}

另外,请注意,如果您想使用Policy作为参考(来自字典),则需要@ManyToMany

答案 1 :(得分:0)

试试这段代码:)

您的消费者类

<div class="info-description">

请注意,在映射的旁边,您应该使用数据库中引用Policy的列的名称,因此,如果它没有policyId,请使用您为其指定的名称

您的政策类

<div class="info-container">
<div class="info-box">
<div class="container">
    <div class="info-title">Why would you buy Buy LoL Account?</div>
    <div class="info-description">
            Are you searching for an affordable high-quality League of Legends account? You’ve come to the right place. 
            We sell Smurf accounts with impressive rankings, from Diamond, Silver, Gold, and even Platinum but the big question 
            is why should you buy from us and not the other online sellers? 
        </div>
    </div>
</div>

<div class="info-box">
    <div class="container">
        <div class="info-title">Buy LoL Smurfs Accounts</div>
<div class="info-description">
        A Smurf is an account owned by a player who has another level 30 account. The Smurf account is mainly used for training purposes against other more skilled opponents. We have Smurfs accounts already in level 30 so you won’t need to level them higher again. These will provide you with the quickest way to amass riot points and rise to level 30 in League of Legends. When you buy Smurfs account from us, it will be delivered instantly with all the necessary instructions.
        </div>
    </div>
</div>  

<div class="info-box">
    <div class="container">
        <div class="info-title">Buy LoL Smurfs Accounts</div>
<div class="info-description">
        A Smurf is an account owned by a player who has another level 30 account. The Smurf account is mainly used for training purposes against other more skilled opponents. We have Smurfs accounts already in level 30 so you won’t need to level them higher again. These will provide you with the quickest way to amass riot points and rise to level 30 in League of Legends. When you buy Smurfs account from us, it will be delivered instantly with all the necessary instructions.
        </div>
    </div>
</div>  
</div>