JPA - Error saving entities in OneToOne relationship - java

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!

Related

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 JPA - Implement unique constraints in code - Tx isolation level

I need to implement unique constrain validation using spring data jpa, and i know it can be done with jpa annotations but i need to do it manually.
I have a class (entity) User like this:
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String email;
private Date createdAt;
}
When i create a new User i have to validate that no other user with identical username or password exists.
I know i need to do something like this (more or less):
public interface UserRepository extends JpaRepository<User, Long> {
public List<User> findByUsernameOrEmail(String username, String email);
}
public class UserService {
#Autowired
private UserRepository userRepository;
#Transactional
public User create(User user) {
List<User> users = this.userRepository.findByUsernameOrEmail(user.getUsername(), user.getEmail());
if (users.size() > 0)
throw new RuntimeException("No repeated username or email allowed");
User newUser = this.userRepository.save(user);
return newUser;
}
}
My question is: what type of transaction isolation level should i use ?
Is it REPETEABLE_READ enough ? I need to avoid duplicated users

AbstractClass InstanceOf specificClass not working

Hi have a strange error.
I have always used the instanceof for see the specific object of an abstract class,
But not run and I don't understand why.
I work for a Java project created with spring boot (1.5.16.RELEASE), is a maven project, with JAVA8 and for my MySql db use querydsl-jpa.
I have an abstractClass for define the user for login.A single user can have only one role, and I have create an AbstractUser class:
#Entity
#Audited
#Data
#DiscriminatorColumn(name="dType", discriminatorType = DiscriminatorType.STRING)
public abstract class AbstractUser extends AbstractDomainAudit{
private static final long serialVersionUID = 7060362023055663647L;
#Column(updatable=false, insertable=false)
private String dType;
#JsonIgnore
private String password;
#JsonIgnore
#Transient
private String newPassword;
#JsonIgnore
#Transient
private String confirmNewPassword;
#Column(nullable=false,unique=true, updatable=false)
private String userName;
private boolean able;
private String name;
#Email
private String email;
}
One of my extender classes is the follow:
#Entity
#Audited
#Data
#DiscriminatorValue(value = "Admin")
public class Administrators extends AbstractUser implements Serializable{
/**
*
*/
private static final long serialVersionUID = 7061564146290031007L;
}
But when in my controller, I try to define the instanceof, not run:
#GetMapping(value="/{id}", params="form")
public String updateForm(#PathVariable(value="id") final Long id, final Model uiModel) {
AbstractUser user = userService.findById(id);
if(user instanceof Administrators) {
Administrators u = (Administrators) user;
uiModel.addAttribute(USER, u);
}else if (user instanceof Commercial){
Commercial ut = (Commercial) user;
uiModel.addAttribute(USER, ut);
}
return UPDATE_PAGE;
}
My condition is ignored.
And I don't understand why. Can anyone help me?
JPA-annotated Classes might be proxied at runtime. Try logging your instance's class
user.getClass().getCanonicalName()
Have a look here for example: JPA, inheritance and instanceof

Spring Entity inheritance, getting null from repository when using findOne

In spring, JPA, i want to extend my User model into More more User types.
And i like it to be in a single table.
i believe i did my example right, but some how i am getting null for the findOne method
can you help my please, i can't figure out what is wrong
here is my code (I have more users types, but the basic is not working):
User:
#Entity
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
#DiscriminatorColumn(name="user_type")
public abstract class User {
public LicenseType license = LicenseType.FREE;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long id;
public String username;
public String password;
#OneToOne(fetch = FetchType.LAZY)
public Country country;
#OneToOne(fetch = FetchType.LAZY)
public UserPhoto profilePhoto;
....
....
}
UserBasic
#Entity
#DiscriminatorValue("UserBasic")
public class UserBasic extends User {
}
UserRepository
#NoRepositoryBean
public interface UserRepository<T extends User> extends Repository<T, Long>{
T findOne(Long id);
T findByFacebookId(Long facebookId);
}
UserBasicRepository
public interface UserBasicRepository extends UserRepository<UserBasic>{
#Modifying
#Query(value="UPDATE User SET someField = now() WHERE id = ?1", nativeQuery= true)
long updateSomeField(Long userId);
}
UserService
public class UserServiceImpl implements UserService {
#Autowired
private UserBasicRepository userBasicRepository;
#Override
public User findOne(Long userId) {
/**
*
* THIS WILL RETURN NULL
* vv
*
*/
return userBasicRepository.findOne(userId);
}
....
....
}
QUESTION UPDATE
now after the comment, i printed the query and i think i see the problem
and userbasic0_.user_type='UserBasic'
i just wanted inheritance, didn't ask for extra comment. how can i remove this condition.
Hibernate:
select
userbasic0_.id as id2_8_0_,
userbasic0_.password as passwor12_8_0_,
userbasic0_.username as usernam15_8_0_,
.....
from
User userbasic0_
where
userbasic0_.id=?
and userbasic0_.user_type='UserBasic'

Hibernate envers add audit column (username)

Requirement is to add username column for every audit entry.
After googling a lot and going through the enverse docs I figured out how to implement it. I have implemented it as follows:
Implemented a revision listener:
#Configurable
public class UserRevisionListener implements RevisionListener
{
public void newRevision(Object revisionEntity)
{
UserRevEntity revision = (UserRevEntity) revisionEntity;
String username = "";
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
Object principal = auth.getPrincipal();
if (principal != null && principal instanceof DepotUser)
username = ((DepotUser) principal).getUsername();
revision.setUsername(username);
}
}
Create an entity class:
#Entity
#Table(name = "USER_REV_ENTITY")
#RevisionEntity(UserRevisionListener.class)
public class UserRevEntity extends DefaultRevisionEntity
{
private static final long serialVersionUID = 1L;
private String username;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
}
I have referred a link that has an example of the same "Thinking in Software ".
Default auditing is working fine.
Firstly, the table for the audit revision with the new username column should get auto generated (I think!). Its not getting generated.
So, I tried by creating the table and adding the table mapping in UserRevEntity. But no luck.
Can anyone help my identifying the issue?
Thanks, in advance.
#MappedSuperclass
public class DefaultRevisionEntity implements Serializable {
#Id
#GeneratedValue
#RevisionNumber
private int id;
#RevisionTimestamp
private long timestamp;
// ... rest of the class body here....
This is DefaultRevisionEntity, which has already declared with id & timestamp.
For your new column username, it has to be annotated as #Column in UserRevEntity as given below.
#Column(name="username")
private String username;

Categories