使用key作为Integer映射java.util.Map并进行排序

时间:2021-10-19 00:22:15

Continuing with this I decided to take an approach of making a different entity and make a OneToOne relationship (I hope this is OK). Bet let me explain what I have been doing:

继续这一点,我决定采取一种方法来建立一个不同的实体并建立OneToOne关系(我希望这没关系)。请允许我解释一下我在做什么:

I have a Juez (judge) entity with these relevant fields:

我有一个具有以下相关领域的Juez(法官)实体:

@Entity
@Table(name = "jueces")
public class Juez implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "id")
    private Integer id;

    @Version
    @Column(name = "opt_lock")
    private Integer version;

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

    // Getters, setters, other properties, etc.

}

And I want another entity called RolDeJueces (judge roll) to hold a java.util.Map<Integer, Juez> in which I want the keys to be sorted (1,2,3...) and mapped to a judge so I can have a roll to make some calculations. Here is what I have until now:

我想要另一个名为RolDeJueces(判断roll)的实体来保存java.util.Map ,其中我想要对键进行排序(1,2,3 ......)并映射到一个判断所以我可以滚动进行一些计算。这就是我现在所拥有的: ,juez>

@Entity
@Table(name = "rol_jueces")
public class RolDeJueces {

     @Id
     private Integer id;

     @Version
     @Column(name = "opt_lock")
     Integer version;

     @OneToMany //Is this ok?
     @MapKeyColumn(name = "orden_juez", updatable = true)
     private Map<Integer, Juez> rol;

     // Getters, setters, other properties, etc.

}

How can I sort the map key and hold a reference to all my judges but only one time (a la @OneToOne)?

我如何对地图键进行排序并保留对我所有评委的引用,但只有一次(la @OneToOne)?

Also, I must be capable of reordering my judges and make the changes persistent e.g.

此外,我必须能够重新安排我的评委,并使更改持久,例如

rolDeJueces.getRol().clear(); // Delete current order
int order = 0;
// Add judges according to new order to the roll
for(Juez juez : getNewOrder()){
    RolDeJueces.getRol().put(++order, juez);
}

// save my new roll and stuff...

Is this possible?

这可能吗?

1 个解决方案

#1


0  

Ok, I figured that I do not need a map for insertion order nor the stuff I wanted.

好吧,我认为我不需要插图顺序的地图也不需要我想要的东西。

Here is the impl:

这是impl:

 @Id
 @GeneratedValue(strategy = GenerationType.TABLE)
 private Integer id;

 @Version
 @Column(name = "opt_lock")
 private Integer version;

 @OneToMany(orphanRemoval = false)
 @OrderColumn(name = "order_jueces")
 private List<Juez> rol;

@OrderColumn will generate an extra column in the table used for the relationship.

@OrderColumn将在用于关系的表中生成一个额外的列。

#1


0  

Ok, I figured that I do not need a map for insertion order nor the stuff I wanted.

好吧,我认为我不需要插图顺序的地图也不需要我想要的东西。

Here is the impl:

这是impl:

 @Id
 @GeneratedValue(strategy = GenerationType.TABLE)
 private Integer id;

 @Version
 @Column(name = "opt_lock")
 private Integer version;

 @OneToMany(orphanRemoval = false)
 @OrderColumn(name = "order_jueces")
 private List<Juez> rol;

@OrderColumn will generate an extra column in the table used for the relationship.

@OrderColumn将在用于关系的表中生成一个额外的列。