简体   繁体   English

Play Framework 1.x中的模型ID的8位随机值

[英]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.

相关问题 在将模型保存到Play Framework 1.x中之前执行一种方法 - Execute a method before saving a model in Play Framework 1.x 在Tomcat上播放框架1.x-httpOnly cookie - Play framework 1.x on Tomcat - httpOnly cookies 在Play 1.x框架中使用SOAP Web服务 - Consume SOAP Web-service in Play 1.x framework 对于Play Framework 1.x,日本的Rythm模板引擎优势? - Advantages Rythm Template Engine over Japid for Play Framework 1.x? 使用Play Framework 1.x,如何让JPA在生产模式下创建缺少的数据库表? - Using Play Framework 1.x, how do I get JPA to create missing DB tables in production mode? 将 MySQL 连接器 Jar 链接到我的依赖项 Java Play Framework 1.x - Linking MySQL Connector Jar To My Dependencies Java Play Framework 1.x 如何在Visual Studio Code + Java上调试Play Framework 1.x应用程序 - How do I debug play Framework 1.x applications on Visual Studio Code + java 是否可以同时使用Play 1.x和Akka? - Is it possible to use Play 1.x and Akka together? 玩! 框架bootstrap(Fixtures.loadModels(“ initial-data.yaml)),在模型的setter中注入“ random”字符串值 - Play! framework bootstrap(Fixtures.loadModels(“initial-data.yaml)), injecting ”random" String value in model's setter 播放框架不是有效的ID - play framework not a valid id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM