Friday, January 27, 2017

Hibernate



http://stackoverflow.com/questions/24721688/org-hibernate-persistentobjectexception-detached-entity-passed-to-persist-whe
org.hibernate.PersistentObjectException: detached entity passed to persist
This error can be explained like this (ouf, how do I do this without describing the entire object lifecycle of Hibernate/JPA..?):
With JPA/Hibernate you define classes to be Entitys. What this means is that objects of this class can be managed by Hibernate, and thus be stored in the database.
@Entity
public class Entity { /** ... */ }
When you create a new object of a class that is an Entity (new Entity()), Hibernate knows nothing about it. It is not stored in the database, and is not under the control of Hibernate in any way. If we want these things to happen, we need to persist our object (em.persist(entity)). When we do this, the object state is stored in the database, but also, the object itself becomes managed by Hibernate, which means that Hibernate will keep track of the object's state. In addition, if you have assigned Hibernate to generate id's, the object will be assigned an id.
When you load an object from the database through Hibernate, this object will also be managed by Hibernate. A newly persisted, or a loaded object will both have an id, and will both be managed by Hibernate. But, the connection between the object and Hibernate can be broken! When this happens, the object becomes detached. A detached object is really just an object of an entityclass, that has its id set, but is not managed by Hibernate.
The options we have when it comes to a detached object are different from the ones we have with a managed object, or a new object. Unlike managed objects, detached objects are not allowed to be passed to the persist-method, since they already have an id, which means that they must have been persisted already! When one wants to take a detached object and make it managed, the merge-method is the one to use. (http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#merge(T) )
This will merge the state of the detached object with the database state corresponding to the objects id.
Calling merge with a new object (without id) will assign an id to it and save it, but beware that it will not do anything with the object pass as a parameter, rather it will return the managed object.
public Entity saveDetached(final Entity entity) {
    return em.merge(entity);
} 
In your case, the solution will depend on a few factors, but if the referred Pagina will always be one already existing in the database I would just remove the cascading of the persist operation from it, and keep using em.persist() (merge has its weaknesses..) If it might be new sometimes you would additionally need to add some logic to figure out whether or not to persist it (before persisting the owning object). (I also think it might work to load the Pagina and Produto from the database and assigning them to the new Pagina before persist.)
TL;DR
When reintroducing a detached object to the persistence-context, use the merge-method of the EntityManager, and not the persist-method.


http://myjourneyonjava.blogspot.com/2013/12/difference-between-opensession-and.html
Session openSession() throws HibernateException
It will return a new session object on every call, which is actually a instance oforg.hibernate.impl.SessionImpl.
  • We can use this method when we decided to manage the Session our self.
  • It does not try to store or pull the session from the current context.
  • If we use this method, we need to flush() and close() the session. It does not flush and close() automatically.
 Transaction transaction = null;
       Session session = HibernateUtil.getSessionFactory().openSession();
       try {
           transaction = session.beginTransaction();
           // do Some work
           
           session.flush(); // extra work
           transaction.commit();            
       } catch(Exception ex) {            
           if(transaction != null) {
               transaction.rollback();
           }
           ex.printStackTrace();
       } finally {
           if(session != null) {
               session.close(); // extra work    
           }            
       }   
Session getCurrentSession() throws HibernateException

Obtains the current session. The "current session" refers to a Hibernate Session bound by Hibernate behind the scenes, to the transaction scope.

A session is opened whenever getCurrentSession() is called for the first time and closed when the transaction ends. This creates a brand new session if one does not exist or uses an existing one if one already exists. It automatically configured with both auto-flush and auto-close attributes as true means Session will be automatically flushed and closed.

We can use getCurrentSession() method when our transaction runs long time. To use this method we need to configure one property in hibernate.cfg.xml file
https://www.mkyong.com/hibernate/hibernate-transaction-handle-example/
try{
 session = HibernateUtil.getSessionFactory().openSession();
 tx = session.beginTransaction();
 tx.setTimeout(5);

 //doSomething(session);

 tx.commit();


}catch(RuntimeException e){
 try{
  tx.rollback();
 }catch(RuntimeException rbe){
  log.error("Couldn’t roll back transaction", rbe);
 }
 throw e;
}finally{
 if(session!=null){
  session.close();
 }
}
Hibernate error - QuerySyntaxException: users is not mapped [from users]
http://stackoverflow.com/questions/9954590/hibernate-error-querysyntaxexception-users-is-not-mapped-from-users
In the HQL , you should use the java class name and property name of the mapped @Entityinstead of the actual table name and column name , so the HQL should be :
List<User> result = (List<User>) session.createQuery("from User").list();

http://blog.jhades.org/how-does-spring-transactional-really-work/
@Configuration
public class EntityManagerFactoriesConfiguration {
    @Autowired
    private DataSource dataSource;
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean emf() {
        LocalContainerEntityManagerFactoryBean emf = ...
        emf.setDataSource(dataSource);
        emf.setPackagesToScan(
            new String[] {"your.package"});
        emf.setJpaVendorAdapter(
            new HibernateJpaVendorAdapter());
        return emf;
    }
}

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts