What datastructures in Java that corresponds to what Hibernate collection type

Hi everyone,

This could be a stupid question, but here it goes.

I am working on evaluating the tool that I have migrated (https://autofetch.wordpress.com/) and I have come across some doubts when I need to wrap the collections in the existing annotation based mapping for the target project I am evaluating. This is done in order for the entities to be trackable so that I can log statistics to what extent the entity is accessed in different stages of the application. This should be done automatically, but I haven’t found a good way to implement this in Hibernate yet. Now to the problem.

In my test case the wrapping of these collections was pretty straightforward, but now with the project that I need to evaluate it is less obvious since I don’t know the new entities that well.

How I did it in my test is like you can see here:

@Tuplizer(impl = AutofetchTuplizer.class)
public class Employee {

    @Column(name = "employee_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long m_id;

    @Column(name = "name")
    private String m_name;
    @JoinColumn(name = "supervisor_id")
    @ManyToOne(cascade = {CascadeType.ALL})
    private Employee m_supervisor;

    @JoinColumn(name = "supervisor_id")
    @OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
    private Set<Employee> m_subordinates;

So I simply use the @CollectionType to wrap the object. However, what makes me confused is in what situation I should wrap with Set, List, Bag, and IdBag, which I have created custom wrappers for. Can I see based on the mapping what type of data structure that should be used? The most troublesome is when to use the Bag types. Or is it just so simple that you just wrap a list with AutofetchList, Set with AutofetchSet, Bag with AutofetchBag, and IdBag with AutofetchIdBag?

I think my confusion is that I don’t fully understand when to use the Bag types, and that I haven’t seen Bag or IdBag being used in the mapping of an entity before. When I look in the code I see mostly just collections in the shape of Sets and Lists, never Bags. Also if I accidentally wrap a collection wrong, what would be the consequences? Worse performance or wouldn’t it even run?

Thanks for all the help!

Bag is any unidirectional @OneToMany List. IdBag is when you add @OrderColumn to a unidirectional @OneToMany.

1 Like