-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Why does Hibernate does not get duplicate list data
PostPosted: Fri Mar 04, 2016 4:21 pm 
Newbie

Joined: Fri Mar 04, 2016 4:16 pm
Posts: 7
Hi,
I have a list created like this :
Code:
   @ManyToMany(cascade={CascadeType.REFRESH}, fetch = FetchType.EAGER)
   @Fetch(FetchMode.SELECT)
   @JoinColumn
   @OrderColumn
   private List<User> user= new ArrayList<User>();


The thing is we can have concurrent acces and the list can get more than 1 element in the same order :
Quote:
User_id User_name Order
1 Bob 0
2 Foo 1
3 Marcel 1


And for some reason, hibernate only gets the 0-1 order and not 0-1-1.
On DataNucleus it gets all the item and when it persist back it sets the order correctly.

Is there any ways to get all Item of a list even if order are the same?

Thanks


Top
 Profile  
 
 Post subject: Re: Why does Hibernate does not get duplicate list data
PostPosted: Sat Mar 05, 2016 1:46 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Try with an @OrderColumn as explained in this article.


Top
 Profile  
 
 Post subject: Re: Why does Hibernate does not get duplicate list data
PostPosted: Sun Mar 06, 2016 3:16 pm 
Newbie

Joined: Fri Mar 04, 2016 4:16 pm
Posts: 7
I already have @OrderColumn.
When we print out the sql, hibernate gets all the list, but then strip out what is duplicated order.
If we have a list with 0-10 order and remove the 0, hibernate will return an empty list.
Is there a reason for that?
Hibernate should just select all item even if the order is not correct.

Thanks


Top
 Profile  
 
 Post subject: Re: Why does Hibernate does not get duplicate list data
PostPosted: Sun Mar 06, 2016 4:01 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
I don't understand the exact problem. For instance, the @JoinColumn doesn't have much sense on the ManyToMany relationship. Try the examples for the docs or even better, try using two @OneToMany associations as explained in the User Guide.


Top
 Profile  
 
 Post subject: Re: Why does Hibernate does not get duplicate list data
PostPosted: Sun Mar 06, 2016 9:45 pm 
Newbie

Joined: Fri Mar 04, 2016 4:16 pm
Posts: 7
Let's take for example a simple List<> of any type with @OrderColumn.
In MySQL DB a table will looks like
some_field order_id <-- that is the order used to reconstruct the list.

the order id will start from 0 to n. If, for example, you go in the database directly add another row with the same order of another one, like 4.
The the order will looks like 0, 1, 2, 3, 4, 4, 5.. n.
When you get the list from hibernate it will only return the row with the first 4. The second 4 will get removed.

If instead of adding another row you delete the row with order 0 and then get the list from hibernate, hibernate will crash because there is no order 0.

Is there a way for Hibernate to just get all the data and then order then by order column? So I would get 0 1 2 3 4 4..n instead of just 0 1 2 3 4..n
Or 1, 2, 3 ,4..n instad of nothing.

Am I clearer now?

Thanks


Top
 Profile  
 
 Post subject: Re: Why does Hibernate does not get duplicate list data
PostPosted: Mon Mar 07, 2016 1:33 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Sure it is. You can use a bidirectional one-to-many association and have the order_id mapped as a regular orderId property on the child-side.
Then, on the mappedBy @OneToMany, you just add the @OrderColumn(name = "orderId") or an @OrderBy("orderId)


Top
 Profile  
 
 Post subject: Re: Why does Hibernate does not get duplicate list data
PostPosted: Tue Mar 08, 2016 9:47 am 
Newbie

Joined: Fri Mar 04, 2016 4:16 pm
Posts: 7
If I understand correctly, I will have to handle the order manually, is that right?

Thanks


Top
 Profile  
 
 Post subject: Re: Why does Hibernate does not get duplicate list data
PostPosted: Tue Mar 08, 2016 11:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Hibernate can take care of the order if you use those annotations.


Top
 Profile  
 
 Post subject: Re: Why does Hibernate does not get duplicate list data
PostPosted: Tue Mar 08, 2016 2:05 pm 
Newbie

Joined: Fri Mar 04, 2016 4:16 pm
Posts: 7
If I do something like this :

Code:
public class User {
...
   @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
   @OrderColumn(name="orderId")
   @JoinColumn
   private List<Test> test = new ArrayList<Test>();
...
}

public class Test {
...
// Get/set
...
   private Integer orderId;
   
   private String text;
   @Id @GeneratedValue private Long id;
}


Hibernate will auto generate orderID.
After creating some test if I go into the database which looks like this :
Code:
id   orderID text   user_id
41   0   0   5
42   1   1   5
43   2   2   5
44   3   3   5
45   4   4   5
46   5   5   5
47   6   6   5
48   7   7   5
49   8   8   5
50   9   9   5


And move the orderID from 9 to 8,

Code:
id   orderID text   user_id
41   0   0   5
42   1   1   5
43   2   2   5
44   3   3   5
45   4   4   5
46   5   5   5
47   6   6   5
48   7   7   5
49   8   8   5
50   8   9   5  <-- Now orderID 8


If I do a simple get of the user and then for each on all "Test" it will print
Code:
0
1
2
3
4
5
6
7
9


If I change from @OrderColumn to @OrderBy without deleting generated table, it works. I get 1 to 9 with 8. But if I start anew (drop table) and put @OrderBy, it does not auto generate orderID.

Is there anyways to let Hibernate autogenerate orderID and still let me fetch every entry, or do I have to handle the order id myself?


Top
 Profile  
 
 Post subject: Re: Why does Hibernate does not get duplicate list data
PostPosted: Tue Mar 08, 2016 3:56 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
I suggest you read the new User Guide because it explains all these mapping in details.
Unidirectional associations use a link table which is where the order column goes too.
What you want is a bidirectional ordered list which can have the order column in the child table.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.