简体   繁体   中英

JPA in SE/FX: how to integrate entities & data model used in TableView

Using: Java SE 8, JavaFX 8, JPA 2.1. I'm learning JavaFX 8 (integrated with jdk 8), I already know SE and EE. so I already worked with JPA in EE web applications. my question is: When you create a table in a JavaFX application, it is a best practice to implement a class that defines the data model and provides methods and fields to further work with the table. for Example the Person class is used to define data in an address book

public class Person {   

    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;

so how entities fit in this situation, I mean there is the Person ENTITY, and the Person data model.

And the questions already here do not answer my question. Thanks

I can say that you can create your entity just as you used to do for your web application ,

@Entity
@Table(name="Person")
public class Person {   
//..  your column and setters and getters
}

personDao :

public interface PersonDao{

public List<Person> listPerson();
//....
}

PersonDaoImpl :

public class PersonDaoImpl implements PersonDAO {


@Override
public List<Person> listPerson() {
    // TODO Auto-generated method stub
    List<Person> list=new ArrayList<>();
    Session s=HibernateUtil.openSession();
    s.beginTransaction();
    list=s.createQuery("from Person").list();
    s.getTransaction().commit();
    s.close();
    return list;
}

PersonService :

public interface PersonService  {
  public List<Person> listPerson();
}

PersonServiceImpl :

public class PersonServiceImpl   implements PersonService{
private  PersonDao personDAO = new PersonDaoImpl();

@Override
public List<Person> listPerson() {
    // TODO Auto-generated method stub
    return  personDAO.listPerson();
}

then you can just use it in your controller :

   public class ScreenController implements Initializable{

public ObservableList<Person> data; 

private PersonService personService=new PersonServiceImpl(); 


@FXML
private TableView<Person> table_person;
@FXML
private TableColumn<Person, String> firstName;
@FXML
private TableColumn<Person, String> lastName;

@Override
public void initialize(URL url, ResourceBundle rb) {

firstName.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName")); 
lastName.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));

data  =  FXCollections.observableArrayList();  
data.addAll(personService.listPerson());
table_person.setItems(data);

//... 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM