how to update table containing foreign keys using query or EntityManager?

Viewed 25

when i send the request using PUT http://localhost:8080/messenger/api/message_mapping/update/1
i get this replay Cannot construct instance of com.hizam.rest_service.entity.User (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (17) at [Source: (io.undertow.servlet.spec.ServletInputStreamImpl); line: 2, column: 12] (through reference chain: com.hizam.rest_service.entity.MessageMapping["to_id"])

class MessageMapping

@Entity
@XmlRootElement
@Getter
@Setter
@Table(name="massengermap")
public class MessageMapping implements Serializable{


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "to_id")
    private User to_id;

    @ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "from_id")
    private User from_id;

    @ManyToOne(targetEntity = Message.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "message_id")
    private Message message_id;


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MessageMapping that = (MessageMapping) o;
        return id == that.id && Objects.equals(to_id, that.to_id) && Objects.equals(from_id, that.from_id) && Objects.equals(message_id, that.message_id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, to_id, from_id, message_id);
    }

    @Override
    public String toString() {
        return "MessageMapping{" +
                "id=" + id +
                ", to_id=" + to_id +
                ", from_id=" + from_id +
                ", message_id=" + message_id +
                '}';
    }
}

class User

@XmlRootElement
    @Entity
    @Getter
    @Setter
    @Table(name="user_message")
    public class User implements Serializable{
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long id;
    
        @Column(name = "nick_name")
        private String nick_name;
    
        @Column(name = "user_type")
        private String userType;
    
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            User user = (User) o;
            return id == user.id && Objects.equals(nick_name, user.nick_name) && Objects.equals(userType, user.userType);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(id, nick_name, userType);
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", nick_name='" + nick_name + '\'' +
                    ", userType='" + userType + '\'' +
                    '}';
        }
    }

class MessageMappingRegistrator

public void update(long id) {
            logger.info("Updated message mapping: " + id);
                MessageMapping item = entityManager.find(MessageMapping.class,id);
                Query items = entityManager.createQuery("UPDATE MessageMapping set  to_id=:idr,from_id=:idd, message_id=:idddr where id=:id");
                items.setParameter("idr",item.getTo_id());
                items.setParameter("idd", item.getFrom_id());
                items.setParameter("idddr", item.getMessage_id());
                items.setParameter("id", item.getId());
                items.executeUpdate();
        }

class MessageMappingRest

@PUT
    @Path("/update/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public void updateMapping(@PathParam("id") long id,MessageMapping item)
    {
       registrator.update(id);
    }

I also tried using the function merge() but it fails when it comes to updating table that contains foreign keys

public MessageMapping update(MessageMapping item) {
        logger.info("Updated message mapping: " + item);
        entityManager.merge(item);
        return item;
    }
0 Answers
Related