#One-To-Many Parameter gets not recognized in request - java

(Not sure if title is fitting for my problem, please correct me if necessary)
I have a controller with a postmapping:
#PostMapping("/user")
ResponseEntity addUser(Users receivedUser, OauthGatewayUser oauthGatewayUser) {
Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
logger.info("POST-Request from user: " + oauthGatewayUser.toString());
logger.info("PostMapping: Received User: " + receivedUser);
userService.addUser(oauthGatewayUser, receivedUser);
return new ResponseEntity(receivedUser, HttpStatus.OK);
}
And I have a users class which can have multiple Islands assigned, so the classes look like this:
#Entity
#Table
public class Users {
#Id
#NonNull
#Column(unique = true)
private String id;
#Column(unique = true)
private String userHandle;
private PrivacyLevel privacyLevelProfile;
private boolean isBlocked;
private Long lastActiveIslandID;
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Island> islands = new ArrayList<>();
public Users(String id) {
lastActiveIslandID = 0L;
this.id = id;
privacyLevelProfile = PrivacyLevel.PUBLIC;
}
public Users(String id, String userHandle) {
lastActiveIslandID = 0L;
this.id = id;
this.userHandle = userHandle;
privacyLevelProfile = PrivacyLevel.PUBLIC;
}
public Users() {
}
//Getters and Setters here
}
Island class:
#Entity
#Table
public class Island {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long islandIdOnDevice;
private String name;
private String islandFruit;
public Island(String name, String userID) {
this.name = name;
this.userID = userID;
}
}
public Island() {
}
//Getters and Setters
}
If I now send this as POST-request:
{"id":"baf539b0","islands":[{"islandIdOnDevice":0,"name":"test","fruit":"avocado","userID":"baf539b0"}]}
Then the spring controller logs:
INFO 11916 --- [.83-8083-exec-3] global: POST-Request from user: OauthGatewayUser { id = baf539b0, name = TestUser }
INFO 11916 --- [.83-8083-exec-3] global: PostMapping: Received User: user{id=baf539b0, name='', isBlocked=false, [], last active=null}
What I want is to receive the island as part of the user so I then can attach it in the user service.

The one-to-many annotation specifies an association. It helps spring understand what to do with the collection. But it doesn't 'join' you data in any way when you query it.
Try this:
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinColumn( name = "id", referencedColumnName = "userId")
private List<Island> islands = new ArrayList<>();

Related

post mapping rest api spring boot cannot get id

