0

New users are created with the POST method, and after this the database will attach it an unique ID, which is necessary to create a token.

The only way to create a token, is after the process of user creation. I query it from the database and now it should have ID on it, but I can't figure how to do that.

Is there any way to retrieve the entity from database right after creating it?

If looked at this similar question, but couldn't find an answer: Is it ok by REST to return content after POST?

@POST
    @Consumes({MediaType.APPLICATION_JSON})
    public Response create(@QueryParam("email") String email, @QueryParam("username") String userName, @QueryParam("password") String password) {
        if (TextUtil.isEmpty(userName) || TextUtil.isEmpty(password) || TextUtil.isEmpty(email)) {
            return Response.status(Response.Status.BAD_REQUEST).build();
        } else {
            User newUser = new User();
            newUser.setEmail(email);
            newUser.setUsername(userName);
            newUser.setPass(password);
            super.create(newUser); //ths line create user in database
            String id = newUser.getUid() + ""; //the newUser has no id yet so it is null but I want this id from database
            return Response.status(Response.Status.OK).entity("id: " + id).build(); 
        }   
    }

Create super.method

public void create(T entity) {
        getEntityManager().persist(entity);
    }

I generate REST api using this tutorial https://netbeans.org/kb/docs/websvc/rest.html

coinhndp
  • 2,281
  • 9
  • 33
  • 64

2 Answers2

0
getEntityManager().persist(entity);

Set the id of the entity by calling entity.setId(...).

Peter Rader
  • 237
  • 3
  • 14
  • I set id in database by using auto increment. How can I set id by code? I cannot set it mannually right? –  coinhndp Nov 16 '17 at 13:57
0

I my comments I show how to sync EntityManager. Maybe you should write a better create method:

public <T> T create(T t) throws Exception {

        try {
            entityManager.persist(t);
            entityManager.flush();
        } catch (Exception e) {
            throw e;
        } finally {
            entityManager.clear();
        }

        return t;
    }

And then

newUser = super.create(newUser);
Rafael Guillen
  • 1,343
  • 10
  • 25
  • the newUser does not have an Id because the id is only created in database –  coinhndp Nov 16 '17 at 16:52
  • When `entityManager.flush();` is called, all changes are written to the database and the Ids defined in JPA are generated. How do you generate the UId field of User? – Rafael Guillen Nov 16 '17 at 17:20
  • I generate it in database by using auto increment. If id is null then it auto generates an id for my user. How should I generate id? I use mySQL –  coinhndp Nov 16 '17 at 17:39
  • UId field or getter should have `@Id` and `@Autogenerated` annotations, look here https://stackoverflow.com/questions/4102449/how-to-annotate-mysql-autoincrement-field-with-jpa-annotations or here https://stackoverflow.com/questions/25047226/jpa-generationtype-auto-not-considering-column-with-auto-increment for more details – Rafael Guillen Nov 16 '17 at 17:43
  • Yes I had it. Look at my code above and you can see I have getUId but it is null (in the database it is not null but in newUser object it is null). Thats my problem –  coinhndp Nov 16 '17 at 17:53