jpa many to many inverse side cant perform merge operation after been cascaded - jpa

i have two entity classes with mapping #ManytoMany
Inverse Side
public class Users implements Serializable {
#ManyToMany(mappedBy = "usersList",cascade=CascadeType.MERGE)
private List<Tags> tagsList = new ArrayList();
Owning Side
public class Tags implements Serializable {
#JoinTable(name = "USERS_TAG_XREF", joinColumns = {
#JoinColumn(name = "TAG_ID", referencedColumnName = "TAG_ID")}, inverseJoinColumns = {
#JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")})
#ManyToMany
private List<Users> usersList = new ArrayList();
Now when i perform an operation on the owning side like this;
for(Tags t : tList){
Users u = em.find(Users.class, id);
t.getUsersList().clear();
em.merge(t);
}
It clears many users U relations with the Tag entity t, i assume this works because Tag entity is the owing side , but when i perform this with the inverse side ;
Users u = em.find(Users.class,id);
u.getTagList().clear();
em.merge(u);
It doesnt do anything on the table, i actually want the code to clear many Tags t relations with one User u, but i feel since this is an inverse side by adding the cascade=CascadeType.MERGE would allow merge operation as this em.merge(u); but it doesnt
please help

You should also remove User from Tag entity
Users u = em.find(Users.class,id);
{
for (Tag t : u.getTagList())
{
t.hetUserList().remove(u);
}
u.getTagList().clear();
em.merge(u);
}

Related

Jpa not inserting the record in the join table for many to many relatioship

I have a Many-to-Many relationship with User and Role JPA entities. When I try to save the entities, both User and Role entities gets persisted in the table, but the junction table is not getting inserted with the records, Where am I going wrong
User Entity
#Entity
#Table(name="users")
#Data
#NoArgsConstructor
#EqualsAndHashCode(exclude = "roles")
#ToString(exclude = "roles")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String password;
private double salary;
public User(String name, String password, double salary) {
super();
this.name = name;
this.password = password;
this.salary = salary;
}
#ManyToMany(
mappedBy = "users")
private Set<Role> roles = new HashSet<>();
public void addRole(Role role) {
this.roles.add(role);
role.getUsers().add(this);
}
}
Role Entity
#Entity
#Table(name = "roles")
#Data
#NoArgsConstructor
#EqualsAndHashCode(exclude = "users")
#ToString(exclude = "users")
public class Role {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String roleName;
public Role(String roleName) {
super();
this.roleName = roleName;
}
#ManyToMany
#JoinTable(
name="user_roles",
joinColumns = #JoinColumn(name="role_id", nullable = false),
inverseJoinColumns = #JoinColumn(name="user_id", nullable = false)
)
private Set<User> users = new HashSet<>();
}
Client class
#EventListener(ApplicationReadyEvent.class)
public void onApplicationStartup(ApplicationReadyEvent event) {
User kiran = new User("kiran", this.passwordEncoder.encode("welcome"), 4500000);
User vinay = new User("vinay", this.passwordEncoder.encode("welcome"), 4500000);
Role userRole = new Role("ROLE_USER");
Role adminRole = new Role("ROLE_ADMIN");
kiran.addRole(userRole);
vinay.addRole(userRole);
vinay.addRole(adminRole);
this.userRepository.save(kiran);
this.userRepository.save(vinay);
}
Where am I going wrong?
You've mapped a bidirectional relationship, but are only setting one side of it in your object model - the wrong side. Should there ever be a discrepancy, the owning side controls the values of foreign keys, and since you have left the owning side empty, they aren't being set. You are responsible to set both sides of relationships and keeping them in synch with what you want in the database.
Since you don't have cascade options set on the relationships, you are also responsible for persisting the roles independently from the Users. Something more like:
public void onApplicationStartup(ApplicationReadyEvent event) {
// you might want to check to see if these roles already exists and use those instead of creating new ones
Role userRole = roleRepository.save(new Role("ROLE_USER"));
Role adminRole = roleRepository.save(new Role("ROLE_ADMIN"));
User kiran = new User("kiran", this.passwordEncoder.encode("welcome"), 4500000);
kiran.addRole(userRole);//assumes this adds the user to the role.users as well.
this.userRepository.save(kiran);
User vinay = new User("vinay", this.passwordEncoder.encode("welcome"), 4500000);
vinay.addRole(userRole);
vinay.addRole(adminRole);
this.userRepository.save(vinay);
}
Also, you are using Set in your entities with Lombok using "#EqualsAndHashCode" generation. Don't do that!
Set uses the equals/hashcode logic to determine if two objects are the same to filter out duplicates, while Lombok generates those methods to use what are mutable fields. In the case you have new entities in those sets (ie this usecase), the IDs are null and will change when set from JPA. You are better off keeping Java equals/hashcode logic if you don't know what effects those will have on your application. try using either List in your model and/or not having Lombok generate those method for you.

