Hibernate list deleting the records - java

I have a many to many mapping in the hibernate and When I fetch the records eagerly there seems to be no issues, however when I try to use the FetchType.lazy it seems to delete the mapping. I am using criteria.list() to get the records and it has pagination on it.
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(name = "PanicOperNotificationList", joinColumns = { #JoinColumn(name = "panicOperNotificationId",insertable = false,nullable = false,updatable = false) },
inverseJoinColumns = { #JoinColumn(name = "panicNotificationListId",insertable = false,nullable = false,updatable=false) })
private Set<PanicNotificationList> notificationLists = new HashSet<>();

Related

Vaadin/Spring duplicated error in a ManytoMany Join

I have a User class and a Movie class which I want to merge into separate Lists. Once for movies that have been watched and one for the watchlist. Separately everything is fine but it seems that when I want to put them on both merges I get a duplication error even though they are saved in differently named columns.
#ManyToMany(cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
#JoinTable(name = "user_watch_list", joinColumns = #JoinColumn(name = "watche_User_ID", referencedColumnName = "id"), inverseJoinColumns = #JoinColumn(name = "watch_Movie_id",referencedColumnName = "movieID"))
private List<Movie> watchList = new ArrayList<>();
#ManyToMany(cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
#JoinTable(name = "user_watched_movies", joinColumns = #JoinColumn(name = "watcheed_User_ID",referencedColumnName = "id"), inverseJoinColumns = #JoinColumn(name = "watched_movie_id",referencedColumnName = "movieID"))
private List<Movie> watchedMovies = new ArrayList<>();

Preventing circular JSON conversions on ManyToMany entities

I am converting JPA entities to JSON for a REST API. Several of the entities have ManyToMany relationships which can cause recursion in the conversion process. I understand how to use #JsonIgnoreProperties to prevent recursion on the ManyToOne relationship, but not ManyToMany. Any guidance appreciated. Here is an example:
#ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST,CascadeType.DETACH})
#JoinTable(name = "insurance_companycodes", joinColumns = {
#JoinColumn(name = "insurance_id", referencedColumnName = "id", nullable = false, updatable = false) }, inverseJoinColumns = {
#JoinColumn(name = "insuranceCompanyCode_id", referencedColumnName = "id", nullable = false, updatable = false) })
private Set<InsuranceCompanyCode> insuranceCompanyCodes = new HashSet<>();

How to Ignore a whole Mapped/Joined Model(Class) in Spring hibernate.

I have three tables. tbl_vendor and tbl_category tables are mapped using #ManyToMany relation. When I am getting 'Vendors' under a particular 'Category' I am also getting 'Areas' because tbl_vendor and tbl_area are also mapped using #ManyToMany.
relations:
in tbl_vendor Model :
#ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
#JoinTable(name = "tbl_category_vendor", joinColumns = {#JoinColumn(name = "vendor_id")}, inverseJoinColumns = {#JoinColumn(name = "category_id")})
private Set<Category> categories = new HashSet<>();
....
#ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
#JoinTable(name = "tbl_area_vendor", joinColumns = {#JoinColumn(name = "vendor_id")}, inverseJoinColumns = {#JoinColumn(name = "area_id")})
private Set<Area> areas = new HashSet<>();
in tbl_category Model :
#JsonIgnore
#ManyToMany(cascade = CascadeType.DETACH, mappedBy = "categories", fetch = FetchType.LAZY)
private List<Vendor> vendors = new ArrayList<>();
in tbl_area Model :
#JsonIgnore
#ManyToMany(mappedBy = "areas", fetch = FetchType.LAZY)
private List<Vendor> vendors = new ArrayList<>();
I have a particular case where I don't need this 'Area' with 'Vendor'.
is there any way to ignore already mapped 'Area' from 'Vendor' using JpaRepository in some particular case ?

Hibernate multiple attribute mappings same table deletion

I have a hopefully simple problem in Hibernate. I have Users and Projects. The users_projects (in my case user_project_association) has an extra attribute "role" to say if the user is project leader or normal user.
#OneToMany(mappedBy = "project", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
#LazyCollection(LazyCollectionOption.EXTRA)
private Set<UserProjectAssociation> userProjectAssociations = new HashSet<>();
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(name = "user_project_role",
joinColumns = #JoinColumn(name = "project_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id"))
#WhereJoinTable(clause = "role_id=7")
#Setter(AccessLevel.NONE)
private Set<User> projectLeaders = new HashSet<>();
#ManyToMany
#JoinTable(name = "user_project_role",
joinColumns = #JoinColumn(name = "project_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id"))
#LazyCollection(LazyCollectionOption.EXTRA)
#Setter(AccessLevel.NONE)
private Set<User> users = new HashSet<>();
If i try to remove a project via "getEntityManager().remove(persistentInstance);" I can see in the log this sql statements:
Hibernate:
/* delete collection com.XXX.core.model.Project.users */ delete
from
user_project_role
where
project_id=?
Hibernate:
/* delete collection com.XXX.core.model.Project.projectLeaders */ delete
from
user_project_role
where
project_id=?
and (
role_id=7
)
Hibernate:
/* delete com.XXX.core.model.UserProjectAssociation */ delete
from
user_project_role
where
id=?
and the error message:
2016-05-25 10:05:57 INFO AbstractBatchImpl:208 - HHH000010: On release of batch it still contained JDBC statements
and nothing is deleted. Does anyone have an idea to get my projects deleted?
Thanks a lot :)
Puh. I found (the or a) solution for it. It's not really elegant, but it's working:
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(name = "user_project_role",
joinColumns = #JoinColumn(name = "project_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id"))
#WhereJoinTable(clause = "role_id=7")
#SQLInsert(sql = "insert into user_project_role (project_id, user_id, role_id) values (?, ?, 7)")
#SQLDelete(sql = "delete from user_project_role where project_id=? and user_id=? and role_id = 7")
#Setter(AccessLevel.NONE)
private Set<User> projectLeaders = new HashSet<>();
#ManyToMany
#JoinTable(name = "user_project_role",
joinColumns = #JoinColumn(name = "project_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id"))
#LazyCollection(LazyCollectionOption.EXTRA)
#WhereJoinTable(clause = "role_id=8")
#SQLInsert(sql = "insert into user_project_role (project_id, user_id, role_id) values (?, ?, 8)")
#Setter(AccessLevel.NONE)
private Set<User> projectUsers = new HashSet<>();
So I removed the userProjectAssociations attribute and added customized #SQLInsert and #SQLDelete Statements.
If I want to get all Users including normal users and project leaders I use this method:
public Set<User> getAllUsers() {
Set<User> allUsers = new HashSet<>();
allUsers.addAll(projectLeaders);
allUsers.addAll(projectUsers);
return allUsers;
}

hibernate annotations one to many with composite foreign key

I have quite big problem with hibernate annotation mapping. Heres my diagram:
As for mapping object, few are simple cause its 1...N for:
TASK_DEF -> REPORT_DATA
REPORT_DATA -> REPORT_DATA_COLUMN
REPORT_DATA -> REPORT_DATA_VALUE
right?
Now problem is when I'm trying to map Collection of REPORT_DATA_VALUE to REPORT_DATA_COLUMN. I've tried this way:
#OneToMany(fetch = FetchType.LAZY)
#ForeignKey(name = "FK_REPORT_DATA_VALUE_REPORT_DA", inverseName = "PK_REPORT_DATA_COLUMN")
#JoinTable(name = "REPORT_DATA_VALUE", joinColumns = {
#JoinColumn(name = "REPORT_DATA_ID"),
#JoinColumn(name = "COLUMN_NM")
}, inverseJoinColumns = {
#JoinColumn(name = "REPORT_DATA_ID"),
#JoinColumn(name = "COLUMN_NM")
})
List<ReportDataValue> reportDataValueList;
But hibernate selects wrong identifies, therefore couldnt execute query. Could someone help me with this?
There is a problem in your code. Try this instead:
#OneToMany(fetch = FetchType.LAZY)
#JoinTable(name = "REPORT_DATA_VALUE", joinColumns = {
#JoinColumn(name = "REPORT_DATA_ID"),
}, inverseJoinColumns = {
#JoinColumn(name = "COLUMN_NM")
})
List<ReportDataValue> reportDataValueList;
By the way, If I were you, I wouldn't create a table for One To Many. I'd do this:
public class A{
#ManyToOne(fetch=FetchType.LAZY)
#JoinColumn(name="B_ID")
B b;
}
public class B{
#OneToMany(mappedBy="b",fetch=FetchType.EAGER)
Set<A> as;
}

Categories

Resources