M:N table does not update - java

I have m:n relationship between User and Document. I am creating Document object, getting List - setDocumentsForUsers() and then I am persisting that object. Problem is, that document is created in my database, but not M:N relationship. What am I doing wrong? I tried to call flush after persisting, but it did not help at all.
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(name = "first_name")
private String firstName;
private String surname;
private String email;
private String password;
#ManyToMany
#JoinTable(
name = "users_roles",
joinColumns = #JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(
name = "role_id", referencedColumnName = "id"))
private List<Role> roles;
#JsonIgnore
#ManyToMany
#JoinTable(
name="users_documents",
joinColumns = #JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(
name="document_id", referencedColumnName = "id"))
private List<Document> usersDocuments;
#OneToMany(mappedBy="user")
private List<Document> sharedDocuments;
public User() {
}
public User(String firstName, String surname, String email, String password) {
this.firstName = firstName;
this.surname = surname;
this.email = email;
this.password = password;
}
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public String getEmail() {
return email;
}
public void setEmail(String email){this.email = email;}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public List<Role> getRoles() {
return roles;
}
public List<Document> getUsersDocuments() {
return usersDocuments;
}
public void setUsersDocuments(List<Document> usersDocuments) {
this.usersDocuments = usersDocuments;
}
public List<Document> getSharedDocuments() {
return sharedDocuments;
}
public void setSharedDocuments(List<Document> sharedDocuments) {
this.sharedDocuments = sharedDocuments;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return getId() == user.getId();
}
#Override
public int hashCode() {
return Objects.hash(getId());
}
#Override
public String toString() {
return "User{" +
"firstName='" + firstName + '\'' +
", surname='" + surname + '\'' +
", email='" + email + '\'' +
", roles=" + roles +
'}';
}
}
My Document class:
#Entity
public class Document {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(unique = true)
private String name;
private String title;
private String description;
#Column(name = "resource_path")
private String resourcePath;
#Column(name = "upload_datetime", columnDefinition = "DATETIME")
#Temporal(TemporalType.TIMESTAMP)
private Date uploadDatetime;
#ManyToMany(mappedBy = "usersDocuments")
private List<User> documentsForUsers;
#ManyToOne
#JoinColumn(name="user_id", nullable=false)
private User user;
public Document() {
}
public Document(String title, String description){
this.title = title;
this.description = description;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getResourcePath() {
return resourcePath;
}
public void setResourcePath(String resourcePath) {
this.resourcePath = resourcePath;
}
#Override
public String toString() {
return "Document{" +
"id=" + id +
", title='" + title + '\'' +
", description='" + description + '\'' +
", resourcePath='" + resourcePath + '\'' +
", uploadDatetime=" + uploadDatetime + '\'' +
". user=" + user;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Document)) return false;
Document document = (Document) o;
return getId() == document.getId();
}
#Override
public int hashCode() {
return Objects.hash(getId());
}
public Date getUploadDatetime() {
return uploadDatetime;
}
public void setUploadDatetime(Date uploadDatetime) {
// Date startDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(uploadDatetime.toString());
this.uploadDatetime = uploadDatetime;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<User> getDocumentsForUsers() {
return documentsForUsers;
}
public void setDocumentsForUsers(List<User> documentsForUsers) {
this.documentsForUsers = documentsForUsers;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Perform this:
Optional<User> user = userService.getUserByEmail(createdBy);
Document document = new Document(title, desc);
document.setUploadDatetime(new Date());
document.setUser(user.get());
List<User> users = userService.getUsersByRoles(roles);
document.setDocumentsForUsers(users);
saveDocument(document);
#Override
public void saveDocument(Document document) {
entityManager.persist(document);
}

I think you need add the option to your #ManyToMany annotation: CascadeType.PERSIST.
By default, hibernate doesn't persists your relationship objects.
Try to use:
#ManyToMany(cascade = CascadeType.PERSIST)

Your class User is an Owner of relation #ManyToMany on
List<Document> usersDocuments;
as User has #JoinTable. Document class is an Inverse end.
If a Document is persisted then a data will be saved without relations. Because Inverse end cares only about itself but no relations.
By default you can persist a relation only from the owner side.
To be able to persist a relation from the inverse end then in your case as I tested:
In Document add cascade:
#ManyToMany(cascade = CascadeType.PERSIST, mappedBy = "usersDocuments")
private List<User> documentsForUsers;
and add a new code in your setter:
public void setDocumentsForUsers(List<User> documentsForUsers) {
if (documentsForUsers != null){
documentsForUsers.forEach(u -> {
u.getUsersDocuments().add(this); //here you should have not-null list u.getUsersDocuments()
});
}
this.documentsForUsers = documentsForUsers;
}
In User class add cascade:
#ManyToMany(cascade = CascadeType.PERSIST)
and
private List<Document> usersDocuments = new ArrayList<>();

Related

How to write this query in HQL?

I'm learning Hibernate and I'm having trouble writing queries against many-to-many tables. I need to translate this into the HQL
SELECT books.id,books.name,books.year,books.priceRent,books.priceSell,books.amount, author.full_name,genre.genre_name FROM books
INNER JOIN book_author ON books.id = book_author.book_id
INNER JOIN author ON book_author.author_id = author.author_id
INNER JOIN book_genre ON books.id = book_genre.genre_id
INNER JOIN genre ON book_genre.genre_id = genre.genre_id
I tried to write like this:
FROM Book b LEFT JOIN b.author LEFT JOIN b.genre
But it gives an error:
java.sql.SQLSyntaxErrorException: Unknown column 'genre3_.id' in 'on clause'
At the same time, I already have all the table entities
Here is Author Entity
#Entity
#Table(name = "author")
public class Author {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "author_id")
private Integer id;
#Column(name = "full_name")
private String name;
public Author(String name) {
this.name = name;
}
public Author() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Book> getBookList() {
return bookList;
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
}
#ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH})
#JoinTable(name = "book_author",joinColumns = #JoinColumn(name = "author_id"),inverseJoinColumns = #JoinColumn(name = "book_id"))
private List<Book> bookList;
#Override
public String toString() {
return "Author{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Here is Genre Entity
#Entity
#Table(name = "genre")
public class Genre {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "genre_id")
private Integer id;
#Column(name = "genre_name")
private String genre;
#ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH})
#JoinTable(name = "book_genre",joinColumns = #JoinColumn(name = "genre_id"),inverseJoinColumns = #JoinColumn(name = "book_id"))
private List<Book> bookList;
public Genre(){}
public List<Book> getBookList() {
return bookList;
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
}
public Genre(String genre) {
this.genre = genre;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
#Override
public String toString() {
return "Genre{" +
"id=" + id +
", genre='" + genre + '\'' +
'}';
}
}
Here is Book Entity
#Entity
#Table(name = "books")
public class Book {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Integer id;
#Column(name = "name")
private String name;
#Column(name = "year")
private String year;
#Column(name = "priceRent")
private Integer priceRent;
#Column(name = "priceSell")
private Integer priceSell;
#Column(name = "amount")
private Integer amount;
#ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH})
#JoinTable(name = "book_author",
joinColumns = #JoinColumn(name = "id"),
inverseJoinColumns = #JoinColumn(name = "author_id"))
private List<Author> authorList;
#ManyToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH})
#JoinTable(name = "book_genre",
joinColumns = #JoinColumn(name = "id"),
inverseJoinColumns = #JoinColumn(name = "genre_id"))
private List<Genre> genreList;
public Book() {
}
public Book(String name, String year, Integer priceRent, Integer priceSell, Integer amount) {
this.name = name;
this.year = year;
this.priceRent = priceRent;
this.priceSell = priceSell;
this.amount = amount;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public Integer getPriceRent() {
return priceRent;
}
public void setPriceRent(Integer priceRent) {
this.priceRent = priceRent;
}
public Integer getPriceSell() {
return priceSell;
}
public void setPriceSell(Integer priceSell) {
this.priceSell = priceSell;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public List<Author> getAuthorList() {
return authorList;
}
public void setAuthorList(List<Author> authorList) {
this.authorList = authorList;
}
public List<Genre> getGenreList() {
return genreList;
}
public void setGenreList(List<Genre> genreList) {
this.genreList = genreList;
}
#Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", year=" + year +
", priceRent=" + priceRent +
", priceSell=" + priceSell +
", amount=" + amount +
'}';
}
}

H2 ERROR: Referential integrity constraint violation - Hibernate one to one mapping

I have two entity (Instructor, InstructorDetail) which have one to one relation.
instructor_detail_id of Instructor entity has a foreign key to the id column of InstructorDetail. So, according to my requirement, when an Instructor is deleted, corresponding instructorDetail also needs to be deleted, but not the vice versa. Now, when I am trying to delete an instructorDetail, it is throwing the referencial integrity constraint error.
Note: I am using H2 db.
Following are the code snippets.
Instructor -
import javax.persistence.*;
#Table(name="instructor")
#Entity
public class Instructor implements IdentityMarker<Integer>{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="instructor_id")
private int id;
#Column(name="name")
private String name;
#Column(name="email")
private String email;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "instructor_detail_id")//, referencedColumnName = "id")
private InstructorDetail instructorDetail;
public Instructor(){
}
public Instructor(String name, String email) {
this.name = name;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public InstructorDetail getInstructorDetail() {
return instructorDetail;
}
public void setInstructorDetail(InstructorDetail instructorDetail) {
this.instructorDetail = instructorDetail;
}
public Integer getReference() {
return id;
}
public void setReference(Integer id){ this.id = id;}
#Override
public String toString() {
return "Instructor{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", instructorDetail=" + instructorDetail +
'}';
}
}
InstructorDetail -
import javax.persistence.*;
#Table(name="instructor_detail")
#Entity
public class InstructorDetail implements IdentityMarker<Integer>{
#Id
#Column(name="id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name="youtube_link")
private String youtubeLink;
#Column(name="hobby")
private String hobby;
#OneToOne(cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
},
mappedBy = "instructorDetail")
// this bi-directional relationship enables us to get the instructor when an instructionDetail is loaded.
private Instructor instructor;
public InstructorDetail(){
}
public InstructorDetail(String youtubeLink, String hobby){
this.youtubeLink = youtubeLink;
this.hobby = hobby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getYoutubeLink() {
return youtubeLink;
}
public void setYoutubeLink(String youtubeLink) {
this.youtubeLink = youtubeLink;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Instructor getInstructor() {
return instructor;
}
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
}
public Integer getReference() {
return id;
}
public void setReference(Integer id){ this.id = id;}
#Override
public String toString() {
return "InstructorDetail{" +
"id=" + id +
", youtubeLink='" + youtubeLink + '\'' +
", hobby='" + hobby + '\''+
'}';
}
#PreRemove
private void preRemove() {
System.out.println("pre remove call");
instructor.setInstructorDetail(null);
}
}
Following is the client code
private static void deleteInstructorDetail(){
InstructorDetailDao instructorDetailDao = new InstructorDetailDaoImpl();
InstructorDetail instructorDetail = instructorDetailDao.getInstructorDetail(2);
Instructor instructor = instructorDetail.getInstructor();
System.out.println("Instructor: " + instructor);
boolean b = instructorDetailDao.deleteInstructorDetail(instructorDetail);
assert b == true: "InstructorDetail is not deleted!";
System.out.println("Trying to load Instructor.. It should be deleted!");
InstructorDao instructorDao = new InstructorDaoImpl();
instructor = instructorDao.getInstructor(instructor.getId());
assert instructor != null: "Instructor also got deleted!";
}
Any help would be appreciated! Thanks in advance.

recursive JSON java

When starting the server, I get stuck.
Data
Apparently it's a matter of models, but it's not clear what.
Connection to the base via JPA.
User
#Entity
#Table (name = "User")
public class User implements UserDetails {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(name = "name", nullable = false)
private String name;
#Column(name = "surname")
private String surname;
#Column(name = "password")
private String password;
#ManyToMany(fetch = FetchType.EAGER, /*cascade = CascadeType.ALL,*/
targetEntity = Role.class)
#JoinTable(name = "user_role",
joinColumns = {#JoinColumn(name = "user_id")},
inverseJoinColumns = {#JoinColumn(name = "role_id")})
private Set<Role> roles;
public User() {
}
public User(String name, String surname) {
this.name = name;
this.surname = surname;
}
public User(String name, String surname, String password, Set<Role> roles) {
this.name = name;
this.surname = surname;
this.password = password;
this.roles = roles;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setPassword(String password) {
this.password = password;
}
public String getPass() {
return password;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
#Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", surname='" + surname + '\'' +
//", role='" + roles.toString() + '\'' +
'}';
}
#Override
public Collection<? extends GrantedAuthority> getAuthorities() {
Set<Role> roleList = new HashSet<>();
roleList.addAll(roles);
return roleList;
}
#Override
public String getPassword() {
return password;
}
#Override
public String getUsername() {
return name;
}
#Override
public boolean isAccountNonExpired() {
return true;
}
#Override
public boolean isAccountNonLocked() {
return true;
}
#Override
public boolean isCredentialsNonExpired() {
return true;
}
#Override
public boolean isEnabled() {
return true;
}
}
Role
#Entity
#Table(name = "Role")
public class Role implements GrantedAuthority {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(name = "roleName", length = 20, nullable = false)
private String roleName;
//private String role;
#ManyToMany(fetch = FetchType.EAGER, /*mappedBy = "roles",*/ targetEntity =
User.class)
#JoinTable(name = "user_role",
joinColumns = {#JoinColumn(name = "role_id")},
inverseJoinColumns = {#JoinColumn(name = "user_id")})
private List<User> users;
public Role() {
}
public Role(String roleName) {
this.roleName = roleName;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Collection<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
#Override
public String toString() {
return "Role with id " + id + " and name " + roleName;
}
public String getAuthority() {
return getRoleName();
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Role role = (Role) o;
return Objects.equals(roleName, role.roleName);
}
#Override
public int hashCode() {
return Objects.hash(roleName);
}
}
I thought it was a cascade and FetchType. But everything seems to be correct.
I tried using toString to contact from the User to the Role, and vice versa. Also without result.
Thanks!
You have to use the annotation #JsonIgnore above the getter of one of the two class, let me explain:
when you call a user object it uses a role object, the called role object contains a user object, so it uses another user object and as you have an infinite loop user -> role -> user -> role -> user -> role .....
#JsonIgnore of the jackson api does when you call a user object, it will get it without a role, the call to the role will be done when you use the getRole ().
Hope this would be helpfull

OneToMany relationship error: Field 'XX' doesn't have a default value

I'm trying to create One-To-Many relationship between two objects but I got this error. I don't know how to mapping property ID from object MyUserAccount to object Book. ID is a String data received from Google (I'm doing Social Login in my project).
Error
PreparedStatementCallback; SQL [INSERT INTO Books(TENSACH, TACGIA, NHANXET, TINHTRANG, THELOAI, IMAGE, IMAGE_NAME) VALUES ( ?, ?, ?, ?, ?, ?, ?)]; Field 'ID' doesn't have a default value; nested exception is java.sql.SQLException: Field 'ID' doesn't have a default value
BookDao (How I save object Book into database)
public void save(Book book) {
// TODO Auto-generated method stub
KeyHolder keyHolder = new GeneratedKeyHolder();
String sql = "INSERT INTO Books(TENSACH, TACGIA, NHANXET, TINHTRANG, THELOAI, IMAGE, IMAGE_NAME) "
+ "VALUES ( :tensach, :tacgia, :nhanxet, :tinhtrang, :theloai, :image, :image_name)";
namedParameterJdbcTemplate.update(sql, getSqlParameterByModel(book), keyHolder);
book.setBook_ID(keyHolder.getKey().intValue());
}
private SqlParameterSource getSqlParameterByModel(Book book) {
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("book_id", book.getBook_ID());
paramSource.addValue("tensach", book.getTensach());
paramSource.addValue("tacgia", book.getTacgia());
paramSource.addValue("nhanxet", book.getNhanxet());
paramSource.addValue("tinhtrang", book.getTinhtrang());
paramSource.addValue("image", book.getImage());
paramSource.addValue("image_name", book.getImage_name());
paramSource.addValue("theloai", book.getTheloai());
return paramSource;
}
Model Book
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private Integer book_ID;
private String tensach;
private String tacgia;
private String nhanxet;
private String tinhtrang;
private String theloai;
private byte[] image;
private String image_name;
private String data;
private MyUserAccount myUserAccount;
public Book() {
}
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "ID", nullable = false)
public MyUserAccount getMyUserAccount() {
return this.myUserAccount;
}
public void setMyUserAccount(MyUserAccount myUserAccount) {
this.myUserAccount = myUserAccount;
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "book_id", unique = true, nullable = false)
public Integer getBook_ID() {
return book_ID;
}
#Column(name = "image_name")
public String getImage_name() {
return image_name;
}
#Column(name = "tensach", length = 50, nullable = true)
public String getTensach() {
return tensach;
}
#Lob
#Type(type = "org.hibernate.type.BinaryType")
#Column(name = "image", columnDefinition = "LONGBLOB", nullable = true)
public byte[] getImage() {
return image;
}
#Column(name = "tacgia", length = 50, nullable = true)
public String getTacgia() {
return tacgia;
}
#Column(name = "nhanxet", length = 100, nullable = true)
public String getNhanxet() {
return nhanxet;
}
#Column(name = "tinhtrang", length = 50, nullable = true)
public String getTinhtrang() {
return tinhtrang;
}
#Column(name = "theloai", length = 50, nullable = true)
public String getTheloai() {
return theloai;
}
#Column(name = "data", length = 16777215)
public String getData() {
return data;
}
public void setBook_ID(Integer book_ID) {
this.book_ID = book_ID;
}
public void setImage_name(String image_name) {
this.image_name = image_name;
}
public void setImage(byte[] image) {
this.image = image;
}
public void setTensach(String tensach) {
this.tensach = tensach;
}
public void setTacgia(String tacgia) {
this.tacgia = tacgia;
}
public void setNhanxet(String nhanxet) {
this.nhanxet = nhanxet;
}
public void setTinhtrang(String tinhtrang) {
this.tinhtrang = tinhtrang;
}
public void setTheloai(String theloai) {
this.theloai = theloai;
}
public void setData(String data) {
this.data = data;
}
#Override
public String toString() {
return "Book [book_ID=" + book_ID + ", tensach=" + tensach + ", tacgia=" + tacgia + ", nhanxet=" + nhanxet
+ ", tinhtrang=" + tinhtrang + ", theloai=" + theloai + ", image=" + Arrays.toString(image) + "]";
}
}
Model MyUserAccount.
public class MyUserAccount implements Serializable {
private static final long serialVersionUID = 1L;
public static final String ROLE_USER = "ROLE_USER";
private String id;
private String email;
private String userName;
private String firstName;
private String lastName;
private String password;
private String role;
private String enabled;
private List<Book> book = new ArrayList<Book>(0);
public MyUserAccount() {
}
#OneToMany(fetch = FetchType.LAZY, mappedBy = "myUserAccount")
public List<Book> getBook() {
return book;
}
public void setBook(List<Book> book) {
this.book = book;
}
public MyUserAccount(String id, String email, String userName, String firstName, //
String lastName, String password, String role, String enabled) {
this.id = id;
this.email = email;
this.userName = userName;
this.firstName = firstName;
this.lastName = lastName;
this.password = password;
this.role = role;
this.enabled = enabled;
}
#Id
#Column(name = "ID", unique = true, nullable = false)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Column(name = "EMAIL", unique = true, nullable = false)
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Column(name = "USER_NAME", unique = true, nullable = false)
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
#Column(name = "FIRST_NAME", nullable = false)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Column(name = "LAST_NAME", nullable = false)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#Column(name = "PASSWORD", nullable = false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
#Column(name = "ROLE", nullable = false)
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
#Column(name = "ENABLED", columnDefinition = "VARCHAR(1) default 'Y'", nullable = false)
public String getEnabled() {
return enabled;
}
public void setEnabled(String enabled) {
this.enabled = enabled;
}
}
Controller
#RequestMapping(value = "/motsach/add/", method = RequestMethod.POST)
public String saveBook(#ModelAttribute("bookForm") #Validated Book book, BindingResult result, Model model,
#RequestParam CommonsMultipartFile[] image, String userName, final RedirectAttributes redirectAttributes)
throws IOException, UnsupportedEncodingException {
MyUserAccount myUserAccount = myUserAccountDAO.findByUserName(userName);
System.out.println(userName + "sssssssssssss");
book.setMyUserAccount(myUserAccount);
redirectAttributes.addFlashAttribute("css", "success");
if (book.getBook_ID() == null) {
System.out.println(book.getBook_ID());
redirectAttributes.addFlashAttribute("msg", "book added successfully!");
} else {
redirectAttributes.addFlashAttribute("msg", "book updated successfully!");
}
for (CommonsMultipartFile aFile : image) {
System.out.println("Saving file: " + aFile.getOriginalFilename());
book.setImage_name(aFile.getOriginalFilename());
book.setImage(aFile.getBytes());
System.out.println("Damn that Shit!");
}
bookService.saveOrUpdate(book);
// POST/REDIRECT/GET
return "redirect:/motsach/" + book.getBook_ID();
}
You have a #manyToOne (with ID as name ) relation that can't be null. So in order to add a book you have to set MyUserAccount to a book before saving or you can turn into :
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "ID", nullable = true)
public MyUserAccount getMyUserAccount() {
return this.myUserAccount;
}
and modify your column in your database to set the possibility of null value.

Problems join fetching with a where clause to same entity

I have the following query in my repository:
SELECT p FROM Project p JOIN p.users u WHERE u.login =:login
There is a Many To Many relationshio between user and project.
Everything works fine and it returns the user's projects, but I want it for each project to return the corresponding set of users. So updated it with a fetch join:
SELECT p FROM Project p JOIN FETCH p.users JOIN p.users u WHERE u.login =:login
But now i got the following exception:
nested exception is java.lang.IllegalArgumentException: Count query validation failed for method public abstract org.springframework.data.domain.Page com.example.app.repository.ProjectRepository.findAllByUserLo‌​gin(java.lang.String‌​,org.springframework‌​.data.domain.Pageabl‌​e)! org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
Cannot find a workaround for it to execute the where clause and fetch the collection at the same time.
Project Entity:
#Entity
#Table(name = "project")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#Document(indexName = "project")
public class Project implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#NotNull
#Size(min = 10, max = 50)
#Column(name = "name", length = 50, nullable = false)
private String name;
#Size(max = 150)
#Column(name = "description", length = 150)
private String description;
#Column(name = "project_type")
private Integer projectType;
#Column(name = "is_active")
private Boolean isActive;
#Column(name = "date_created")
private ZonedDateTime dateCreated;
#Column(name = "date_updated")
private ZonedDateTime dateUpdated;
#ManyToMany
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#JoinTable(name = "project_user",
joinColumns = #JoinColumn(name="projects_id", referencedColumnName="ID"),
inverseJoinColumns = #JoinColumn(name="users_id", referencedColumnName="ID"))
private Set<User> users = new HashSet<>();
#OneToMany(mappedBy = "project")
#JsonIgnore
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Milestone> milestones = new HashSet<>();
#OneToMany(mappedBy = "project")
#JsonIgnore
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<TaskList> taskLists = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getProjectType() {
return projectType;
}
public void setProjectType(Integer projectType) {
this.projectType = projectType;
}
public Boolean isIsActive() {
return isActive;
}
public void setIsActive(Boolean isActive) {
this.isActive = isActive;
}
public ZonedDateTime getDateCreated() {
return dateCreated;
}
public void setDateCreated(ZonedDateTime dateCreated) {
this.dateCreated = dateCreated;
}
public ZonedDateTime getDateUpdated() {
return dateUpdated;
}
public void setDateUpdated(ZonedDateTime dateUpdated) {
this.dateUpdated = dateUpdated;
}
public Set<User> getOwners() {
return users;
}
public void setOwners(Set<User> users) {
this.users = users;
}
public Set<Milestone> getMilestones() {
return milestones;
}
public void setMilestones(Set<Milestone> milestones) {
this.milestones = milestones;
}
public Set<TaskList> getTaskLists() {
return taskLists;
}
public void setTaskLists(Set<TaskList> taskLists) {
this.taskLists = taskLists;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Project project = (Project) o;
if(project.id == null || id == null) {
return false;
}
return Objects.equals(id, project.id);
}
#Override
public int hashCode() {
return Objects.hashCode(id);
}
#Override
public String toString() {
return "Project{" +
"id=" + id +
", name='" + name + "'" +
", description='" + description + "'" +
", projectType='" + projectType + "'" +
", isActive='" + isActive + "'" +
", dateCreated='" + dateCreated + "'" +
", dateUpdated='" + dateUpdated + "'" +
'}';
}
}
User Entity:
#Entity
#Table(name = "user")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#Document(indexName = "user")
public class User extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#NotNull
#Pattern(regexp = Constants.LOGIN_REGEX)
#Size(min = 1, max = 100)
#Column(length = 100, unique = true, nullable = false)
private String login;
#JsonIgnore
#NotNull
#Size(min = 60, max = 60)
#Column(name = "password_hash",length = 60)
private String password;
#Size(max = 50)
#Column(name = "first_name", length = 50)
private String firstName;
#Size(max = 50)
#Column(name = "last_name", length = 50)
private String lastName;
#Email
#Size(max = 100)
#Column(length = 100, unique = true)
private String email;
#NotNull
#Column(nullable = false)
private boolean activated = false;
#Size(min = 2, max = 5)
#Column(name = "lang_key", length = 5)
private String langKey;
#Size(max = 20)
#Column(name = "activation_key", length = 20)
#JsonIgnore
private String activationKey;
#Size(max = 20)
#Column(name = "reset_key", length = 20)
private String resetKey;
#Column(name = "reset_date", nullable = true)
private ZonedDateTime resetDate = null;
#Column(name = "avatar", nullable = true)
private String avatar;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login.toLowerCase(Locale.ENGLISH);
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean getActivated() {
return activated;
}
public void setActivated(boolean activated) {
this.activated = activated;
}
public String getActivationKey() {
return activationKey;
}
public void setActivationKey(String activationKey) {
this.activationKey = activationKey;
}
public String getResetKey() {
return resetKey;
}
public void setResetKey(String resetKey) {
this.resetKey = resetKey;
}
public ZonedDateTime getResetDate() {
return resetDate;
}
public void setResetDate(ZonedDateTime resetDate) {
this.resetDate = resetDate;
}
public String getLangKey() {
return langKey;
}
public void setLangKey(String langKey) {
this.langKey = langKey;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
if (!login.equals(user.login)) {
return false;
}
return true;
}
#Override
public int hashCode() {
return login.hashCode();
}
#Override
public String toString() {
return "User{" +
"login='" + login + '\'' +
", avatar='" + avatar + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", activated='" + activated + '\'' +
", langKey='" + langKey + '\'' +
", activationKey='" + activationKey + '\'' +
"}";
}
}
Try to remove second join:
SELECT p FROM Project p JOIN FECTH p.users u WHERE u.login =:login
And if you want to get Projects which contains specified user by login then you can try this:
SELECT p FROM Project p JOIN FECTH p.users u WHERE :login in elements(u.login)

Categories