我对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);
}
带有命名查询的car服务
@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 = "car ",catalog = "ATS ")公共类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最终版
当我调用控制器时,我收到一个异常:
com.fasterxml.jackson.databind。JsonMappingException:找不到类org.hibernate.proxy.pojo.javassist的序列化程序。JavassistLazyInitializer和未发现用于创建BeanSerializer的属性(为了避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:com.model.Response[“body”]-
我想知道我是否必须从实体中删除集合变量,以及如何解决映射中的异常。感谢我在每个实体上使用@ JsonIgnoreProperties({ " hibernateLazyInitializer "," handler"}),但它是正确的吗?此外,我对实体查询进行了循环(hibernate工具写所有实体有可能出错,或者我选择了错误的方式来生成实体?)
当我使用存储库方法时,例如
@Override
@Transactional
public List<Fleet> getFleets() {
return fleetRepository.findAll();
}
没问题,很好用
更新:我在@OneToMore
变量上添加了@JsonManagedIndex
,在@ManyToOne
变量上添加了@JsonBackReference
,它似乎可以工作,但现在在汽车上,我只能看到Acquiption,而不是Fleet和CarType(所以有JsonBackReference的注释),而不是查询,我收到了所有对象,但carType和Fleet为空,我需要相反的,所以我需要车队和carType,而不是收购。这是我找到它的链接,带有Jackson JSON和Hibernate JPA问题的无限递归
尝试将@ManyToOne(fetch=FetchType.EAGER)
添加到您的汽车
entity-attribute(或method)
Car实体应具有实体对象而不是代理对象。
从应用程序返回实体不是好的做法,因为您无法控制将返回哪些关系和数据。相反,您应该返回VO(值对象)。使用VO是安全的,您可以控制从应用程序返回的内容。对于从实体到VO对象(包括关系)的映射,您可以使用一些映射器,例如Orika。
我有这个目标: 还有这个:
问题内容: 我在以下情况下使用ObjectMapper,但是,Person类具有JSON属性“ last_name”,当“ name”属性映射良好时,似乎没有被映射。我在下面包括了我的Person类。感谢任何可能发生这种情况的原因。正在使用Jackson核心/映射器1.8.5。 人类: 问题答案: Java Bean规范定义了预期的映射。因此拥有方法意味着将只映射确切的属性“ lastName”。
是否强制将我的外键实体从ClassA映射到ClassB中的主实体?
我认为错误并不在注释中,因为我更改了几次注释,仍然得到了相同的异常。
问题内容: 我有一个带有集合的类,它们是Hibernate管理的POJO。我也在使用 hbm.xml 文件映射我的实体。当用户转到管理屏幕时,我希望他将数据查看到表中,该表还将包含最近完成的工作。但是,只有访问可检测内容的详细信息页面,才能使用完整的修订集。 我的机会是显示最后的修订日期,该日期将作为每个实例的属性单独加载。所以我有这样的事情: detectable.hbm.xml 这是行不通的,
我试图正确地建立不同实体之间的关系。 看着代码,我认为一切都是好的和连贯的(我已经验证了属性的名称与在Hibernate映射配置文件中建立的名称相同),但是,好吧,它没有编译。所以很明显这是不可以的。显示的错误如下: antlr-2.7.6.jar aopalliance-1.0.jar aspectjrt-1.6.8.jar AspectJTools-1.5.4.jar AspectJWeave