mtmx
mtmx

Reputation: 947

Hibernate: POST request for user registration with adding role foreign key

I have problem with adding the role to a user in one post request. This is my DB diagram:

enter image description here And I have the following classes: first is entity role:

    package application.model;

    import lombok.*;
    import javax.validation.constraints.NotEmpty;
    import javax.validation.constraints.NotNull;
    import javax.persistence.*;
    import java.util.List;

    @NamedQueries({
            @NamedQuery(name = User.GET_USERS, query = User.QUERY_GET_USERS),
    })
    @Getter
    @Setter
    @NoArgsConstructor
    @Entity
    @Table(name = "users")
    public class User {
        public static final String GET_USERS = "User.get_users";
        public static final String QUERY_GET_USERS = "select u from User u";


        @Id
        @NotNull
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        @Column(name = "id")
        public int id;

        @NotNull
        @NotEmpty
        @Column(name="firstname")
        private String firstname;

        @NotNull
        @Column(name="lastname")
        private String lastname;

        @NotNull
        @Column(name="email")
        private String email;

        @NotNull
        @Column(name="password")
        private String password;

        @JoinTable
        @OneToMany
        private List<Role> roles;
    }

second entity is Role:

    package application.model;

    import lombok.Getter;
    import lombok.NoArgsConstructor;
    import lombok.Setter;
    import javax.persistence.*;
    import javax.validation.constraints.NotNull;

    @Getter
    @Setter
    @NoArgsConstructor
    @Entity
    @Table(name = "role")
    public class Role {
        @Id
        @NotNull
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        public int id;

        @NotNull
        @Column(name="name")
        private String name;

    }

also, I have UserDAO:

    package application.dao;

    import application.model.User;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.PersistenceContextType;
    import java.util.List;


    @Repository
    public class UserDAOImpl implements UserDAO {

        @PersistenceContext(type = PersistenceContextType.EXTENDED)
        private EntityManager em;

        @Transactional(readOnly = false)
        @Override
        public User addCustomer(User user) {
            return em.merge(user);
        }

        @Override
        public List<User> findAll() {
            return em.createNamedQuery(User.GET_USERS, User.class)
                    .getResultList();
        }

    }

userService

    package application.service;

    import application.dao.UserDAO;
    import application.model.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    @Service
    public class UserServiceImpl implements UserService {

        @Autowired
        private UserDAO userDAO;

        @Override
        public User addCustomer(User user) {
            return userDAO.addCustomer(user);
        }
    }

So, I have a problem with adding roles to the user during registration. My post request without adding role is:

    {
        "firstname": "imasdie5",
        "lastname": "nazasdwisko5",
        "email": "masdil",
        "password": "pass"
    }

and in response I have null on roles field, this is ok because the role is not assigned:

    {
        "id": 27,
        "firstname": "imasdie5",
        "lastname": "nazasdwisko5",
        "email": "masdil",
        "password": "pass",
        "roles": null
    }

when I try to register user + role assign with the following JSON:

    {
        "firstname": "imasdie5",
        "lastname": "nazasdwisko5",
        "email": "masdil",
        "password": "pass",
          "roles":{ "id":2 }
    }

I receive error 400 with "The server cannot or will not process the request due to something that is perceived to be a a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing)."

Do You have any advice on how to register the user with role assign? I don't know why I have error 400 in this case, how to avoid it and set role in one post request? (user_id, role_id is stored in users_role table - this table don't have entity) Or maybe this is impossible and first, I should have to register the user, get registered user id, and insert his role to uesrs_role table by hql insert create a query?

https://github.com/mtpx/khn - this is my whole repository if someone need it :)

Upvotes: 0

Views: 188

Answers (1)

AnnKont
AnnKont

Reputation: 506

Try this

{
        "firstname": "imasdie5",
        "lastname": "nazasdwisko5",
        "email": "masdil",
        "password": "pass",
        "roles": [
             { "id":2 }
         ]
    }

roles is an array.

Upvotes: 1

Related Questions