Im learning, and so far i created many to many bidirectional database - user can create many groups and group can have many users - and i cannot find a way for my GroupsController Post mapping to work, from my understanding, it requires to get firstly Users id, in order to set the right relationship in Join table for Group, because the relationship should be set only when user create/join group, not when user create sign up procedure. Postman throws 500 and intelliJ:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because the return value of "com.ilze.highlight.entity.Groups.getId()" is null] with root cause
java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because the return value of "com.ilze.highlight.entity.Groups.getId()" is null
I use lombok - #Data, #Getter, therefore getId() should be available for use from Group class. My GroupsController with POST mapping when user decides to create a new group:
#RestController
#RequestMapping("api/groups") // pre-path
public class GroupsController{
#Autowired
private GroupsService groupsService;
#Autowired
private UserService userService;
#Autowired
private final GroupsRepository groupsRepository;
#Autowired
private UserRepository userRepository;
public GroupsController(GroupsRepository groupsRepository) {
this.groupsRepository = groupsRepository;
}
#GetMapping("/all-groups")
public List<Groups> getGroups(){
return (List<Groups>) groupsRepository.findAll();
}
#PostMapping("/user/{usersId}/create-group")
public ResponseEntity<Groups> createGroup(#PathVariable(value = "usersId") Long usersId, #RequestBody Groups groupRequest){
Groups group = userRepository.findById(usersId).map(users -> {
long groupsId = groupRequest.getId();
// add and create new group
users.addGroup(groupRequest);
return groupsRepository.save(groupRequest);
}).orElseThrow(() -> new ResourceNotFoundException("Not found user with id = " + usersId));
return new ResponseEntity<>(group, HttpStatus.CREATED);
}
}
Group database class:
#Data
#Entity
#AllArgsConstructor
#NoArgsConstructor
#Getter
#Table(name = "group_collection")
public class Groups {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name ="group_name", unique = true, nullable = false, length = 20)
private String groupName;
#Column(name = "size", nullable = false)
private int size;
#Column(name = "strict", nullable = false)
private boolean strict;
#Column(name = "open", nullable = false)
private boolean open;
#Column(name ="description", length = 300)
private String description;
#Column(name = "create_time", nullable = false)
private LocalDateTime createTime;
#ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.DETACH,
CascadeType.REFRESH
},
mappedBy = "groups")
#JsonIgnore
private Set<User> users = new HashSet<>();
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
And Users class for database:
#Data
#Entity
#AllArgsConstructor
#NoArgsConstructor
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Long id;
#Column(name = "username", unique = true, nullable = false, length = 100)
private String username;
#Column(name = "password", nullable = false)
private String password;
#Column(name = "email", nullable = false)
private String email;
#Column(name = "create_time", nullable = false)
private LocalDateTime createTime;
#Enumerated(EnumType.STRING)
#Column(name = "role", nullable = false)
private Role role;
#Transient
private String accessToken;
#Transient
private String refreshToken;
#ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE,
CascadeType.DETACH,
CascadeType.REFRESH
})
#JoinTable(name = "groups_x_user",
joinColumns = { #JoinColumn(name = "users_id") },
inverseJoinColumns = {#JoinColumn(name = "groups_id")})
private Set<Groups> groups = new HashSet<>();
public void addGroup(Groups group) {
this.groups.add(group);
group.getUsers().add(this);
}
public void removeGroup(long id){
Groups group = this.groups.stream().filter(g ->
g.getId() == id).findFirst().orElse(null);
if(group != null){
this.groups.remove(group);
group.getUsers().remove(this);
}
}
For reference my GroupsService implementation:
#Service
public class GroupsServiceImpl implements GroupsService{
private final GroupsRepository groupsRepository;
public GroupsServiceImpl(GroupsRepository groupsRepository) {
this.groupsRepository = groupsRepository;
}
#Override
public Groups saveGroup(Groups group) {
group.setCreateTime(LocalDateTime.now());
return groupsRepository.save(group);
}
#Override
public Optional<Groups> findByGroupName(String groupName) {
return groupsRepository.findByGroupName(groupName);
}
}
You need to persist the object from request. And since you have Many-2-Many relation, you can insert related object from both sides. In your case: just add existing user to the newly created group
The method will look something like that:
#PostMapping("/user/{usersId}/groups")
public ResponseEntity<Groups> createGroup(#PathVariable(value = "usersId") Long usersId, #RequestBody Groups groupRequest) {
Groups createdGroup = userRepository.findById(usersId)
.map(user -> {
groupRequest.setId(null); // ID for new entry will be generated by entity framework, prevent override from outside
groupRequest.getUsers().add(user); // add relation
return groupsRepository.save(groupRequest);
}).orElseThrow(() -> new ResourceNotFoundException("Not found user with id = " + usersId));
return new ResponseEntity<>(createdGroup, HttpStatus.CREATED);
}

Hibernate One to One mapping Foreign Key null

Here I m having two different tables, User and Profile Image. Both are mapped using One to One relation. The problem is- Foreign key of Profile Image (user_userid) is null when a new profile image is inserted into the database. Why is the user_id remains null... I come up with this issue many time.... Is there any issue with the way I m mapping both the entities?...
User
#Entity
#Getter
#Setter
#Table(name = "Users")
public class User {
#Id
#Column(name = "user_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#OneToOne(cascade = CascadeType.ALL,mappedBy = "user",fetch =FetchType.LAZY)
private UserProfileImage userProfileImage;
}
Profile Image
#Entity
#Getter
#Setter
public class UserProfileImage {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "name")
private String name;
#Column(name = "type")
private String type;
#Column(name = "picByte", length = 1000000)
private byte[] picByte;
#OneToOne(fetch =FetchType.LAZY)
#JoinColumn(name = "user_id")
private User user;
public UserProfileImage() {
super();
}
public UserProfileImage(String name, String type, byte[] picByte) {
this.name = name;
this.type = type;
this.picByte = picByte;
}
}
Controller
public ResponseEntity<String> addProfileImage(int id,MultipartFile file) {
User user=userRepository.findById(id);
UserProfileImage present=user.getUserProfileImage();
if(present==null) {
UserProfileImage userProfileImage = new UserProfileImage(file.getOriginalFilename(), file.getContentType(), file.getBytes());
user.setUserProfileImage(userProfileImage);
}else{
present.setName(file.getOriginalFilename());
present.setType(file.getContentType());
present.setPicByte(file.getBytes());
user.setUserProfileImage(present);
}
userRepository.save(user);
}
As it's stated in the hibernate documentation:
Whenever a bidirectional association is formed, the application developer must make sure both sides are in-sync at all times.
So, you should correct your addProfileImage method in this way:
public ResponseEntity<String> addProfileImage(int id,MultipartFile file) {
User user=userRepository.findById(id);
UserProfileImage present=user.getUserProfileImage();
if(present==null) {
UserProfileImage userProfileImage = new UserProfileImage(file.getOriginalFilename(), file.getContentType(), file.getBytes());
// sync both sides of bidirectional association
user.setUserProfileImage(userProfileImage);
userProfileImage.setUser(user);
} else {
present.setName(file.getOriginalFilename());
present.setType(file.getContentType());
present.setPicByte(file.getBytes());
// this is redundant
// user.setUserProfileImage(present);
}
userRepository.save(user);
}

Hibernate Map ID automatically from field

I have something similar to this:
#Entity
#Table(name = "claim", schema = "test")
public class Claim implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "idClaim", unique = true, nullable = false)
private Integer idClaim;
#OneToOne(mappedBy = "claim", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#JsonManagedReference
private ClaimReturnInfo claimReturnInfo;
#Column(name = "notes")
private String notes;
// Getters and setters
}
#Entity
#Table(name = "claim_returninfo", schema = "test")
public class ClaimReturnInfo implements Serializable {
#Id
#Column(name = "Claim_idClaim")
private Integer id;
#MapsId("Claim_idClaim")
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "Claim_idClaim")
#JsonBackReference
private Claim claim;
#Column(name = "description")
private String description;
// Getters and setters
}
ClaimReturnInfo Id is not autogenerated because we want to propagate the Id from its parent (Claim). We are not able to do this automatically and we are getting this error: ids for this class must be manually assigned before calling save() when 'cascade' is executed in ClaimReturnInfo .
Is it possible to map Claim Id into ClaimReturnInfo Id or should we do this manually?
Even if we set this ID manually on claimReturnInfo and we can perform updates, we still get this error when trying to create a new Claim:
// POST -> claimRepository.save() -> Error
{
"notes": "Some test notes on a new claim",
"claimReturnInfo": {
"description": "Test description for a new claimReturnInfo"
}
}
In the ServiceImplemetation:
#Override
#Transactional
public Claim save(Claim claim) throws Exception {
if(null != claim.getClaimReturnInfo()) {
claim.getClaimReturnInfo().setId(claim.getIdClaim());
}
Claim claimSaved = claimRepository.save(claim);
return claimSaved;
}
I have tried using the following mappings and from your comments it was apparent that Json object is populated correctly.
I have noticed that the annotation #MapsId is the culprit.If you check the documentation of #MapsId annotation it says
Blockquote
The name of the attribute within the composite key
* to which the relationship attribute corresponds. If not
* supplied, the relationship maps the entity's primary
* key
Blockquote
If you change #MapsId("Claim_idClaim") to #MapsId it will start persisting your entities.
import javax.persistence.*;
#Entity
#Table(name = "CLAIM")
public class Claim {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "idClaim", unique = true, nullable = false)
private Long idClaim;
#Column(name = "notes")
private String notes;
#OneToOne(mappedBy = "claim", cascade = CascadeType.ALL, optional = false)
private ClaimReturnInfo claimReturnInfo;
public Long getIdClaim() {
return idClaim;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public ClaimReturnInfo getClaimReturnInfo() {
return claimReturnInfo;
}
public void setClaimReturnInfo(ClaimReturnInfo claimReturnInfo) {
if (claimReturnInfo == null) {
if (this.claimReturnInfo != null) {
this.claimReturnInfo.setClaim(null);
}
} else {
claimReturnInfo.setClaim(this);
}
this.claimReturnInfo = claimReturnInfo;
}
}
package com.hiber.hiberelations;
import javax.persistence.*;
#Entity
#Table(name = "CLAIM_RETURN_INFO")
public class ClaimReturnInfo {
#Id
#Column(name = "Claim_idClaim")
private Long childId;
#Column(name = "DESCRIPTION")
private String description;
#MapsId
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "Claim_idClaim")
private Claim claim;
public Long getChildId() {
return childId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Claim getClaim() {
return this.claim;
}
public void setClaim(Claim claim) {
this.claim = claim;
}
}

