Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: ElementCollection and ObservableList
PostPosted: Tue Sep 12, 2017 1:51 pm 
Newbie

Joined: Tue Sep 12, 2017 1:26 pm
Posts: 4
Hello,

I am developing a front end for on site sever maintanance and use JavaFX. I am facing a single problem with my following class:

Code:
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;

import javax.persistence.*;
import java.util.List;

@Entity
public final class Person {

   private int id;

   @Id
   public int getId() { return id; }
   public void setId(int id) { this.id = id; }

   @Transient
   private final StringProperty nameProperty = new SimpleStringProperty("");
   public StringProperty nameProperty() { return nameProperty; }

   @Column
   public String getName() { return nameProperty.get(); }
   public void setName(String name) { nameProperty.set(name); }

   @Transient
   private final ListProperty<String> contactsProperty = new SimpleListProperty<>(FXCollections.observableArrayList());
   public ListProperty<String> contactsProperty() { return contactsProperty; }

   @ElementCollection
   public List<String> getContacts() { return contactsProperty.get(); }
   public void setContacts(List<String> contacts) { contactsProperty.set(FXCollections.observableList(contacts)); }
}


I can do everything normally with it like change the name, add/remove contacts and flush them. No problem. But if I call refresh on a Person object it doesn't update the contacts list. It seems that Hibernate doesn't want to touch the ObservableList on refresh calls. (All other calls like persist, flush, find etc. work fine)

Is this a bug or is it unsupported to use custom collections?


Top
 Profile  
 
 Post subject: Re: ElementCollection and ObservableList
PostPosted: Tue Sep 12, 2017 3:11 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1586
Location: Romania
Most likely is due to this method:

Code:
public void setContacts(List<String> contacts) { contactsProperty.set(FXCollections.observableList(contacts)); }


When you call setContacts, you never set the contacts property so Hibernate can never propagate the changes to the DB.

Also, what's the reason you are mixing field-based access with property-based access? Either you use one or the other.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: ElementCollection and ObservableList
PostPosted: Tue Sep 12, 2017 3:38 pm 
Newbie

Joined: Tue Sep 12, 2017 1:26 pm
Posts: 4
Quote:
When you call setContacts, you never set the contacts property so Hibernate can never propagate the changes to the DB.


Where do I not set the contactsProperties?
Code:
contactsProperty.set(FXCollections.observableList(contacts));


And why does Hibernate update the DB Table/Object correctly, when I call:

Code:
//works
entityManager.getTransaction().begin();
entityManager.persist(person);
entityManager.getTransaction().commit();

############################################

//works
person.getContacts().add("SomeContact");
entityManager.getTransaction().begin();
entityManager.flush();
entityManager.getTransaction().commit();

############################################

//works
person.contactsProperty().add("SomeContact");
entityManager.getTransaction().begin();
entityManager.flush();
entityManager.getTransaction().commit();

############################################

//works
entityManager.getTransaction().begin();
Person person = entityManager.find(Person.class, id);
entityManager.getTransaction().commit();


It only fails to update the object correctly when I call

Code:
//fails (contents of contacts is the same even if the db table changed)
entityManager.refresh(person);


Quote:
Also, what's the reason you are mixing field-based access with property-based access? Either you use one or the other.


I don't understand, what you mean here. I follow the JavaFX Beans convention which looks like this (http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm):

Code:
private Property<T> fieldProperty;
public Property<T> fieldProperty() { return fieldProperty; }

public T getField() { return fieldProperty.get(); }
public void setField(T field) { fieldProperty.set(field); }


Isn't the ability to annotate the getter specifically for the case, where the field is not persistable? (when getter/setter does custom stuff)


Top
 Profile  
 
 Post subject: Re: ElementCollection and ObservableList
PostPosted: Wed Sep 13, 2017 3:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1586
Location: Romania
Quote:
Isn't the ability to annotate the getter specifically for the case, where the field is not persistable? (when getter/setter does custom stuff)


Nope. If you have a non-persistable field, you marked it with @Transient. If you're using field-based access, the getter/setters or any other method will not be taken into consideration so you don't need to annotate them.

As for refresh, what exactly is that it is not working? Is it that the refresh is not cascaded to the contacts?

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: ElementCollection and ObservableList
PostPosted: Wed Sep 13, 2017 4:15 am 
Newbie

Joined: Tue Sep 12, 2017 1:26 pm
Posts: 4
Quote:
Nope. If you have a non-persistable field, you marked it with @Transient. If you're using field-based access, the getter/setters or any other method will not be taken into consideration so you don't need to annotate them.


Thanks! I didn't know that.

Quote:
As for refresh, what exactly is that it is not working? Is it that the refresh is not cascaded to the contacts?


Exactly. After a refresh the contents of the contacts list (in Person) and the contacts table (in the DB) are not identical.


Top
 Profile  
 
 Post subject: Re: ElementCollection and ObservableList
PostPosted: Wed Sep 13, 2017 5:33 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1586
Location: Romania
Quote:
Exactly. After a refresh the contents of the contacts list (in Person) and the contacts table (in the DB) are not identical.


If you can replicate it using our test case templates, then you should open a Jira issue for it.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: ElementCollection and ObservableList
PostPosted: Wed Sep 13, 2017 8:10 am 
Newbie

Joined: Tue Sep 12, 2017 1:26 pm
Posts: 4
I created an issue: https://hibernate.atlassian.net/browse/HHH-11983


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 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.