简体   繁体   English

按组划分的JPA实体的辅助ID

[英]Secondary ID for JPA entity by group

Let's suppose there are 2 JPA entities: 假设有两个JPA实体:

public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @ManyToOne
    @JoinColumn(name = "CITY_ID")
    private City city;

    @Temporal(TemporalType.TIMESTAMP)
    private Date createdAt;

    String name;
}

public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @OneToMany(mappedBy = "city")
    List<Person> people;
}

And there are 10 "City" objects with 5 different "Person" in each of them, that makes a total of 50 items in "Person" table, randomly generated. 并且有10个“城市”对象,每个对象中有5个不同的“人物”,这使得“人物”表中总共有50个项目是随机生成的。

Now i want to list "Person" objects from "City" ID 3 by querying a rest end point: 现在,我想通过查询其余端点来列出“城市” ID 3中的“人”对象:

GET /api/cities/3/people

Let's suppose response from the rest api controller is: 让我们假设来自其余api控制器的响应是:

[
    { id : 11, city_id: 3, created_at: 1445831581186, name: 'Michael' },
    { id : 19, city_id: 3, created_at: 1446831581186, name: 'Jennifer' },
    { id : 26, city_id: 3, created_at: 1447831581186, name: 'Josh' },
    { id : 27, city_id: 3, created_at: 1448831581186, name: 'Aaron' },
    { id : 45, city_id: 3, created_at: 1449831581186, name: 'Gabriel' }
]

what i need is response to be like: 我需要的是像这样的回应:

[
    { id : 1, city_id: 3, created_at: 1445831581186, name: 'Michael' },
    { id : 2, city_id: 3, created_at: 1446831581186, name: 'Jennifer' },
    { id : 3, city_id: 3, created_at: 1447831581186, name: 'Josh' },
    { id : 4, city_id: 3, created_at: 1448831581186, name: 'Aaron' },
    { id : 5, city_id: 3, created_at: 1449831581186, name: 'Gabriel' }
]

and for a filtered query: 对于过滤查询:

GET /api/cities/3/people?createdAfter=1447831581186

[
    { id : 4, city_id: 3, created_at: 1448831581186, name: 'Aaron' },
    { id : 5, city_id: 3, created_at: 1449831581186, name: 'Gabriel' }
]

i mean, the json response ids must be reordered within "City" domain. 我的意思是,JSON响应ID必须在“城市”域内重新排序。

The ID's on the MySQL database must still be the original ones 11, 19, 26, 27 and 45, i don't want / need this "temporary" id to persist, but i can have a secondary "ID_IN_CITY" column if needed. MySQL数据库上的ID仍必须是原始的11、19、26、27和45,我不希望/需要此“临时” ID保留,但是如果需要,我可以有一个辅助的“ ID_IN_CITY”列。 Also, if i change the "City" of a "Person" this secondary id must be recalculated. 另外,如果我更改“人员”的“城市”,则必须重新计算该辅助ID。

What's the best technique to achieve this ? 实现此目的的最佳技术是什么?

This example was just to illustrate my problem, my goal is to make ids clean to the end user of my system. 这个例子只是为了说明我的问题,我的目标是使ID对我系统的最终用户干净。 When they create the first item, they'll see beautiful ID 1, and not ID 3455958. Also not giving them a clue of how large is my database. 当他们创建第一个项目时,他们会看到漂亮的ID 1,而不是ID3455958。也没有给他们提供有关我的数据库有多大的线索。

Thanks 谢谢

First, let me ask a question that may seem unnecessary, but bears asking: 首先,让我问一个看似不必要的问题,但请问:

Why does it matter that you want the user to see "1" when they first create a user? 为什么要让用户在首次创建用户时看到“ 1”很重要?

This seems like a silly requirement and possibly even a code smell. 这似乎是一个愚蠢的要求,甚至可能还有代码气味。

To answer your question, it's old-school hack and not a JPA solution, but you could hold a Map of ids in memory to map to the records that have been displayed. 要回答您的问题,这是一种老式的技巧,而不是JPA解决方案,但是您可以在内存中保存一个ID映射以映射到已显示的记录。 So for instance you would store the database ID for the 'Michael' record in an Map object that had the DB id as a key and the displayed id as a value. 因此,例如,您会将“ Michael”记录的数据库ID存储在Map对象中,该对象具有DB ID作为键,而显示的ID作为值。 If the DB id does not exist in the Map, then add it, incrementing the request ID number. 如果地图中不存在数据库ID,则添加它,并增加请求ID号。

...
static Map<Long, Long> idGrid = new HashMap<Long, Long>();
long displayId = 1L;

//then insert
idGrid.put(databaseId, displayId++);

//remove
idGrid.remove(databaseId);

//get an id
Long displayId = idGrid.get(databaseId);

...

This would allow you to store any database ID, and display any other arbitrary ID to the user. 这将允许您存储任何数据库ID,并向用户显示任何其他任意ID。

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

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