hibernate

How can i resolve the N+1 Selects problem ?

Hello everyone, I have trouble understanding how to avoid the n+1 select in jpa or hibernate. From what i read, there's the 'left join fetch', but i'm not sure if it still works with more than one list (oneToMany).. Could someone explain it to me, or give me a link with a clear complete explanation please ? I'm sorry if this is a noo...

How to deal with databases for websites written in Java, more specifically Wicket?

Hi there. I'm new to website development using Java but I've got started with Wicket and make a little website. I'd like to expand on what I've already made (a website with a form, labels and links) and implement database connectivity. I've looked at a couple of examples, in example Mystic Paste, and I see that they're using Hibernate ...

NULL handling with subselect in Hibernate Criteria API

I'm constructing a Hibernate Criterion, using a subselect as follows DetachedCriteria subselect = DetachedCriteria.forClass(NhmCode.class, "sub"); // the subselect selecting the maximum 'validFrom' subselect.add(Restrictions.le("validFrom", new Date())); // it should be in the past (null needs handling here) subselect.add(Property.f...

Hibernate one-to-one: getId() without fetching entire object

I want to fetch the id of a one-to-one relationship without loading the entire object. I thought I could do this using lazy loading as follows: class Foo { @OneToOne(fetch = FetchType.LAZY, optional = false) private Bar bar; } Foo f = session.get(Foo.class, fooId); // Hibernate fetches Foo f.getBar(); // Hibernate fetch...

Why is hibernate returning a proxy object?

I have a service method that calls a DAO which then returns an object from the database. This method is called from numerous parts of the system. However, one particular method is getting a return type of ObjectClass_$$_javassist_somenumber as the type. Which is throwing things off. I call the service method exactly the same as everywher...

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

I am reading Hibernate in Action and the author suggests to move business logic into our domain models (p. 306). For instance, in the example presented by the book, we have three entities named Item, Bid, and User and the author suggests to add a placeBid(User bidder, BigDecimal amount) method to the Item class. Considering that usually...

Hibernate Criteria: Add restrictions to Criteria and DetachedCriteria

Currently our queries add a variety of Restrictions to ensure the results are considered active or live. These Restrictions are used in several places/queries so a method was setup similar to public Criteria addStandardCriteria(Criteria criteria, ...) { // Add restrictions, create aliases based on parameters // and othe...

Can I use JPA/EJB3 on a table that was created at runtime?

This is weird and probably not possible but I'll ask anyway. I'm making this app that reads in a meta file and creates some tables then populates them with data. I was wondering if I could somehow use JPA to populate those tables. Obviously, there's no way I could have an entity with annotations on it since the table didn't exist at c...

ManyToMany Relation does not create the primary key

Hello, I have a ManyToMany relationship between two classes: ClassA and ClassB, but when the table for this relationship (table called objectA_objectB) there is no primary key on it. In my ClassA I have the following: @ManyToMany(fetch=FetchType.LAZY) @OrderBy(value="name") @JoinTable(name="objectA_objectB", joinColumns= ...

Hibernate Transient Extends problem

@MappedSuperclass public abstract class BaseAbstract implements Serializable{ private static final long serialVersionUID = 1L; protected String test = //some random value; public String getTest() { return test; } public void setTest(String test){ this.test = test; } } @Entity public class Artist extends BaseAbstract { p...

JPA query for getting the whole tree

Hello, I have a class which models all categories and they can be ordered hierarchically. @Entity @Table(name="categories") public class Category { @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequence") @SequenceGenerator(name="sequence", sequenceName="categories_pk_seq", allocationSize=1) @Column(n...

Problem connecting to Apache Derby using Hibernate configuration file in Netbeans (ERROR XSDB6)

I've created a local Apache Derby database in Netbeans, but am having problems when I try and autogenerate the POJO files, using the "Hibernate Reverse Engineering Wizard". My Hibernate configuration (generated by Netbeans from the database connection, then I added a few bits): <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate...

Avoid having a huge collection of ids by calling a DAO.getAll()

Instead of returning a List<Long> of ids when calling PersonDao.getAll() we wanted not to have an entire collection of ids in memory. Seems like returning a org.springframework.jdbc.support.rowset.SqlRowSet and iterate over this rowset would not hold every object in memory. The only problem here is i cannot cast this row to my entity. ...

1:M relationship in Hibernate and cascading operations

Table SUBCOURSE references COURSE COURSE(id, name) SUBCOURSE(id, course_id, name) So, 1:M. Hibernate generates for Course: @OneToMany(fetch = FetchType.LAZY, mappedBy = "course", cascade = CascadeType.ALL) public Set getSubCourses() { return this.subCourses; } for Subcourse it generates @ManyToOne(fetch = ...

Storage model for various user setting and attributes in database?

I'm currently trying to upgrade a user management system for one web application. This web application is used to provide remote access to various networking equipment for educational purposes. All equipment is assigned to various pods, which users can book for periods of time. The current system is very simple - just 2 user types: admi...

Java back-door interface, or, restricting method access by calling object

I'm using Java. I want to have a setter method of one class that is only accessible to an "owning" object of another class. I'll be more specific: I have Node and Port interfaces. Every node has a set of Ports, and each Port belongs to a single Node. A Port may be enabled or disabled, handled with a simple boolean property. However...

Hibernate Criteria: Perform JOIN in Subquery/DetachedCriteria

I'm running into an issue with adding JOIN's to a subquery using DetachedCriteria. The code looks roughly like this: Criteria criteria = createCacheableCriteria(ProductLine.class, "productLine"); criteria.add(Expression.eq("productLine.active", "Y")); DetachedCriteria subCriteria = DetachedCriteria.forClass(Model.class, "model"); subCr...

Confused about distinct/aggregate queries with (N)Hibernate

I'm not sure how to approach queries that don't map 1:1 to my persistent entities - in other words, distinct and aggregate queries. For example, I need to retrieve a distinct list of property values for populating a drop-down list. Should I write a class and a mapping for the "entities" that are returned by this query? Or should I just u...

org.hibernate.TransientObjectException during Criteria.list()

I have seen posts all over the internet that talk about how to fix the TransientObjectExceptions during save/update/delete but I am having this problem when calling list on my Criteria. I have two objects A and B. A has a field named b which is of type B. In my mapping b is mapped as a many-to-one. This all runs in a larger persisten...

Help regarding composite pattern with hibernate

Hello, So i am stuck, i am creating a gwt web application, i will be using a tree(gwt Tree and TreeItems) structure to show a list of folders(class Folder) and files(class FileLocation), the folder and filelocation class will all implement a Hierarchy interface basing the classes on the composite pattern. but i am using hibernate to sto...