Way to save a #Requestbody object directly to database - java

I have a UserEntity object from a registration form. Here is the object:
#Entity
#Table(name = "users")
public class UserEntity {
private String name;
private String email;
private String password;
And in my controller:
#PostMapping("/register")
public void register(#RequestBody UserEntity user) {
user.save(); // how to do this?
}
Is there a simple way to save this object directly to my database? What would be the most direct way to do that above?

You can by using Repository, just need to create Repository interface.
some thing like that
public interface UserEntityRepositoryextends PagingAndSortingRepository<UserEntity ,Long>
and inject the Repository
#Autowired
UserEntityRepository userEntityRepository ;
then save
userEntityRepository.save(user)

Related

How to get contents of multiple related tables with Hibernate Spring?

I've created a new project in Java using Springboot. I've followed a tutorial online to create an Entity called User which I have stored in a PostgreSQL database using Hibernate and a schema file. The User file is shown below. I'm now trying to create a Controller that will allow me to get all the data of a User from the database, but Hibernate creates another table to store the #ElementCollection. This means when I use the default queries, I get the User, but the roles column is not able to be accessed since it has stored in another table. It is stored as a jsonb. How can I get the entire User data?
User class
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private #Getter long id;
private #Getter #Setter String email;
private #Getter #Setter String encryptedPassword;
private #Getter String accessToken;
private #Getter float netWorth;
#ElementCollection
private #Getter #Setter Set<String> roles;
... (Other class functions)
}
Repository and Service class
#Repository
public interface UserRepository extends CrudRepository<User, Long> {}
#Service
public class UserService {
#Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository repository;
public Optional<User> findOne(long id) {
return repository.findById(id);
}
}
Controller Class
#RestController
#RequestMapping(path="/user") // This means URL's start with /user (after Application path)
public class UserController {
#Autowired
private UserService userService;
#GetMapping(path="/get/{id}") // Map ONLY GET Requests
public ResponseEntity<User> getUser(#PathVariable("id") long id) {
// #ResponseBody means the returned String is the response, not a view name
// #RequestParam means it is a parameter from the GET or POST request
try {
Optional<User> savedUser = userService.findOne(id);
if (!savedUser.isPresent()) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "User with ID: " + id + " not found.");
}
return new ResponseEntity<>(savedUser.get(), HttpStatus.FOUND);
} catch (Exception e) {
throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR, "Error in retrieving user.", e);
}
}
}

Child and Parent Database - Spring Boot

