java - How to post list of users in Spring -
i have domain class in spring:
@entity @table(name="like") public class { @id @generatedvalue(strategy = generationtype.identity) long id; @manytoone(cascade= cascadetype.merge, targetentity = user.class) @joincolumn(name = "user_id") @ondelete(action= ondeleteaction.cascade) set<user> user; @onetomany(mappedby = "like", orphanremoval = true ,cascade= cascadetype.all, targetentity = picture.class) @ondelete(action= ondeleteaction.cascade) set<picture> pictures; public like() { } public like(set<user> user) { this.user = user; } public long getid() { return id; } public void setid(long id) { this.id = id; } @jsonignore public set<user> getuser() { return user; } @jsonignore public void setuser(set<user> user) { this.user = user; } @jsonignore public set<picture> getpictures() { return pictures; } @jsonignore public void setpictures(set<picture> pictures) { this.pictures = pictures; } } }
and have table in sql script
create table if not exists `like` ( `id` int not null primary key auto_increment, `user_id` bigint, foreign key (user_id) references `user`(id) )engine=innodb default charset=utf8;
and problem when send post request in postman:
java.lang.illegalargumentexception: can not set java.lang.long field com.nyters.webapp.domain.user.id java.util.hashset
controllerlike.java
@restcontroller @requestmapping("api/like") public class likecontroller { private likeservice likeservice; @autowired public likecontroller(likeservice likeservice){ this.likeservice = likeservice; } @requestmapping(path = "/{id}", method = requestmethod.get) public responseentity<likedto> findone(@pathvariable long id) { likedto picturedto = likeservice.findone(id); if (picturedto != null) { return new responseentity<>(picturedto, httpstatus.ok); } else { return new responseentity<>(httpstatus.not_found); } } @requestmapping(method = requestmethod.get) public responseentity<list<likedto>> findall() { list<likedto> likedtos = likeservice.findall(); if (likedtos != null) { return new responseentity<>(likedtos, httpstatus.ok); } else { return new responseentity<>(httpstatus.not_found); } } @requestmapping(method = requestmethod.post) public responseentity<likedto> save(@requestbody string likedtostring) throws ioexception { likedto likedto = new objectmapper().readvalue(likedtostring, likedto.class); likedto saved = likeservice.save(likedto); if (saved != null) { return new responseentity<>(saved, httpstatus.ok); } else { return new responseentity<>(httpstatus.not_found); } }
i guess prob mapping
like.java
@fetch(value = fetchmode.select) @onetomany(cascade = cascadetype.all) @joincolumn(name = "user_id") @jsonignore private list<user> userlist; //based on user_id u can fetch userlist db
user.java
@id @generatedvalue(strategy = generationtype.auto) @column(name = "user_id") private long user_id;
Comments
Post a Comment