Hibernate - choose between multiple collection associations

I'm having trouble choosing hibernate to return a properly populated object graph when select contains joins in many collections.

For instance:

String sql = "select distinct changeset " + "from Changeset changeset " + "join fetch changeset.changeEntries as changeEntry " + "join fetch changeEntry.repositoryEntity as repositoryEntity " + "join fetch repositoryEntity.repository as repository " + "where repository.connectionName = :connectionName"; 
  • There are many ChangeEntries in the change set.
  • There is one RepositoryEntity object in ChangeEntry
  • There is one repository in RepositoryEntity.

The above statement returns the correct data, but the object graph is incorrectly populated - that is, each set of changes contains each ChangeEntry, and not just its own children.

Here are the relevant fragments of these two classes:

 public class Changeset { @NotNull @OneToMany(mappedBy="changeset", targetEntity=ChangeEntry.class, cascade={CascadeType.ALL }, fetch=FetchType.EAGER ) private Set<IChangeEntry> changeEntries; 

A...

 public class ChangeEntry { @NotNull @ManyToOne(targetEntity=Changeset.class) @ForeignKey(name="FkChangeEntryChangeset") private IScmChangeset changeset; @NotNull @ManyToOne(targetEntity=RepositoryEntity.class, cascade=CascadeType.ALL,fetch=FetchType.EAGER) @ForeignKey(name="FkChangeEntryRepoEntity") private IRepositoryEntity repositoryEntity; 

Any help is appreciated

Hello

Marty

+4
source share
2 answers

Marty:

I had the same problem and could not find a suitable solution. You can use simple result transformers to control the number of returned objects:

 CriteriaSpecification.ROOT_ENTITY CriteriaSpecification.DISTINCT_ROOT_ENTITY 

But objects will always contain complete results for their children. I came across a great article using the DTO template along with projections and the aliasToBean transformer.

Here it is: http://swik.net/Hibernate/Hibernate+GroupBlog/Hibernate+3.2:+Transformers+for+HQL+and+SQL/cmxs

It worked well for me. The biggest problem is that you need to create these flattened DTO objects. This is not so important for me, because my specialized DTO objects are used in search results and are used many times, plus I donโ€™t need a lot of them.

I know that this does not directly concern your problem, if you find a way to achieve your goal, please update the message as I would like to see a solution.

+1
source

Does one object load by its identifier? If not, read on. :-)

I'm not sure if using @ForeignKey . Try adding @JoinColumn to many aspects of your relationship. For instance.

 @NotNull @ManyToOne(targetEntity=RepositoryEntity.class, cascade=CascadeType.ALL,fetch=FetchType.EAGER) @ForeignKey(name="FkChangeSet") @JoinColumn(name = "CHANGESET_ID", referencedColumnName = "ID") private Changeset changeset; 

Note. I just saw that you are showing the mapping information for the Repository , but I assume that it looks the same for Changeset .

0
source

All Articles