[英]Random 8 digit value for model id in Play Framework 1.x
I would like to use random 8 digit number id for model
in play! 1.2.5
我想在
play! 1.2.5
使用随机的8位数字ID作为model
play! 1.2.5
play! 1.2.5
instead of auto_increment
value and also without creating a random value
, assigning it to id by myself. play! 1.2.5
代替auto_increment
值,并且也没有创建random value
,而是由我自己将其分配给id。 I do not know it is possible or not. 我不知道有没有可能。 I really googled about it but I could not find anything.
我真的用谷歌搜索,但找不到任何东西。
for the answer below. 对于下面的答案。 Here is what i need in my project.
这是我在项目中需要的。 Suppose that i have a user object which has 2 attr.
假设我有一个具有2 attr的用户对象。 name and surname.
名和姓。 With the defult creation of this object, JPA assing a auto_inc value for the id of this object.
通过创建该对象,JPA会为该对象的id分配一个auto_inc值。
@Entity
public class User extends Model{
public String name;
public String surname;
}
And here i have createUser method in my controller. 在这里,我的控制器中有createUser方法。
public static void createUser(String name, String surname){
User user = new User();
user.name = name;
user.surname = surname;
/* it seems to me that the answer below can be a solution for what i want like that
* user.id = javaGenereted8digitNumId();
* But I dont want this. I want it is handled in model class
* and I guess it can be with JPA GenericModel. am I right?
*/
user.save();
}
use: 采用:
int ID = (int) (Math.Random()*(99999999-a)+a); //a being the smallest value for the ID
and if you want to include the 0's on the left, use: 如果要在左侧添加0,请使用:
import java.text.DecimalFormat;
and in your code: 并在您的代码中:
DecimalFormat fmt = new DecimalFormat("00000000");
int ID = Integer.parseInt(fmt.format((int) (Math.Random()*(99999999-a)+a)));
EDIT: This is an update to your update of the question. 编辑:这是对问题更新的更新。
If you want the model class of User to create its own unique 8digit ID everytime, i suggest you make a static variable that saves the last currently used ID for all User objects created. 如果您希望User的模型类每次创建自己的唯一8位ID,我建议您创建一个静态变量,该变量将为创建的所有User对象保存最后一个当前使用的ID。 This way on creation of a new user, it will simply create a new user with the next available ID, this will of course be limited to 99999999 ID's.
这样,在创建新用户时,它将简单地创建具有下一个可用ID的新用户,这当然将限制为99999999 ID。 If you want to go further then this, you will need to create a very large static String containing all the used ID's seperated by spaces, and everytime you want to add a user, it will check for the ID availability by using .contains("ID") method
如果要进一步进行此操作,则需要创建一个非常大的静态字符串,其中包含用空格分隔的所有使用的ID,并且每次要添加用户时,都将使用.contains(“检查ID的可用性。 ID“)方法
Here's an example of what your User class should look like i believe: 这是您认为User类应该看起来像的一个示例:
public class User extends Model
{
public String name, surname;
public int ID;
public static int lastID;
public static String usedIDs;
public User(String name, String surname) //regular User objects
{
this.name = name;
this.surname = surname;
ID = ++lastID;
usedID's += ID + " ";
}
public User(String name, String surname, int n) //first User object created
{
this.name = name;
this.surname = surname;
ID = 1;
lastID = 1;
usedID's = ID + " ";
}
//rest of your methods
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.