I have developed two tables in Spring Boot, User and UserMeta. User is the parent and UserMeta is the child table. The foreign-key is user_id. I may be looking at it the wrong way, but I want to be able to first create an entity of User. Then, I want to create an entity of UserMeta. Simply UserMeta should contain additional data to User.
However, when first creating a User and then a UserMeta entity, I get e new User entity (ending up with two User entities and one UserMeta entity.)
The problem I think is that I create a UserMeta object with a User, since I want to have a relationship between User and UserMeta. But if I want to be able to first create a User and then a UserMeta, should I simply ignore a foreign-key? Or, does it exists another way of creating a UserMeta entity without creating a new User?
User
public class User {
#Id
#SequenceGenerator(name = "user_sequence", sequenceName = "user_sequence", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_sequence")
//#OneToOne(optional=false)
private Long userId;
private String username;
private String password;
private String email;
#OneToOne(mappedBy = "user")
private UserMeta userMeta;
public User(String username, String email, String password) {
this.username = username;
this.email = email;
this.password = password;
}
}
UserMeta
public class UserMeta {
#Id
#SequenceGenerator(name = "user_meta_sequence", sequenceName = "user_meta_sequence", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_meta_sequence")
private Long userMeta_Id;
private String lastname;
private int age;
#OneToOne(
cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
optional = false
)
#JoinColumn(
name = "user_Id",
referencedColumnName="userId"
)
private User user;
public UserMeta(String lastName, int age, User user){
this.lastname = lastName;
this.age = age;
this.user = user;
}
}
UserRepository
public interface UserRepository extends CrudRepository<User, Long> {
}
UserService
public interface UserService {
User saveUser(User user);
}
UserServiceImpl
#Service
public class UserServiceImpl implements UserService {
private UserRepository userRepository;
public UserServiceImpl(UserRepository userRepository) {
super();
this.userRepository = userRepository;
}
#Override
public User saveUser(User user) {
// TODO Auto-generated method stub
return this.userRepository.save(user);
}
UserController
#RestController
public class UserController {
private UserService userService;
public UserController(UserService userService) {
super();
this.userService = userService;
}
#PostMapping("/user")
public ResponseEntity<User> saveUser(#RequestBody User user) {
return new ResponseEntity<User>(userService.saveUser(user), HttpStatus.CREATED);
}
}
UserMetaRepository
public interface UserMetaRepository extends CrudRepository<UserMeta, Long> {
}
UserMetaService
public interface UserMetaService {
UserMeta saveUserMeta(UserMeta userMeta);
}
UserMetaServiceImpl
#Service
public class UserMetaServiceImpl implements UserMetaService{
private UserMetaRepository userMetaRepo;
public UserMetaServiceImpl(UserMetaRepository userMetaRepo) {
super();
this.userMetaRepo = userMetaRepo;
}
#Override
public UserMeta saveUserMeta(UserMeta userMeta) {
return this.userMetaRepo.save(userMeta);
}
}
UserMetaController
#RestController
public class UserMetaController {
public UserMetaService userMetaService;
public UserMetaController(UserMetaService service) {
super();
this.userMetaService = service;
}
#PostMapping("/userMeta")
public ResponseEntity<UserMeta> saveUserMeta(#RequestBody UserMeta userMeta) {
return new ResponseEntity<UserMeta>(this.userMetaService.saveUserMeta(userMeta), HttpStatus.CREATED);
}
}
you should use this constructor in the User class,
public User(String username, String email, String password, UserMeta userMeta) {
this.username = username;
this.email = email;
this.password = password;
this.userMeta = userMeta;
}
now when you save your user the user Meta will be added to your UserMeta table,
If you want to add a user Meta to an existing user you will only need to set the userMeta and save it with a simple userRepository.save(theUpdatedUser)
you can also create userMeta seperately with your code above, and if you want to assign it to a user already in data base or not you can allows use the power of spring data and use simple userRepository.save(userWithTheAssignedMeta)
the same logic applies the other way for metaUser.
The problem here is that your UserMetadata creation logic is using incomplete JSON:
{ "lastName":"foo", "age":1, "user":{ "username":"foo", "password":"bar", "email":"foo-bar" } }
Within this, the problem is the 'user' has all the data, duplicating what was already created the the database, but does not identify it. Since the mapping has cascade.ALL set on it, Spring/JPA will persist the UserMetadata and find this User instance that doesn't have identity, so persist it - giving it identity from the sequence.
There are a few ways you might correct this. First and easiest is to send the User ID in the json from the previously created instance:
{ "lastName":"foo", "age":1, "user":{ "userId":1, "username":"foo", "password":"bar", "email":"foo-bar" } }
This will allow Spring/JPA to recognize the user's identity and merge it and the data provided into the database. It means though that you must send complete data for the User - it will push incomplete data into the DB.
If that is a concern, you can change the cascade options. You may not want cascading persist/merge at all on this relationship, and I suspect when you delete userMetadata you don't really want to delete the User instance, so I think this might have been done incorrectly (maybe put it on the user->UserMetadata relationship instead?). If you remove the cascade settings, spring/JPA will let you just pass in JSON with the USER id specified, as this gives it enough to set the fk:
{ "lastName":"foo", "age":1, "user":{ "userId":1} }

Spring CrudRepository failing to return JSON from valid object while using Lombok #Data Annotation

I'm attempting to get a basic spring application up and running from a tutorial with mysql but I'm running into issues with the GetMapping and PostMapping annotations, here is what I have so far:
I've manually added a user into my table with id=0, name="test" via mysql workbench and verified that the data is in fact there.
I was forced to do it via mysql workbench because attempting to post with curl results in no change
Attempting to call localhost/api/user/1 when there is no user with that ID gives me a 404, which is what is expected while calling it with localhost/api/user/0 gives me an http ok code 200, I'm just not actually receiving a populated JSON object.
Debugging the application on the getUser (using url localhost/api/user/0) shows a user in memory with id=0, name="test" however once the return ResponseBody.ok()... is completed the response I get via browser AND curl is still {}, an empty JSON object
User.Java
#Data
#Builder
#Entity
#Table(name = "user")
public class User {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
}
UserRepository.java
#Repository
public interface UserRepository extends CrudRepository<User, Long> {}
UserController.java
#RestController
#RequestMapping("/api/user")
public class UserController {
#Autowired
private UserRepository userRepository;
#GetMapping
public List<User> getAllUsers(){
return (List<User>) userRepository.findAll();
}
#GetMapping("/{id}")
public ResponseEntity<User> getUser(#PathVariable(value="id") long id){
Optional<User> user = userRepository.findById(id);
if(user.isPresent()) {
return ResponseEntity.ok().body(user.get());
}else {
return ResponseEntity.notFound().build();
}
}
#PostMapping
public User saveUser(#Validated #RequestBody User user) {
return userRepository.save(user);
}
}
So to summarize I'm receiving an empty JSON object from my GetMapping AND PostMapping annotated calls, even while I have valid data in the table (or have submitted valid post data) when I should be receiving back a json object with {id:0, name:"test"}, does anyone know what might be happening?
Edit:
It appears as though lombok is not actually injecting getters and setters when I run my application, changing my user.java to
#Entity
#Table(name = "user")
public class User {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private long id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return this.id;
}
}
and recalling localhost/api/user/0 returns the expected json object, Why isnt lombok injecting these functions into my code properly with the #Data annotation? (eclipse)
So this was happening because lombok was not injecting getters/setters and the ResponseEntity.ok.body(user) could not retrieve the values within the User class.
Apparently according to this stackoverflow answer when using eclipse lombok requires a plugin to properly inject the methods.
So this problem can be fixed by either manually defining the appropriate getters/setters needed or by installing the plugin for eclipse.

