简体   繁体   English

SE / FX中的JPA:如何集成TableView中使用的实体和数据模型

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

Using: Java SE 8, JavaFX 8, JPA 2.1. 使用:Java SE 8,JavaFX 8,JPA 2.1。 I'm learning JavaFX 8 (integrated with jdk 8), I already know SE and EE. 我正在学习JavaFX 8(与jdk 8集成),我已经知道SE和EE。 so I already worked with JPA in EE web applications. 因此我已经在EE Web应用程序中使用JPA。 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. 我的问题是:在JavaFX应用程序中创建表时,最佳实践是实现一个类,该类定义数据模型并提供进一步使用该表的方法和字段。 for Example the Person class is used to define data in an address book 例如,Person类用于定义通讯录中的数据

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. 所以实体如何适应这种情况,我的意思是,这里有Person ENTITY和Person数据模型。

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 , 我可以说,您可以像创建Web应用程序一样创建实体,

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

personDao : 人道:

public interface PersonDao{

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

PersonDaoImpl : 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 : 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);

//... 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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