how to lazy load collection when using spring-data-jpa, with hibernate, from an console application

One solution can be to make User.orders an eagerly fetched collection by @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER) private Set<Order> orders = new HashSet<Order>(); Entity associations are lazily loaded by default. This means that the orders Set is actually just a proxy object that won’t get initialized until you invoke a method on it. This is good, … Read more

Hibernate unidirectional one to many association – why is a join table better?

Consider the situation where the owned entity type can also be owned by another parent entity type. Do you put foreign key references in the owned table to both parent tables? What if you have three parent types? It just doesn’t scale to large designs. A join-table decouples the join, so that the owned table … Read more

Is it a good idea to “migrate business logic code into our domain model”?

As said We have a distinct layer for business logic (usually called Service layer) Domain-Driven-Design (DDD) states you should put business logic inside your domain model. And, believe me, it is really good. As said by POJO in Action book about Service layer It is Use Case driven It can define Transaction boundaries Before @Service … Read more

How to disable hibernate caching

Can someone please answer how to disable caching in persistence.xml. The second-level cache and query cache are disabled by default (and queries are not cached unless you explicitly cache them). The first-level cache can’t be disabled. I tried to disable by changing properties (…) This would disable the second-level cache and query cache, if they … Read more

Is it possible to use Hibernate with PostgreSql’s JSONB data type?

Thanks Vlad Mihalcea we have such opportunity! ) He created hibernate-types lib: <dependency> <groupId>com.vladmihalcea</groupId> <artifactId>hibernate-types-52</artifactId> <version>2.1.1</version> </dependency> which adds a support of ‘json’, ‘jsonb’ and other types to Hibernate: @Data @NoArgsConstructor @Entity @Table(name = “parents”) @TypeDefs({ @TypeDef(name = “string-array”, typeClass = StringArrayType.class), @TypeDef(name = “int-array”, typeClass = IntArrayType.class), @TypeDef(name = “json”, typeClass = JsonStringType.class), @TypeDef(name … Read more

How to use Hibernate annotations @ManyToOne and @OneToMany for associations

What’s wrong is the following: @OneToMany(fetch = FetchType.LAZY) @JoinColumn(name=”idEmployees”) public List<Task> getTasks() { return tasks; } And it’s wrong for two reasons. @JoinColumn(name=”idEmployees”) means: this OneToMany is mapped using a join column (i.e. a foreign key) named idEmployees. But the join column is not named idEmployees. idEmployees is the primary key of the Employee table. … Read more

Hibernate : No CurrentSessionContext configured

As per the best of my knowledge, your configuration is not proper for current session. Instead of <property name=”hibernate.current_session_context_class”>org.hibernate.context.ThreadLocal‌​SessionContext</property> use <property name=”hibernate.current_session_context_class”>thread</property> For more information on this, please visit this link: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/architecture.html#architecture-current-session Specially, read the last lines of last paragraph. For those who’re using Hibernate 4.1, <property name=”hibernate.current_session_context_class”>org.hibernate.context.internal.ThreadLocalSessionContext</property> Reference: https://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/context/internal/ThreadLocalSessionContext.html