简体   繁体   English

我不能在代码中使用任何ID

[英]I can't use any ID in my code

I was always taught to use IDs in my code to refer to records into the database. 我总是被教导要在代码中使用ID来引用数据库中的记录。

But let's take the case we have same roles in the table Role. 但是,让我们假设在表Role中具有相同的角色。 Now I want to query only the records related to the role Player: 现在,我只想查询与角色播放器相关的记录:

ID    ROLE
1     Admin
2     Organizer
3     Player

I don't know in my code the ID of Player, but I want to retrieve all the players, so with Hibernate I wrote: 我在代码中不知道Player的ID,但是我想检索所有的Player,所以我在Hibernate中写道:

String queryString = "from User u where u.role.role = ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, "player");
return queryObject.list();

As you can see I wrote "player" in the code. 如您所见,我在代码中写了“ player”。 I think this is not the best way and I should use an ID instead. 我认为这不是最好的方法,我应该改用ID。 But I don't know the ID and it may change depending on the server on which I run the application. 但是我不知道ID,它可能会根据运行应用程序的服务器而改变。 A second problem with my solution is that "player" can be capitalized into the database and this may be changed over time. 我的解决方案的第二个问题是“玩家”可以大写到数据库中,并且可以随时间改变。

So, what should be the solution to all these problems? 那么,所有这些问题的解决方案应该是什么? Is there any way to use the ID instead? 有什么方法可以代替使用ID? Or any other way to improve this code? 或任何其他方式来改进此代码?

In this case it seems that role should be an enum and your query would look something like: 在这种情况下,似乎角色应该是一个枚举,您的查询将类似于:

queryObject.setParameter(0, Role.PLAYER);

Also, you might take a look at the criteria API which will help you create more type-safe queries that are more robust vs. refactoring. 另外,您可能会看到标准API,它将帮助您创建更可靠与重构相比更安全的类型安全查询。

You can do using create a mapping table like , 您可以使用创建映射表,例如

UserRoleMapping ID - Incremental, UserId - Refers to user table, RoleId - Refers to role table

As one user can have more than one role so it will satisfy that thing too. 由于一个用户可以担任多个角色,因此它也将满足这一要求。 to Get the roles using query 使用查询获取角色

select role.id from userrolemapping urm innerjoin on user u.id = urm.id where u.id = ?

You should create a enum class like this. 您应该创建一个这样的enum类。

public enum Role { 公共枚举角色{

Admin(1),
Organizer(2),
Player(3);

} }

And change your code to 并将您的代码更改为

String queryString = "from User u where u.id= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject .setParameter(0, Role.PLAYER);
return queryObject.list();

using IDs or string/vachar etc. lookup is all dependent on the data that you have in the database. 使用ID或字符串/ vachar等。查找都取决于数据库中的数据。 Some organization keep the id static and some keep the name/description static. 一些组织将id保持为静态,而一些组织将名称/描述保持为静态。 So, make sure you have good understanding of your DB data and what will stay static . 因此,请确保您对DB数据以及将保持静态的数据有充分的了解。 However, if the role is static you can use HQL ignore case like the example I provided for you below (I'm not adding information about the ID static path because others have already provided information about it for and don't want to duplicate it ). 但是,如果角色是静态的,则可以使用HQL忽略大小写,例如我在下面为您提供的示例(我没有添加有关ID静态路径的信息,因为其他人已经提供了有关该信息的信息,并且不想重复该信息) )。 --note you can take the percentages out if you only want "player" -请注意,如果您只想要“玩家”,则可以取出百分比

String queryString = "from User u where lower( u.role.role )  like lower('%"+ ? +"%')";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, "player");
return queryObject.list();

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

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