我对Hibernate-Spring MVC有几个问题。 这是我从控制器到数据库实体的调用示例:
控制器
@Override
@RequestMapping(value = { "/cars/{idFleet}"}, method = RequestMethod.GET)
public @ResponseBody Response<List<Car>> getCars(@PathVariable int idFleet) throws QueryException{
return fleetAndCarService.findCarsByIdFleet(idFleet);
}
服务
@Override
// @Transactional
public Response<List<Car>> findCarsByIdFleet(int idFleet) throws QueryException {
try{
return new Response<List<Car>>(HttpStatus.OK.value(),databaseFleetsAndCarsServices.findCarsByIdFleet(idFleet));
} catch (Exception e) {
throw new QueryException(e);
}
}
数据库服务
@Override
public List<Car> findCarsByIdFleet(int idFleet) {
return carServices.findByFleetIdFleet(idFleet);
}
带有命名查询的汽车服务
@Override
@Transactional
public List<Car> findByFleetIdFleet(int idFleet) {
return carRepository.findByFleetIdFleet(idFleet);
}
汽车库
public interface CarRepository extends JpaRepository<Car, Integer> {
//Query method of spring, I put findBy and then the key of research
List<Car> findByFleetIdFleet(int idFleet);
}
汽车实体
@Entity
@Table(name =&#34; car&#34;,catalog =&#34; ATS&#34;) public class Car实现了java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer idCar;
private CarType carType;
private Fleet fleet;
private String id;
private int initialKm;
private String carChassis;
private String note;
private Set<Acquisition> acquisitions = new HashSet<Acquisition>(0);
public Car() {
}
public Car(CarType carType, Fleet fleet, int initialKm, String carChassis) {
this.carType = carType;
this.fleet = fleet;
this.initialKm = initialKm;
this.carChassis = carChassis;
}
public Car(CarType carType, Fleet fleet, String id, int initialKm, String carChassis, String note,
Set<Acquisition> acquisitions) {
this.carType = carType;
this.fleet = fleet;
this.id = id;
this.initialKm = initialKm;
this.carChassis = carChassis;
this.note = note;
this.acquisitions = acquisitions;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_car", unique = true, nullable = false)
public Integer getIdCar() {
return this.idCar;
}
public void setIdCar(Integer idCar) {
this.idCar = idCar;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_carType", nullable = false)
public CarType getCarType() {
return this.carType;
}
public void setCarType(CarType carType) {
this.carType = carType;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_fleet", nullable = false)
public Fleet getFleet() {
return this.fleet;
}
public void setFleet(Fleet fleet) {
this.fleet = fleet;
}
@Column(name = "id", length = 5)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@Column(name = "initialKm", nullable = false)
public int getInitialKm() {
return this.initialKm;
}
public void setInitialKm(int initialKm) {
this.initialKm = initialKm;
}
@Column(name = "carChassis", nullable = false, length = 20)
public String getCarChassis() {
return this.carChassis;
}
public void setCarChassis(String carChassis) {
this.carChassis = carChassis;
}
@Column(name = "note", length = 100)
public String getNote() {
return this.note;
}
public void setNote(String note) {
this.note = note;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "car")
public Set<Acquisition> getAcquisitions() {
return this.acquisitions;
}
public void setAcquisitions(Set<Acquisition> acquisitions) {
this.acquisitions = acquisitions;
}
}
汽车类型(通过外键链接,一对多关系)
/**
* CarType generated by hbm2java
*/
@Entity
@Table(name = "carType", catalog = "ATS")
public class CarType implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String idCarType;
private String note;
private Set<Car> cars = new HashSet<Car>(0);
public CarType() {
}
public CarType(String idCarType) {
this.idCarType = idCarType;
}
public CarType(String idCarType, String note, Set<Car> cars) {
this.idCarType = idCarType;
this.note = note;
this.cars = cars;
}
@Id
@Column(name = "id_carType", unique = true, nullable = false, length = 5)
public String getIdCarType() {
return this.idCarType;
}
public void setIdCarType(String idCarType) {
this.idCarType = idCarType;
}
@Column(name = "note", length = 100)
public String getNote() {
return this.note;
}
public void setNote(String note) {
this.note = note;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "carType")
public Set<Car> getCars() {
return this.cars;
}
public void setCars(Set<Car> cars) {
this.cars = cars;
}
}
车队实体
@Entity
@Table(name = "fleet", catalog = "ATS")
public class Fleet implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer idFleet;
private Ecu ecu;
private String application;
private String cubic;
private int power;
private String euroClass;
private String engineType;
private String traction;
private String transmission;
private String note;
private Set<Car> cars = new HashSet<Car>(0);
收购实体
@Entity
@Table(name = "acquisition", catalog = "ATS")
public class Acquisition implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer idAcquisition;
private Car car;
private DpfWeighting dpfWeighting;
private MissionProfile missionProfile;
private OilSample oilSample;
private Shift shift;
private SwVersion swVersion;
private Date date;
private float sessionDuration;
private int beginKm;
private int endKm;
private String driverName;
private String dataset;
private String drChannelsConf;
private String excelRow;
private Set<Rdi> rdis = new HashSet<Rdi>(0);
private Set<SelfLearning> selfLearnings = new HashSet<SelfLearning>(0);
private Set<Iupr> iuprs = new HashSet<Iupr>(0);
我和maven一起使用 4.2.1.RELEASE 4.3.11.Final
当我拨打我的控制器时,我收到一个例外:
com.fasterxml.jackson.databind.JsonMappingException:没有序列化程序 上课时发现 org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer而且没有 发现创建BeanSerializer的属性(以避免异常, 禁用SerializationFeature.FAIL_ON_EMPTY_BEANS))(通过引用 链: com.model.Response [&#34;主体&#34;] - &GT; java.util.ArrayList中[0] - &GT; com.domain.Car [&#34; carType&#34;] - &GT; com.domain .CarType _ _ $$ jvst615_e [&#34;处理&#34;])
我想了解是否必须从实体中删除set变量,如何在映射时解决此异常。谢谢 目前我在每个实体上使用@JsonIgnoreProperties({&#34; hibernateLazyInitializer&#34;,&#34; handler&#34;}),但这是正确的吗?此外我还有cicle循环到实体查询中(是否有可能hibernate工具错误写入所有实体或我已经选择了错误的生成实体的方式?)
当我使用像
这样的存储库方法时@Override
@Transactional
public List<Fleet> getFleets() {
return fleetRepository.findAll();
}
没问题,效果很好
更新 :
我在@JsonManagedReference
变量上添加了@OneToMany
,在@JsonBackReference
变量上添加了@ManyToOne
它似乎有效,但现在在车里我只能看到收购而不是Fleet和CarType(所以有使用JsonBackReference进行注释而不是查询我收到所有对象但是carType和Fleet都是null,我需要相反,所以我需要fleet和carType,而不是获取
这是我找到它的链接
Infinite Recursion with Jackson JSON and Hibernate JPA issue
答案 0 :(得分:0)
尝试向@ManyToOne(fetch = FetchType.EAGER)
实体添加Car
- 属性carType
(或方法getCarType
- 取决于您使用的内容)。对于EAGER fetch - Car
实体应该具有实体对象而不是代理对象。
从应用程序返回实体不是一个好习惯,因为您无法控制将返回哪些关系和数据。而不是你应该返回VO(值对象)。使用VO是安全的,您可以控制从应用程序返回的内容。对于从entites到VO对象(包括关系)的mappig,您可以使用一些映射器,例如Orika。