hibernate: deleting entity in a one to many relationship

I have a following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`spindledb`.`section`, CONSTRAINT `FK_ftoru9cp83n512p9is8x3vo53` FOREIGN KEY (`scenario_id`) REFERENCES `scenario` (`scenario_id`))
Here are my classes:
Scenario:
#Entity
#Table(name = "scenario")
public class Scenario {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "scenario_id")
private int id;
#Column(name = "title", nullable = false)
private String title;
#NotNull
#DateTimeFormat(pattern = "dd/MM/yyyy")
#Column(name = "creation_date", nullable = false)
#Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
private LocalDate creationDate;
#ManyToOne
#LazyCollection(LazyCollectionOption.FALSE)
#JoinColumn(name = "id", nullable = false)
private User user;
#OneToMany(mappedBy = "scenario", orphanRemoval = true)
#LazyCollection(LazyCollectionOption.FALSE)
private Set<Plot> plotList = new HashSet<Plot>();
#OneToMany(mappedBy = "scenario", orphanRemoval = true)
#LazyCollection(LazyCollectionOption.FALSE)
private Set<Character> characterList = new HashSet<Character>();
#OneToMany(mappedBy = "scenario", cascade=CascadeType.ALL, orphanRemoval = true)
#LazyCollection(LazyCollectionOption.FALSE)
#OrderBy("sequence ASC")
private Set<Section> sectionList = new HashSet<Section>();
Section:
#Entity
#Table(name = "section")
public class Section {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "section_id")
private int id;
#Size(min = 4, max = 50)
#NotNull
#Column(name = "name")
private String name;
#NotNull
#Column(name = "type")
private String type = SectionType.TEXT.getSectionType();
#Column(name = "visibility")
private boolean visibility;
#NotNull
#Column(name = "sequence")
private int sequence;
#ManyToOne (cascade=CascadeType.ALL)
#LazyCollection(LazyCollectionOption.FALSE)
#JoinColumn(name = "scenario_id", nullable = false)
private Scenario scenario;
Controller:
#RequestMapping(value = { "/delete-{id}-scenario" }, method = RequestMethod.GET)
public String deleteScenario(#PathVariable int id) {
scenarioService.deleteScenarioById(id);
return "redirect:/home";
}
Scenario service:
#Service("scenarioService")
#Transactional
public class ScenarioServiceImpl implements ScenarioService {
#Autowired
private ScenarioDao dao;
#Override
public Scenario findById(int id) {
return dao.findById(id);
}
#Override
public void saveScenario(Scenario scenario) {
dao.saveScenario(scenario);
}
public void updateScenario(Scenario scenario) {
Scenario entity = dao.findById(scenario.getId());
if(entity!=null){
entity.setTitle(scenario.getTitle());
entity.setCreationDate(scenario.getCreationDate());
}
}
#Override
public void deleteScenarioById(int id) {
dao.deleteScenarioById(id);
}
Dao
#Repository("scenarioDao")
public class ScenarioDaoImpl extends AbstractDao<Integer, Scenario> implements ScenarioDao {
#Override
public Scenario findById(int id) {
return getByKey(id);
}
#Override
public void saveScenario(Scenario scenario) {
persist(scenario);
}
#Override
public void deleteScenarioById(int id) {
Query query = getSession().createSQLQuery("delete from scenario where id = :id");
query.setString("id", ""+id);
query.executeUpdate();
}
I understand that the problem is that there may be a Section that can not exist without scenario. Right now however section table in database is empty and I still can't remove Scenario. Thanks for advice
Deleting an entity via Query would bypass any Cascade settings you put via annotation.
I would suggest find the entity first by id, then delete the entity object:
Object scenario = session.load(Scenario.class, id);
if (scenario != null) {
session.delete(scenario);
}
use cascade=CascadeType.ALL with all #ManyToOne relations in class Scenario because if you are going to delete any Scenario from database it must not be referenced any where in data base.
the other way to delete is.
Serializable id = new Long(1); //your id
Object persistentInstance = session.load(Scenario.class, id);
if (persistentInstance != null) {
session.delete(persistentInstance);
}

JPQL manyToMany unidirectional query

I have been looking the forums and everywhere for a unidirectional manyToMany query. I find many examples but i can't really adjust them to my needs :(
I have 2 entity classes (Anime & User)
Anime {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "anime_id")
private Integer id;
//Other fields etc.
}
User {
#Id
#ValidUsername
#Column(name = "user_id")
private String username;
#ManyToMany()
#JoinTable(name = "users_animes",
joinColumns = #JoinColumn(name = "anime_id", referencedColumnName = "anime_id"),
inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "user_id"))
private final List<Anime> animes = new ArrayList<>();
}
Anime simply holds the data from the anime.
User holds the username etc of the user and a list of anime that he subscribed to.
Now i'm trying to find a query that will let me get all the animes in that list.
It's mapped in a table as "users_animes".
Would be a huge help since i'm fairly new to JPQL.
Thanks!
Here's a simple example. Let's assume that we have Country Entity, which can have multiple Citizens:
#Entity
#Table (name = "countries")
public class Country implements Serializable {
#Id
#Column (name = "coun_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column (name = "country_name")
private String countryName;
#ManyToMany (cascade = CascadeType.ALL)
#JoinTable (
name = "citizen_country",
joinColumns = #JoinColumn (name = "country_id", referencedColumnName = "coun_id"),
inverseJoinColumns = #JoinColumn (name = "citizen_id", referencedColumnName = "cit_id")
)
private List<Citizen> citizens;
public Country() {}
public Country(String countryName) {
this.countryName = countryName;
}
//getters and setters
}
And Citizen, which can belong to multiple Countries:
#Entity
#Table (name = "citizens")
public class Citizen implements Serializable {
#Id
#Column (name = "cit_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column (name = "country_name")
private String citizenName;
public Citizen() {}
public Citizen(String citizenName) {
this.citizenName = citizenName;
}
public void setId(Long id) { this.id = id; }
public Long getId() { return id; }
public void setCitizenName(String citizenName) { this.citizenName = citizenName; }
public String getCitizenName() { return citizenName; }
//getters and setters
}
It's unidirectional, just as you wanted it to be. Therefore, Citizen Entity is unaware of Country, so you can't get directly information to which Country a certain Citizen belongs. However, you can retrieve info about which Citizens belong to a certain Country.
Going further, you can populate your tables:
Citizen citizen1 = new Citizen("Smith");
Citizen citizen2 = new Citizen("Kowalski");
Citizen citizen3 = new Citizen("Muller");
dataAccess.saveCitizen(citizen1);
dataAccess.saveCitizen(citizen2);
dataAccess.saveCitizen(citizen3);
// now let's fetch them from DB, along with their other properties (only id in this case)
citizen1 = dataAccess.getCitizenByName("Smith");
citizen2 = dataAccess.getCitizenByName("Kowalski");
citizen3 = dataAccess.getCitizenByName("Muller");
Country country1 = new Country("Foo");
Country country2 = new Country("Bar");
// create lists to hold citizens for each country
List<Citizen> citizenList1 = new ArrayList();
List<Citizen> citizenList2 = new ArrayList();
// add elements to the lists
citizenList1.add(citizen1);
citizenList1.add(citizen2);
citizenList2.add(citizen2);
citizenList2.add(citizen3);
//assign lists of citizens to each country
country1.setCitizens(citizenList1);
country2.setCitizens(citizenList2);
//save data in DB
dataAccess.saveCountry(country1);
dataAccess.saveCountry(country2);
//fetch list of all persisted countries (associated Citizens will come along)
countries = dataAccess.getAllCountries();
And finally:
#Stateless
public class DataAccess {
#PersistenceContext
EntityManager em;
public void saveCountry(Country country) {
em.persist(country);
}
public void saveCitizen(Citizen citizen) {
em.persist(citizen);
}
public Citizen getCitizenByName(String name) {
Query query = em.createQuery("SELECT c FROM Citizen c WHERE c.citizenName = :name");
query.setParameter("name", name);
return (Citizen) query.getSingleResult();
}
public List<Country> getAllCountries() {
Query query = em.createQuery("SELECT c FROM Country c");
return (List<Country>) query.getResultList();
}
}

Categories