JPA - Error saving entities in OneToOne relationship

I am doing a project for my studies using Spring, Maven, Tomcat, mySQL. I would like to create a website where users can login and update their settings and based on these settings they generate stuff.
At the moment the login is working fine and new users can be registered and saved to the database. Now I created a new entity and made a one to one reletaionship between the two tables - one is the table of the login details like password and username and the other one contains the settings of this user. The issue I am facing:
I have some textfield and combobox in the UI with vaadin - I populate the fields and click save
A binder passes these settings to a service that saves the object
It gets the currently logged in user and loads it from the database
When SQL tries to save the objects it throws error:
Caused by: java.sql.SQLException: Field 'user_login_id' doesn't have a
default value
Here are the entities:
#Entity
#Table(name = "USERLOGIN")
public class UserLogin implements UserDetails {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "id")
private Integer id;
#Column(name = "username")
private String username;
#Column(name = "password")
private String password;
#OneToOne(mappedBy = "userlogin")
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
**Other getters and setters**
}
#Entity
#Table(name = "USER")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue
private Integer id;
#Column(name = "meal")
private Integer meal;
#OneToOne
#JoinColumn(name = "userlogin_id")
private UserLogin userlogin;
public UserLogin getUserLogin() {
return userlogin;
}
public void setUserLogin(UserLogin userLogin) {
this.userlogin = userLogin;
userLogin.setUser(this);
**Other getters and setters**
}
The service that saves the settings:
#Service
public class AddUserServiceImpl implements AddUserService{
#Autowired
private UserRepository userRepository;
#Autowired
private CurrentUserService currentUserService;
public void saveUser(User userDAO) {
User user = new User();
user.setMeal(userDAO.getMeal());
user.setUserLogin(currentUserService.getCurrentUser());
userRepository.save(user);
}
}
The repository extends the JPArepository:
#Repository
public interface UserRepository extends JpaRepository<User, Integer>{
}
And finally the service and the repository that loads the currently logged in user:
#Service
public class CurrentUserServiceImpl implements CurrentUserService {
#Autowired
UserLoginRepository userLoginRepository;
public String getCurrentUsername() {
return SecurityContextHolder.getContext().getAuthentication().getName();
}
public UserLogin getCurrentUser() {
return userLoginRepository.findByUserName(getCurrentUsername());
}
}
#Repository
public interface UserLoginRepository extends JpaRepository<UserLogin,
Integer> {
#Query("select u from UserLogin u where u.username=:username")
UserLogin findByUserName( #Param("username") String username);
}
Any help would be appreciated I am really new to this topic. So the main goal is that I want to have a table that stores properties for the currently logged in user - these settings should be able to be updated any time. Is there any best practice for this?
Thank you!

SpringData MongoDB findBy Nested Property (non ID) Using DBRef

Using SpringData MongoDB (spring-data-mongodb 1.9.1.RELEASE) I am needing to query a User based on a User's Role linked by #DBRef.
User
#Document(collection = "user")
public class User {
private String userName;
private boolean isActive;
#DBRef
private List<Role> roles;
}
Role
#Document(collection = "role")
public class Role {
private String roleName;
private String description;
private long roleNum;
}
User Repository
#Repository
public interface UserRepository extends MongoRepository<User, String> {
public User findByUserName(String username);
#Query(value = "{'roles.$roleName' : ?0}")
public List<User> findByRolesRoleName(String roleName);
}
A question similar has been asked, but not answered. Makes me think that maybe this type of findBy is not supported.
This seems fairly straight forward, however, the results of findByRolesRoleName is always an empty list (size = 0).
Has anyone gotten a findBy for this type of relationship working properly?
It is not possible to query on non id properties of DBRef in MongoDB itself. Thus it is not possible to do so using Spring Data MongoDB.

Categories