JPA Specification: Select all entities which have at least one param with attribute from list

I have 2 entities with relationship ManyToMany
#Entity
#Table
public class TranslationUnit implements Serializable {
#Id
private Long id;
#ManyToMany(mappedBy = "translationUnit", fetch = FetchType.EAGER)
private Set<Category> categories = new HashSet<>();
}
#Entity
#Table
public class Category implements Serializable {
#ManyToMany
#JoinTable(name = "category_translation_unit",
joinColumns = #JoinColumn(name = "categories_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(name = "translation_units_id", referencedColumnName = "id"))
private Set<TranslationUnit> translationUnits = new HashSet<>();
}
In Category I have 1 field, which should be used for filtering:
String name;
I need to be able to specify list of Category names (List), and select those TranslationUnits which have at least one Category with specified name.
I have several other filtering options, which should be used together, and I successfully built Specifications for them. But I've stuck with this one.
Please help.
P.S. One of my existing Specifications looks like this:
Specification idSpec = (Specification) (r, q, cb) -> {
List<Predicate> predicates = new ArrayList<>();
if (!filterRequest.getTranslationUnitIds().isEmpty())
predicates.add(r.get(TranslationUnit_.id).in(filterRequest.getTranslationUnitIds()));
return cb.and(predicates.toArray(new Predicate[predicates.size()]));
};
Good day. You could use IN for filtering translation units by category names list. I believe, it will look like this using Criteria API:
Root<TranslationUnit> itemsRoot = ...;
Join join = itemsRoot.join("categories");
List<Predicate> predicates = new ArrayList<>();
predicates(join.get("name").in(categoryNamesList));

ManyToMany relationship not persisting in JPA when adding a new one

I have this piece of code which adds an entry to film_actor (in theory), but it doesn't work. This code doesn't crash, but doesn't save the added entry to the database.
Actor nuevo = ActorFacadeEJB.find(actor_id);
Film pelicula = FilmFacadeEJB.find(film_id);
pelicula.getActors().add(nuevo);
Also I have this code:
#ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
#JoinTable(name = "film_actor", joinColumns = { #JoinColumn(name = "film_id") }, inverseJoinColumns = {
#JoinColumn(name = "actor_id") })
private Set<Actor> actors;
public Set<Actor> getActors() {
return actors;
}
The actor also has a film set:
#ManyToMany(mappedBy="actors")
private Set<Film> film= new HashSet<Film>();
How can I fix all this to make it work? I googled it, and many people have similar code to mine, but just mine doesn't work.
Make sure your Annotations are proper as below :
#Entity
#Table(name="film")
class Film{
#ManyToMany(cascade = {CascadeType.ALL})
#JoinTable(name="film_actor",
joinColumns={#JoinColumn(name="film_id")},
inverseJoinColumns={#JoinColumn(name="actor_id")})
private Set<Actor> actors=new HashSet<Actor>;
}
#Entity
#Table(name="actor")
class Actor{
#ManyToMany(mappedBy="actors")
private Set<Film> film= new HashSet<Film>();
}
You need to assign both sides of the relationship:
pelicula.getActors().add(nuevo);
nuevo.getFilms().add(pelĂ­cula);

Jpa eclipselink bug when try persist collection by another entity

Hello i have got one entity which contains collection of object:
#Entity
public class ResourceType{
#Id
public Integer id;
#ManyToMany(mappedBy = "resourcesTypes")
private Set<Resource> resources = new HashSet<>();
}
As you see this entity contains collection of Resource
#Entity
public class Resource{
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(name = "RESOURCE_TYPE_CROSS", joinColumns = { #JoinColumn(name = "RESOURCE_ID") }, inverseJoinColumns = { #JoinColumn(name = "RESOURCE_TYPE_ID", referencedColumnName = "ID") })
private List<ResourceType> resourcesTypes = new ArrayList<>();
}
So i guess if i get all resourceTypes with all resources this collection is in local cache.
But if i try persist resource :
#Override
public ResourcesDTO createResource(ResourcesDTO resource) throws CreateResourceException {
if (resource.getResourcesTypes().isEmpty()) {
throw new CreateResourceException("Can not create resouce " + resource.getName()
+ " none resource types is set");
resourceDAO.create(map(resource));
log.debug("Created resource: " + r);
resource.setId(r.getId());
return resource
}
If i perssist Resource with ResourcesTypes and next i will get all ResourceTypes entity manager do not found new Resources. But in my cross table resource_type_cross everything is ok. i will try to do something like that after create new resource:
for(ResourceType rt : resource.getResourceTypes()){
em.refresh(rt);
}
But its not working properly. After i will restart server everything is fine. But why entity manager do not refresh resource type ??
This is what i use to read all ResourceTypes:
public ResourceType getAllResourceTypes(){
em.createQuery("Select n from Resource n left join fetch n.children");
return em.find(ResourceType.class, 0); //0 - ROOT
}
as follow article : http://www.tikalk.com/java/load-tree-jpa-and-hibernate
The Main question is that :
create or update entity by one side do not update another, so in my case i create and update resource type by resource entity, but i am using resouce type to get all resource.
First, there is a problem with your ManyToMany mappings. You have made both sides unidirectional accessing the same join table instead of bidirectional. One side should control the mapping, with the other set to mappedby the owning side.
Second, have you flushed or committed the changes when you refresh ResourceTypes? It is probably better to maintain both sides by setting the reference in code rather than query to refresh each and every ResourceType

JPA returns multiple objects of the same instance when listing all entities of a class

I have a JPA entity with a list of child entities. In this case a user entity with roles attached to it.
It looks (a bit simplified - some fields/methods omitted) like this:
#Entity
public class MyUser{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long myUserId;
private String username;
#OneToMany
#JoinTable(name = "userrole",
joinColumns = {
#JoinColumn(name="myUserId", unique = true)
},
inverseJoinColumns = {
#JoinColumn(name="roleId")
}
)
private Collection<Role> roles;
public Collection<Role> getRoles() {
return roles;
}
}
If intressting, the Role entity is very simple.
#Entity
public class Role {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long roleId;
private String role; // a few more string fields here .
When I add two users and a few hundred roles per user I get a wierd behaviour when I list the users. Each user get's listed a few hundred times (same user = same unique id).
The problematic code:
Query q = em.createQuery("SELECT u FROM MyUser u LEFT JOIN FETCH u.roles");
Collection<MyUser> users = q.getResultList();
for(MyUser u : users){
// print/use u here
}
However, when I just access the database and do select statements, it seems fine. Every user exists only once.
I use OpenJPA 1.2 together with a IBM DB2 database in this case.
I think you have your model wrong, typically a user-role relationship is not OneToMany but "ManyToMany" so you should change your code to look something like this:
#Entity
public class MyUser{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long myUserId;
private String username;
#ManyToMany //This should be many to many
#JoinTable(name = "userrole",
joinColumns = {
#JoinColumn(name="myUserId") //The userId in the join table should
//NOT be unique because the userId can
//be many times with different roles
},
inverseJoinColumns = {
#JoinColumn(name="roleId")
}
)
private Collection<Role> roles;
public Collection<Role> getRoles() {
return roles;
}
}
Try this way and see if it works.
Also your query shouldn't need the Left Join, the roles should be fetched automatically by JPA once you use the getRoles() method on each entity (using LAZY Fetch)
Actually, it's reasonable to have #ManyToMany mapping for User and UserRole entities. The problem with your query is that it returns all the rows from the join table what I believe you don't need. So just add group by u to your query as follows:
SELECT u FROM MyUser u LEFT JOIN FETCH u.roles GROUP BY u
and you'll be done.