How insert automatic ManyToMany with OpenJPA - java

I have 2 classes mapped with OpenJPA. One class is User and has relationship ManyToMany with the AppProfile. In the database I have the table relations USER_APP_PROFILES (ID,User_ID,App_ID).
My class Open JPA User
#Table(name = "USER_PROFILE",schema = "BPMS")
public class UserProfile {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="USER_ID")
private Integer userID;
#ManyToMany(mappedBy="listUserProfile", fetch=FetchType.EAGER)
private List<AppProfile> listAppProfile;
}
My class AppProfile
#Table(name = "APP_PROFILE",schema = "BPMS")
public class AppProfile {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="APP_ID")
private Integer appID;
#ManyToMany(fetch=FetchType.LAZY)
#JoinTable(
name="USER_APP_PROFILES",
joinColumns={#JoinColumn(name="APP_ID", referencedColumnName="APP_ID")},
inverseJoinColumns={#JoinColumn(name="USER_ID", referencedColumnName="USER_ID")})
private List<UserProfile> listUserProfile;
}
I fetch a user into the database by EntitiyManager, after I add the List AppProfile.
Example:
UserProfile userProfile //(populate with fetch in database)
AppProfile app = new App();
app.setAppID(11);
List<AppProfile> listApp = new ArrayList<AppProfile>();
listApp.add(app);
userProfile.setListAppProfile(listApp);
em.merge(userProfile)
How do I merge, if I need JPA automatic insert in table USER_APP_PROFILES:
User_App_Profile_ID : new register
UserID : 1
AppID : 11

Try moving the #JoinTable to UserProfile (changing the parameters accordingly) and adding cascade={CascadeType.ALL} to the #ManyToMany annotation.
That should do the trick.

Related

JPA Error: InvalidDataAccessApiUsageException: detached entity passed to persist

I have two entities mapped to one another using the oneToMany annotation. One entity is bookedBus and the second is drivers The drivers entity would already have a row inserted into that would later become a foreign reference (FK) to bookedBus entity(PK). Below are the two entities, setters and getter have been skipped for brevity.
First entity
#Entity
#Table(name = "bookedBuses")
public class BookedBuses implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinColumn(name = "driver_id")
private Drivers driver;
}
Second entity
#Entity
public class Drivers implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "driver")
private List<BookedBuses> bookedBus;
}
Now When I try to save to the booked bus entity it throws the following exception
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.bus.api.entity.Drivers; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.bus.api.entity.Drivers
Below is how I tried saving to the bookedBus entity
BookedBuses bookedRecord = new BookedBuses();
bookedRecord.setBookedSeats(1);
bookedRecord.setBookedBusState(BookedBusState.LOADING);
bookedRecord.setBus(busService.getBusByPlateNumber(booking.getPlateNumber()));
bookedRecord.setRoute(booking.getRoute());
infoLogger.info("GETTING DRIVER ID ======= " + booking.getDriver().getId());
Drivers drivers = new Drivers(booking.getDriver().getId());
List<BookedBuses> d_bu = new ArrayList<>();
drivers.setBooked(d_bu);
drivers.addBooked(bookedRecord);
bookedRecord.setDriver(drivers);
bookedBusService.save(bookedRecord);
My BookBusService Save Method as requested
#Autowired
private BookedBusRepository bookedBusRepo;
public boolean save(BookedBuses bookedRecord) {
try {
bookedBusRepo.save(bookedRecord);
return true;
} catch (DataIntegrityViolationException ex) {
System.out.println(ex);
AppConfig.LOGGER.error(ex);
return false;
// Log error message
}
}
1st you have some mix up in naming: you have Driver & Drivers. Like this:
private Drivers driver;
Also selecting variable names like this:
BookedBuses bookedRecord = new BookedBuses();
will cause a lot of confusion. Do not mix plural & singular between types and preferably also do not introduce names that might not be easily associated like record. Also this:
private List<BookedBuses> bookedBus;
which should rather be like:
private List<BookedBus> bookedBuses;
(and would alsoi require change to your class name BookedBuses -> BookedBus)
Anyway the actual problem seems to lie here:
Drivers drivers = new Drivers(booking.getDriver().getId());
You need to fetch existing entity by id with a help of repository instead of creating a new one with id of existing. So something like:
Drivers drivers = driverRepo.findOne(booking.getDriver().getId()); // or findById(..)
It seems that you have a constructor (that you did not show) that enables to create a driver with id. That is not managed it is considered as detached. (You also have drivers.addBooked(bookedRecord); which you did not share but maybe it is trivial)
Note also some posts suggest to changeCascadeType.ALL to CascadeType.MERGE whether that works depends on your needs. Spring data is able to do some merging on save(..) based on entity id but not necessarily in this case.
This line
Drivers drivers = new Drivers(booking.getDriver().getId());
If you already have the driver ID available with you then there's no need to pull the driver ID again from the DB.
After removing the Cascade attribute from #OneToMany & #ManyToOne your code should work.
#Entity
#Table(name = "bookedBuses")
public class BookedBuses implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
`
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "driver_id")
private Drivers driver;
}
#Entity
public class Drivers implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#OneToMany(fetch = FetchType.LAZY)
#JoinColumn(name = "driver_id")
private List<BookedBuses> bookedBus;
}

UnsupportedOperationException when updating cascading children in Spring+JPA

I'm using OneToMany mapping in my SpringBoot project, while I'm having problems when updating the children along with parent update, sample code is like below:
User.java
#Table(name = "user")
#Entity
public class User {
#Id
#GeneratedValue
private Integer id;
#OneToMany(mappedBy = "groupUser", cascade = {CascadeType.ALL}, orphanRemoval = true)
private List<UserGroup> userGroups = new ArrayList<>();
}
UserGroup.java
#Table(name = "user_group")
#Entity
public class UserGroup {
#Id
#GeneratedValue
private Integer id;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name="user_id")
private User groupUser;
}
SampleUsageCode.java
#Service
public class UserService {
#Autowired
private UserRepository userRepositry;
#Transactaional
public batchUpdateUsers(Collection<User> toBeSavedUsers) {
Map<Integer, User> toBeSavedIdUserMap = toBeSavedUsers.stream()
.collect(groupBy(User::getId(), toList()));
Collection<User> existingUsers = userRepositry.findByIdIn(toBeSavedIdUserMap.entrySet().stream()
.map(Map.Entry::getKey).collect(toList()));
existingUsers.forEach(user -> user.getUserGroups().add(toBeSavedIdUserMap.get(user.getId()).getUserGroups()));
}
}
To simplify the problem, Let's just assume the user groups in to-be-saved users is totally different with the existing ones in the database. The problem is when I try to add new user groups to existing users, it throws java.lang.UnsupportedOperationException. It seems the persistentBag type of userGroups in User is not editable.
I tried with just creating a new collection to store both existing and new user groups, but another error with A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance occurs when I try to save the updated users. How can I achieve this cascading-children merge requirement?
So the problem is caused by that the user groups list I prepared for the test is Unmodifiable

Hibernate persist entity without fetching association object. just by id

I have an simple association between 2 entities:
public class Car {
...
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "user_id")
private User user;
...
}
and
public class User {
#Id
#GeneratedValue
#Column(name = "user_id")
private long userId;
...
#OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
private Set<Car> cars;
...
}
Then I get some user id from client. For example, userId == 5;
To save car with user I need to do next:
User user = ... .findOne(userId);
Car car = new Car();
car.setUser(user);
... .save(car);
My question is: Can I persist car record without fetching user?
Similarly like I would do by using native SQL query: just insert userId like string(long) in Car table.
With 2nd lvl cache it will be faster but in my opinion I don't need to do extra movements. The main reason that I don't want to use native Query is because I have much more difficult associations in my project and I need to .save(car) multiple times. Also i don't want to manually control order of query executions.
If I use session.createSQLQuery("insert into .....values()") will the Hibernate's batch insert work fine?
Correct me if I'm wrong.
Thanks in advance!
UPDATE:
Actually the mapping is similar to:
There is #ManyToMany association between User and Car. But cross table is also an entity which is named, for example, Passanger. So the mapping is next:
public class User{
#OneToMany(fetch = FetchType.LAZY, mappedBy = "user", targetEntity = Passenger.class)
private Set<Passenger> passengers;
}
Cross entity
#IdClass(value = PassengerPK.class)
public class Passenger {
#Id
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "user_id")
private User user;
#Id
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "car_id")
private Car car;
... other fields ...
}
Car entity:
public class Car {
#OneToMany(fetch = FetchType.LAZY, mappedBy = "car", targetEntity = Passenger.class, cascade = CascadeType.ALL)
private Set<Passenger> passengers;
}
And the code:
List<User> users = ... .findInUserIds(userIds); // find user records where userId is IN userIds - collection with user Ids
Car car = new Car(); //initialization of car's fields is omitted
if (users != null) {
car.setPassengers(new HashSet<>(users.size()));
users.forEach((user) -> car.getPassengers().add(new Passenger(user, car)));
}
... .save(car);
"Can I persist car record without fetching user?"
Yes, that's one of the good sides of Hibernate proxies:
User user = entityManager.getReference(User.class, userId); // session.load() for native Session API
Car car = new Car();
car.setUser(user);
The key point here is to use EntityManager.getReference:
Get an instance, whose state may be lazily fetched.
Hibernate will just create the proxy based on the provided id, without fetching the entity from the database.
"If I use session.createSQLQuery("insert into .....values()") will the Hibernate's batch insert work fine?"
No, it will not. Queries are executed immediately.
If someone is using Spring Data JPA: The same can be achieved in Spring Data JPA can be done using the method
JpaRepository.getReferenceById(ID id)
This replaced the former
getOne(ID)
Hibernate users can implement this method:
public <T extends Object> T getReferenceObject(Class<T> clazz, Serializable id) {
return getCurrentSession().get(clazz, id);
}
And call like:
MyEntity myEntity = getRefererenceObject(MyEntity.class, 1);
You can change id type to Integer or Long as per your entity model.
Or T can be inherited from your BaseEntity if you have one base class for all entities.
The following approach works for me:
User user = new User();
user.setId(userId);
car.setUser(user);

Cannot add or update a child row: a foreign key constraint fails - Bidirectional mapping in hibernate

Hi I have a problem with JPA...
DB:
Java classes (irrelevant fields are ommited):
User:
#Entity
public class User{
#Id
private int iduser;
//bi-directional many-to-one association to UserInfo
#OneToMany(mappedBy="user", cascade= {CascadeType.ALL}, fetch=FetchType.EAGER)
private List<UserInfo> userInfos;
}
UserInfo:
#Entity
#Table(name="user_info")
public class UserInfo {
#Id
#Column(name="iduser_info")
private int iduserInfo;
//bi-directional many-to-one association to User
#ManyToOne
private User user;
}
Currently when I try to do this (again I omitted setting irrelevant fields):
User u = new User();
UserInfo info = new UserInfo();
u.addUserInfo(info);
em.persist(u); // save user
I get this error:
Cannot add or update a child row: a foreign key constraint fails (`webstore`.`user_info`, CONSTRAINT `fk_user_info_user` FOREIGN KEY (`user_iduser`) REFERENCES `user` (`iduser`) ON DELETE NO ACTION ON UPDATE NO ACTION)
I have been banging my head all day and I can't figure it out... I have also searched for solutions here but they all suggest that this error shows that I want to enter UserInfo without user_iduser value entered, but even if I add this before persist:
info.setUser(u);
it still doesn't work - is bidirectional mapping even supported with cascading? The desired effect is that User should be inserted and then all the UserInfos in the list after it refering the original User. How can I achieve that?
I don't want to do
SET foreign_key_checks = 0/1;
everytime =(
Try:
#ManyToOne
#JoinColumn(name="user_iduser", nullable=false)
private User user;
Then see if this works. I'm assuming that in user_info table, the user_id field is NOT NULL in your RDBMS. Also, since it's bidirectional, you will have to setXXX on UserInfo and User.
User u = new User();
UserInfo info = new UserInfo();
info.setUser(u);
u.addUserInfo(info);
em.persist(u); // save user
Update: Are you sure you're specifying an ID for both User and UserInfo? It looks like the ID is not set hence there is no reference to link UserInfo to User.
If the ID are AUTO_INCREMENT, then add the following after #Id annotation:
#GeneratedValue(strategy=GenerationType.IDENTITY)
I also had this error, fixed this error by adding #GeneratedValue(strategy = GenerationType.IDENTITY) annotation on top of iduser and you must have foreign keys CASCADE ON DELETE, UPDATE.
this is worked for me in my example ,
i created books and library.
#Entity
#Table(name = "book")
public class Books{
#Id
private int library_id;
private String libraryname;
//add getters and setters bellow
}
#Entity
#Table(name = "library")
public class Book {
#Id
private int book_id;
private String book_title;
#JsonIgnore
#ManyToOne
#JoinColumn(name="library_id" , nullable = false)
#OnDelete(action = OnDeleteAction.CASCADE)
private Library library;
//set getters and setters
}
in controller i used this method
#RequestMapping(value = "/{libraryId}/book", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public Book createBook(#PathVariable(value = "libraryId") Integer libraryId, #RequestBody Book book) {
List<Book> books = new ArrayList<Book>();
Library author1 = new Library();
Optional<Library> byId = LibraryRepository.findById(libraryId);
Library author = byId.get();
book.setLibrary(author);
Book book1 = bookRepository.save(book);
books.add(book1);
author1.setBooks((List<Book>) books);
return book1;
}

How to implement lazy loading using Spring data JPA (JPARepository)?

I am using Spring Data JPA and Hibernate as a provider. I've created several Repository classes which extends to JPARepository<Entity,Serializable> class. I am failing at the moment when I am fetching one entity it brings attached / connected entities along with it ! which are either connected via #OneToOne #OneToMany etc. How can I avoid fetching those connected entities ?
I have tried with #OneToMany(fetch=FetchType.LAZY) etc but still no luck. Following are my java code:
Repository
public interface TicketRepository extends JpaRepository<Ticket, Integer>{
}
Ticket Entity
#Entity
#Table(name = "tbl_tickets")
public class Ticket {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
private Integer id;
#Column(name = "customer", nullable = false, length = 256)
private String customer;
#OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
#JoinColumn
private User creator;
// ... other properties
}
Service
#Service
public class TicketService {
public Ticket save(Ticket obj,String id) {
User user = userService.findById(Integer.valueOf(id));
obj.setCreator(user);
Ticket savedTicket = ticketRepository.save(obj);
}
}
savedTicket always fetches User entity as well which I do not want to. How could I achieve this ?
Thanks
Get Lazy loading working on nullable one-to-one mapping you need to let hibernate do Compile time instrumentation and add a #LazyToOne(value = LazyToOneOption.NO_PROXY) to the one-to-one relation.
#OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
#JoinColumn
#LazyToOne(value = LazyToOneOption.NO_PROXY)
private User creator;
Hope this will work.

Categories