Consider the following:
#Entity
public class MainEntity {
#OneToOne(orphanRemoval = true, cascade = CascadeType.ALL)
private ChildEntity childEntity;
}
#Entity
public class ChildEntity {
#OneToMany(cascade = CascadeType.ALL)
#LazyCollection(FALSE)
private List<AnotherEntity> otherEntities;
}
Now, when i first call
final ChildEntity anewChild = new ChildEntity();
anewChild.addOtherEntity(anotherEntity); //Several Entities can be added here
mainEntity.setChildEntity(anewChild);
EntityManager.persist(mainEntity);
Everything works fine, and then i do some updates, long after the transaction is finished.
final ChildEntity anotherNewChild = new ChildEntity();
anotherNewChild.addOtherEntity(anotherEntity); //Several Entities can be added here
mainEntity.setChildEntity(anotherNewChild);
//A log of LOG.info(mainEntity); shows all fields appropriately set
//At some point during merge operation, the new ChildEntity will need to be persisted.
//According to my logs, an invocation of EntityManager.persist(anotherNewChild) occurs, during as the merge is propagated to the new entity.
//At this point is where the ChildEntity.otherEntities is detected to be null
return EntityManager.merge(mainEntity);
The problem is that, with persist, the
List<AnotherEntity>
is not null and not empty, while on merge, the
List<AnotherEntity>
is null
I am doing this over ejb remote invocation.
Hibernate 4.3.6
wildfly 8.1.0
jpa 2.1
Is there something i am missing here?
Reproduced issue with the following code:
https://github.com/marembo2008/hibernate-jpa-bug
Opened an issue on Hibernate Issue tracker.
https://hibernate.atlassian.net/browse/HHH-9751
The problem is that merge returns a new entity, so you should do something like this:
mainEntity = EntityManager.merge(mainEntity);
Related
I'm having 3 JPA Entities like this as well as the corresponding JPA-Repositories.
#Entity
public class ChairEntity {
...
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinTable(name = "chair_image")
private Set<ImageEntity> images = new HashSet<>();
...
}
#Entity
public class TableEntity {
...
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinTable(name = "table_image")
private Set<ImageEntity> images = new HashSet<>();
...
}
#Entity
public class ImageEntity{
...
private String description;
#Lob
private byte[] data;
...
}
Using a REST-API these Objects are created and updated. This works usually fine, e.g. i may add multiple imageEntities at once like this (all codes blocks are inside their own transaction)
chairEntity.getImages().add(new ImageEntity(..));
chairEntity.getImages().add(new ImageEntity(..));
chairRepository.save(chairEntity);
...or update multiple ImageEntities of the same chairEntity at once.
chairEntity.getImages().stream().forEach(imageEntity -> {
imageEntity.setDescription("some other description");
}
chairRepository.save(chairEntity);
In both cases all Changes are successfully cascaded and saved.
If, however, I am updating an existing ImageEntity as well as adding another entity, it fails:
chairEntity.getImages().stream().forEach(imageEntity -> {
imageEntity.setDescription("some other description");
}
chairEntity.getImages().add(new ImageEntity(...));
chairRepository.save(chairEntity); // crashes
The exception is as followed (an equivalent error is thrown using h2db):
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "chair_image_pkey"
When inspecting the DB-Log, it seems like Hibernate is trying to:
inserting the new image (successfully)
updating the existing image (successfully)
inserting an entry into the Join-Table/Collection-Table (chair_image) referencing the chair and the existing image. This then throws this JdbcSQLIntegrityConstraintViolationException, as this combination of Foreign keys already exists (the old image already existed before).
Why is this happening and how do i solve it? Saving and Flushing the Changes individually inside the same transaction doesn't seem to work either.
A workaround, in case anyone else comes across this problem: Reverse the order of operations:
chairEntity.getImages().add(new ImageEntity(...));
chairRepository.saveAndFlush(chairEntity);
chairEntity.getImages().stream().forEach(imageEntity -> {
imageEntity.setDescription("some other description");
}
chairRepository.save(chairEntity); // crashes
The order in which hibernate executes the SQL-Statements stays the same, but due to the flush between, the faulty insert into the Join-Table no longer happens.
I'm implementing a Spring boot application and using Spring Data JPA in it. As you know you don't have to implement the repository interface for just CRUD methods, because Spring Data JPA creates an implementation on the fly. So I have just this:
public interface PersonRepository extends JpaRepository<Person, Long> {}
I'm working with one-to-many relationship, this is in my Person domain:
#OneToMany(cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.LAZY,
mappedBy = "person")
private Set<Contact> contacts = new HashSet<>();
I decided to write an integration test for child removal from the parent:
#Test
public void removeFromContacts() {
// given
Person person = new Person ("test person");
Contact contact = new Contact("test#gmail.com", "+123456789");
contact.setPerson(person);
person.getContacts().add(contact);
personRepository.save(person);
Person savedPerson = personRepository.findOne(person.getId());
Contact persistedContact = savedPerson.getContacts().stream().findFirst().orElse(null);
// when
savedPerson.getContacts().remove(persistedContact);
persistedContact.setPerson(null);
Person edited = personRepository.save(savedPerson);
// then
Assert.assertTrue(edited.getContacts().isEmpty());
}
This test fails. The reason is savedPerson.getContacts().remove(persistedContact) line doesn't change anything, remove method returns false. It's pretty strange, because I'm trying to remove an object from a hash set which has only one object with exact same hash code (equals() method returns true as well). According to this answer the contact object could've been altered somehow after adding it to the hash set. The only thing I can think of is it happened after this line: personRepository.save(person).
If I'm right then I'm really confused: how should I remove the contact from a person, and even if I find a way, is it okay for personRepository.save method to cause a set to malfunction? And if I'm wrong I would love to know the right answer.
Thanks in advance.
Class Compte and Class User joind to one-to-one relationship
public void delete(Integer integer){
User user = userRepository.findOne(integer);
Compte compte = user.getCompte();
compte.setUser(null);
compteRepository.save(compte);
user.setCompte(null);
userRepository.save(user);
compteRepository.delete(compte);
userRepository.delete(user);
}
I have a Hibernate query with Criteria.
What I would like to do is (just for one query) tell hibernate to ignore an existing #ManyToOne annotation.
that is because hibernate creates and Left join on other tables.
I could figure it how to do it.
I have found this 2 links which didn't solve my problem:
Hibernate: How to remove an entity to which none refers to anymore in ManyToOne?
What is the difference between DELETE_ORPHAN and DELETE?
If you have such mapping:
//Parent
public class A {
...
}
//Child
public class B {
private A parent; //Many to one
...
}
Please try something like this:
Criteria q = ....;
q.setFetchMode("parent", FetchMode.SELECT);
....
I have 2 objects joined together defined as such:
public class A {
...
#Id
#Column(name = "A_ID")
#SequenceGenerator(...)
#GeneratedValue(...)
public Long getA_ID();
#OneToOne(mappedBy = "a", fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = B.class)
public B getB();
...
}
#VirtualAccessMethods(get = "getMethod", set = "setMethod")
public class B {
...
#Id
public Long getA_ID();
#MapsId
#OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL ,targetEntity = A.class)
#JoinColumn(name="A_ID")
public A getA();
getMethod(String name);
setMethod(String name, Object value);
...
}
When I go to em.merge(A) with B joined onto A for an INSERT, everything works fine. However if I do the same thing for an update, it will update only A. The update logic is like so:
#Transactional
public void update(Object fieldOnANewValue, Object fieldOnBNewField) {
A objA = em.executeQuery(...) //loads objA by primary key
objA.setFieldOnA(fieldOnANewValue);
B objB = objA.getB(); //lazy loads objB
objB.setMethod("FieldOnB", fieldOnBNewValue);
}
If I look at the logs, there is a SQL UPDATE statement committing the changes I made to A, but nothing for B. If I manually call em.merge(objB) the same issue exists. Does anyone know exactly what EclipseLink does to determine whether or not to generate an UPDATE statement? Particularly with regard to #VirtualAccessMethods? However, I have had the #OneToOne mappings setup differently before and em.merge(objB) worked fine then, plus INSERT works, so I'm not sure if that's the issue. On the flip side, if I have another object that is also joined onto A, but just is a normal POJO like A is, the UPDATE statement is generated for that. Caching is turned off, and I've verified that the objects are updated correctly before merge is called.
Please show the complete code and mappings.
Given you are using virtual access (are you using this correctly?), it could be some sort of change tracking issue related to the virtual access. Does the issue occur without using virtual access?
Try setting,
#ChangeTracking(ChangeTrackingType.DEFERRED)
to see if this has an affect.
You could also try,
#InstantiationCopyPolicy
Hi I have two classes like this:
public class Indicator implements Serializable {
...
#OneToMany(mappedBy = "indicator",fetch=FetchType.LAZY)
private List<IndicatorAlternateLabel> indicatorAlternateLabels;
public List<IndicatorAlternateLabel> getIndicatorAlternateLabels() {
return indicatorAlternateLabels;
}
public void setIndicatorAlternateLabels(List<IndicatorAlternateLabel> indicatorAlternateLabels) {
this.indicatorAlternateLabels = indicatorAlternateLabels;
}
...
}
public class IndicatorAlternateLabel implements Serializable {
...
#ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
#JoinColumn(name = "IndicatorID")
#XmlTransient
private Indicator indicator;
...
}
When I use them like this:
public MetricTypeDetail getMetricTypeDetail(Integer metricTypeId) {
Criteria crit = sessionFactory.getCurrentSession().createCriteria(Indicator.class, "sub")
.add(Restrictions.eq("number", metricTypeId))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).setCacheable(true);
crit.setMaxResults(1);
Indicator indicator=(Indicator) crit.uniqueResult();
MetricTypeDetail metricTypeDetail=new MetricTypeDetail(indicator);
List<IndicatorAlternateLabel> indicatorAlternateLabels = null;
indicatorAlternateLabels=indicator.getIndicatorAlternateLabels();
metricTypeDetail.setIndicatorAlternateLabels(indicatorAlternateLabels);
return metricTypeDetail;
}
This code returns an exception:
failed to lazily initialize a collection of role: com.porism.service.domain.Indicator.indicatorAlternateLabels, no session or session was closed
Any idea? I'm very new to Hibernate
Lazy exceptions occur when you fetch an object typically containing a collection which is lazily loaded, and try to access that collection.
You can avoid this problem by
accessing the lazy collection within a transaction.
Initalizing the collection using Hibernate.initialize(obj);
Fetch the collection in another transaction
Use Fetch profiles to select lazy/non-lazy fetching runtime
Set fetch to non-lazy (which is generally not recommended)
Further I would recommend looking at the related links to your right where this question has been answered many times before. Also see Hibernate lazy-load application design.
It's possible that you're not fetching the Joined Set. Be sure to include the set in your HQL:
public List<Node> getAll() {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("FROM Node as n LEFT JOIN FETCH n.nodeValues LEFT JOIN FETCH n.nodeStats");
return query.list();
}
Where your class has 2 sets like:
public class Node implements Serializable {
#OneToMany(fetch=FetchType.LAZY)
private Set<NodeValue> nodeValues;
#OneToMany(fetch=FetchType.LAZY)
private Set<NodeStat> nodeStats;
}
Try swich fetchType from LAZY to EAGER
...
#OneToMany(fetch=FetchType.EAGER)
private Set<NodeValue> nodeValues;
...
But in this case your app will fetch data from DB anyway.
If this query very hard - this may impact on performance.
More here:
https://docs.oracle.com/javaee/6/api/javax/persistence/FetchType.html
==> 73
as suggested here solving the famous LazyInitializationException is one of the following methods:
(1) Use Hibernate.initialize
Hibernate.initialize(topics.getComments());
(2) Use JOIN FETCH
You can use the JOIN FETCH syntax in your JPQL to explicitly fetch the child collection out. This is somehow like EAGER fetching.
(3) Use OpenSessionInViewFilter
LazyInitializationException often occurs in the view layer. If you use Spring framework, you can use OpenSessionInViewFilter. However, I do not suggest you to do so. It may leads to a performance issue if not used correctly.