简体   繁体   中英

Jquery Jtable - Show records with many-to-one attributes (Spring-Hibernate)

I'm stuck with the following...

I have this objects, using hibernate and spring:

public class Employee
{
    @Id @GeneratedValue
    @Column(name = "id")
    String id;
    @Column(name = "name")
    String name;
    @ManyToOne
    @JoinColumn(name = "city")
    City city;

     //gets and sets...
}

and this..

public class City
{
   @Id @GeneratedValue
   @Column(name = "id")
   String id;
   @Column(name = "name")
   String city;

   //gets and sets...
}

I need to show the employee records with jTable. I'm doing this:

$('#empleados').jtable(
{
   title: 'Employees',
   sorting: true,
   selecting: true,
   actions: 
   {
      listAction: 'getEmployeesTable',
      createAction: 'addEmployee',
      updateAction: 'updateEmployess',
      deleteAction: 'deleteEmployee'
   },
fields: {
id:{
key: true,
list: false

},

name: {
title: "Name"
},
city: {
title: "City",
options: "getCities",
display:function(data){
return data.record.city.name;
}

}
}

}
});

I'm seeing the column City value and the combo values on create and edit form correctly, but when I want to Save or Update a record, I have an error because of the mapping between the city name and the city object. I found examples but putting only the id's of the many-to-one attribute, but not the entire object.

Try:

public class Employee
{
    @Id @GeneratedValue
    @Column(name = "id")
    Long id;
    @Column(name = "name")
    String name;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "city_id")
    City city;
}

public class City
{
   @Id 
   @GeneratedValue
   @Column(name = "city_id")
   Long id;
   @Column(name = "city_name")
   String city;

   //gets and sets...
}

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