Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Saturday, 20 December 2014

How to hibernate Gnome Shell

Install the relative plugin found on https://extensions.gnome.org/extension/755/hibernate-status-button/

Friday, 27 January 2012

JPA+Hibernate: How to show the bounded values instead of the question marks on the queries

log4j.properties:
#log4j.logger.org.hibernate=INFO, hb
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
#log4j.logger.org.hibernate.hql.ast.AST=info
#log4j.logger.org.hibernate.tool.hbm2ddl=warn
#log4j.logger.org.hibernate.hql=debug
#log4j.logger.org.hibernate.cache=info
#log4j.logger.org.hibernate.jdbc=debug

#log4j.appender.hb=org.apache.log4j.ConsoleAppender
#log4j.appender.hb.layout=org.apache.log4j.PatternLayout
#log4j.appender.hb.layout.ConversionPattern=HibernateLog --> %d{HH:mm:ss} %-5p %c - %m%n
#log4j.appender.hb.Threshold=TRACE

Rereferences

http://stackoverflow.com/questions/2536829/hibernate-show-real-sql

Friday, 11 November 2011

JPA/Hibernate reminder: initialize all fields! if not then set it to null

If I don't initialize a field then I get a transient exception.
If I initialized it by calling a class constructor then the new object is
persisted to db. What if I want this Foreign Key (FK) to be empty?

solution: If I initialize it to null then everything it's ok (why?).

Note: If I remove the CascadeType.ALL then I get again the transient exception

References

http://cagataycivici.wordpress.com/2005/11/15/pivefacedwith/

JPA error: "Use of @OneToMany or @ManyToMany targeting an unmapped class"

Possible reason 2: target

I had two classes with bi-directional @ManyToMany relationship.
They both return Sets i.e. Generics therefore I have to set
target. But I did the mistake to set it to Set but they right
was setting it to the types of the elements the Sets include.
@Entity
@Table(name="A")
public class A implements Serializable {

 //@ManyToMany(...,targetEntity=Set.class) //wrong
 @ManyToMany(...,targetEntity=A.class) //right
 public Set getManyB(){...}

}

@Entity
@Table(name="B")
public class B implements Serializable {

 @ManyToMany(...,targetEntity=Set.class) //wrong
 @ManyToMany(...,targetEntity=A.class) //right
 @JoinTable(...)
 public Set getManyA(){...}
}

Tuesday, 8 November 2011

JPA EntityManager: persist() doesn't produce sql statement 'insert', i.e. doesn't save object in db

MySQL (/etc/mysql/my.cnf):

default-table-type=innodb

Note: InnoDB supports transactions but the default table type doesn't!

Use org.springframework.orm.jpa.JpaTransactionManager (not org.springframework.orm.hibernate3.HibernateTransactionManager)

Don't use org.springframework.orm.hibernate3.HibernateTransactionManager because you will have to
handle all the transaction management (open,close,flush) and if you forget to do so the persist()
and merge() will not product sql statements insert and updates, i.e. will not save objects into db.
(see reference)

Are transactions enabled? (are you sure?):

Spring @Transactional: Verifying transaction support / Local method calls (RECOMMENDED)

Transaction strategies: Understanding transaction pitfalls


Spring reference documentation of transactions

Monday, 7 November 2011

JPA/Hibernate: @PrePersist, @PreUpdate, @PostLoad don't work!

If using Hibernate:

  • if using the Session API: @PrePersist, @PreUpdate wont't work (see 1)
    [solution(see 2): register an event listener to hibernate events]
  • if using the EntityManager API: @PrePersist, @PreUpdate will work

 

If using pure JPA: @PrePersist, @PreUpdate work fine!

 

References
(1),
(2)

Friday, 4 November 2011

Hibernate error: org.hibernate.MappingException: Could not determine type for: java.util.List

solution: move JPA annotations to getters and remove them from fields
eg (wrong):
@OneToMany
private List foos;
public List getFoos() { return foos; }
public void setFoos(List foos) { this.foos = foos; }

eg (right):
private List foos;
@OneToMany
public List getFoos() { return foos; }
public void setFoos(List foos) { this.foos = foos; }