-->
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.  [ 11 posts ] 
Author Message
 Post subject: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Tue Aug 29, 2017 1:46 am 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
I have a Springboot project. Its also using Hibernate and Thymeleaf - and I am experiencing different behavior in my form depending whether I use a Set or List between my two domain objects Episode and Person and the relationship between them is held in a entity (EpisodePerson) as there is an additional field (EpisodeRole) I record on the join.

Person

@Entity
public class Person {

private String name;
etc...

Episode

@Entity
public class Episode {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "episode", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<EpisodePerson> persons = new ArrayList<EpisodePerson>();

EpisodePerson

@Entity
public class EpisodePerson {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
private Person person;

@ManyToOne
private Episode episode;

public enum EpisodeRole {
Goodie, Baddie, Witness,
}

@Column
@Enumerated(EnumType.STRING)
private EpisodeRole episodeRole;

My Episode form in html has a group of fields for capturing details about each person. There is always a minimum of two people on the form, so person[0] and person[1] are hard coded.

Any more people are added via a button which uses jquery to clone the person fieldset. I have been using the same form to display existing Episodes as I do to add a new Episode.

A typical HTML line looks like:

<input type="text" class="form-control person" data-property="firstname" placeholder="Firstname"
name="firstname" th:field="*{persons[1].person.firstname}"/>

Which brings me to the question- why is it that if my domain objects are setup to use Set then I get array out of bounds when loading the page for a new Episode?

But if using List there is no error?

It maybe that there is a better approach to the whole way people are displayed on the form.

Thanks


Top
 Profile  
 
 Post subject: Re: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Tue Aug 29, 2017 3:02 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
Which brings me to the question- why is it that if my domain objects are setup to use Set then I get array out of bounds when loading the page for a new Episode?


That's a question for the Thymeleaf forum and how they manage to implement the persons[1] syntax. For a Set, what would that mean since there is no index-based ordering?

Anyway, changing the type of a persisted collections from List to Set just to please Thymeleaf shouldn't be the right approach when designing your system. You also have to take into consideration whether you need a certain ordering.

Without application or database-level ordering, there is no such guarantee. So, why would persons[0] or persons[1] mean when you have 3 elements. If you didn't specify a certain order, you cannot get consistent results.


Top
 Profile  
 
 Post subject: Re: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Tue Aug 29, 2017 4:13 am 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
vlad wrote:
Quote:
Which brings me to the question- why is it that if my domain objects are setup to use Set then I get array out of bounds when loading the page for a new Episode?


That's a question for the Thymeleaf forum and how they manage to implement the persons[1] syntax. For a Set, what would that mean since there is no index-based ordering?

Anyway, changing the type of a persisted collections from List to Set just to please Thymeleaf shouldn't be the right approach when designing your system. You also have to take into consideration whether you need a certain ordering.

Without application or database-level ordering, there is no such guarantee. So, why would persons[0] or persons[1] mean when you have 3 elements. If you didn't specify a certain order, you cannot get consistent results.


So the episode always has a minimum of two people, often more (average about 4).

I thought I could send episode to the view with two empty people to satisfy the first two - but this solution I do not think is very good.

What do you suggest?

Al


Top
 Profile  
 
 Post subject: Re: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Tue Aug 29, 2017 4:31 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
I thought I could send episode to the view with two empty people to satisfy the first two - but this solution I do not think is very good.


Nope, it's not. Why don't you use a for-loop in the UI to display the people collection? You don't need to assume any particular size. Just display dinamically whatever is coming from the DB.


Top
 Profile  
 
 Post subject: Re: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Tue Aug 29, 2017 4:41 am 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
vlad wrote:
Quote:
I thought I could send episode to the view with two empty people to satisfy the first two - but this solution I do not think is very good.


Nope, it's not. Why don't you use a for-loop in the UI to display the people collection? You don't need to assume any particular size. Just display dinamically whatever is coming from the DB.


There is a th:each - but persons 1 and 2 need to be in a certain part of the form, and 3 on wards in another part.

I could combine th:if (condition past index 3) for one part of the form and corresponding part for the first two.

Its working now with lists, which on the face of it should be better sinced they are indexed and kept in order? (I dont want the order of the persons in the list to change)


Top
 Profile  
 
 Post subject: Re: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Tue Aug 29, 2017 4:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
I could combine th:if (condition past index 3) for one part of the form and corresponding part for the first two.


I'd do it as you suggested. You can use the iteration index so that the first and the second entries are displayed separately from the rest.


Top
 Profile  
 
 Post subject: Re: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Tue Aug 29, 2017 2:42 pm 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
vlad wrote:
Quote:
I could combine th:if (condition past index 3) for one part of the form and corresponding part for the first two.


I'd do it as you suggested. You can use the iteration index so that the first and the second entries are displayed separately from the rest.


Ok, so when a new episode is sent to the view with no persons attached, I would need a 'add person' button. Or, as I said because it has at least two people I can initalize person 0 and person 1?


Top
 Profile  
 
 Post subject: Re: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Tue Aug 29, 2017 2:48 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Quote:
Ok, so when a new episode is sent to the view with no persons attached, I would need a 'add person' button. Or, as I said because it has at least two people I can initialize person 0 and person 1?


That depends on your app logic. If there is no person attached, then the for-each block will not be executed and you won't get any exception. If there must be 2 entries, then the user must fill in the data for those persons, right? So, you need to allow the user to enter that data.

However, this has nothing to do with Hibernate, so you should ask this question on the Thymeleaf forum to see how you can do this according to your application requirements.


Top
 Profile  
 
 Post subject: Re: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Tue Aug 29, 2017 9:55 pm 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
vlad wrote:
Quote:
Ok, so when a new episode is sent to the view with no persons attached, I would need a 'add person' button. Or, as I said because it has at least two people I can initialize person 0 and person 1?


That depends on your app logic. If there is no person attached, then the for-each block will not be executed and you won't get any exception. If there must be 2 entries, then the user must fill in the data for those persons, right? So, you need to allow the user to enter that data.

However, this has nothing to do with Hibernate, so you should ask this question on the Thymeleaf forum to see how you can do this according to your application requirements.


There is a lot of talk about why NOT to use Set in MVC :

https://stackoverflow.com/questions/170 ... lating-set


Top
 Profile  
 
 Post subject: Re: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Tue Aug 29, 2017 10:17 pm 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
bigal.nz wrote:
vlad wrote:
Quote:
Ok, so when a new episode is sent to the view with no persons attached, I would need a 'add person' button. Or, as I said because it has at least two people I can initialize person 0 and person 1?


That depends on your app logic. If there is no person attached, then the for-each block will not be executed and you won't get any exception. If there must be 2 entries, then the user must fill in the data for those persons, right? So, you need to allow the user to enter that data.

However, this has nothing to do with Hibernate, so you should ask this question on the Thymeleaf forum to see how you can do this according to your application requirements.


There is a lot of talk about why NOT to use Set in MVC :

https://stackoverflow.com/questions/170 ... lating-set


And perhaps that backing object is the answer:

https://stackoverflow.com/questions/952 ... l-problems


Top
 Profile  
 
 Post subject: Re: Set vs List in Springboot / Hibernate / Thymeleaf project
PostPosted: Wed Aug 30, 2017 1:16 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That way is easier to encapsulate the logic and make sure you always have at least two persons for Episode.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.