简体   繁体   English

JPA:如何在多对多关系中保存包含订单的列表

[英]JPA : How to save list with order in a many-to-many relationship

When I persist a list which has an order, the result in the database has a different order. 当我持有具有订单的列表时,数据库中的结果具有不同的顺序。 How can I persist a many-to-many relationship with list order in JPA? 如何在JPA中保持与列表顺序的多对多关系?

@Entity
@Table(name = "house")
public final class House {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Basic(optional = false)
    @Column(name = "name")
    private String name

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "house_room",
            joinColumns =
            @JoinColumn(name = "id_house", referencedColumnName = "id"),
            inverseJoinColumns =
            @JoinColumn(name = "id_room", referencedColumnName = "id"))
    private List<Room> rooms;
}

House object: 房子对象:

Room room1 = new Room();
room1.setName("Kitchen");

Room room2 = new Room();
room2.setName("Bathroom");

Room room3 = new Room();
room3.setName("Bedroom");

List<Run> runs = new ArrayList<Run>();
rooms.add(0,room2);
rooms.add(1,room1);
rooms.add(2,room3);

House cottage = new House();
cottage.setRooms(rooms);

persist(cottage); // order in database should be "room2", "room1" then "room3" but it is saved as "room1", "room2" then "room3"

There are 2 ways to order list in JPA using @OrderBy and @OrderColumn annotations. 有两种方法可以使用@OrderBy和@OrderColumn注释在JPA中排序列表。 @OrderColumn is perhaps what you are looking for. @OrderColumn也许就是你要找的东西。

@OrderBy @OrderBy

Specify an ordering rule based on the comparison of a particular attribute of the entity or element 根据实体或元素的特定属性的比较指定排序规则

Example: 例:

@OrderBy("name ASC")
private List<Room> rooms;

By adding an @OrderBy annotation on the mapping, we can indicate that we want the rooms to be ordered in ascending alphabetical order by its name attribute. 通过在映射上添加@OrderBy注释,我们可以指示我们希望房间按名称属性按字母顺序升序排序。 Note: We needn't have included the ASC in the @OrderBy annotations because it would be ascending by default. 注意:我们不需要在@OrderBy注释中包含ASC,因为默认情况下它会升序。

Simply changing the order of the items in a List in memory will not cause that order to be stored in the database at commit time. 简单地更改内存中List的项目顺序不会导致该命令在提交时存储在数据库中。

The annotation applies only when collection is retrieved from the database. 仅在从数据库中检索集合时才应用注释。 That means, the order will not be stored in the database but when data is retrieved from the DB and stored to the List collection in memory, they will be ordered. 这意味着,订单不会存储在数据库中,但是当从数据库中检索数据并将数据存储到内存中的List集合时,它们将被订购。

@OrderColumn @OrderColumn

The OrderColumn annotation specifies a column that is used to maintain the persistent order of a list. OrderColumn批注指定用于维护列表的持久顺序的列。 Example: 例:

@OrderColumn(name="ROOM_ORDER")
private List<Room> rooms;

This means you have an additional column named "ROOM_ORDER" that will indicate the order of your items in your table. 这意味着您有一个名为“ROOM_ORDER”的附加列,它将指示表中项目的顺序。

Relational databases do not store order of referenced data. 关系数据库不存储引用数据的顺序。 In order to store the order of elements in a list, JPA needs another column in DB table. 为了将元素的顺序存储在列表中,JPA需要DB表中的另一列。 You may map the column with @OrderColumn in the same ways, as you mapped @JoinColumn (you just need single @OrderColumn , even when you used multiple @JoinColumn annotations) 你可以用相同的方式映射列@OrderColumn ,就像映射@JoinColumn (你只需要单个@OrderColumn ,即使你使用了多个@JoinColumn注释)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM