gpt4 book ai didi

java - 使用 Jackson 注释映射 Hibernate 实体

转载 作者:行者123 更新时间:2023-11-29 21:50:34 24 4
gpt4 key购买 nike

我正在使用 Spring、hibernate 和 MySql,但我在查询结果的序列化方面遇到一些问题。首先在我的实体中,我在 Set 结构(@OneToMany 端)上添加了 @JsonManagedReference,在单个对象引用(@ManyToOne 端)上添加了 @JsonBackReference,它可以工作,但我无法检索所有需要的信息(例如 @ManyToOne 引用) 。所以我在集合结构上交换@JsonBackReference,在单个对象上交换@JsonManagedReference,但我检索

No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.model.tablesField.TableUI["data"]->java.util.ArrayList[0]->com.domain.Car["carType"]->com.domain.CarType_$$_jvst744_f["handler"])

我还尝试在 Set 结构上使用 @JsonIgnore,但它不适用于相同的问题。这是我的 Spring 配置

private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put(PROPERTY_NAME_HIBERNATE_DIALECT, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
// properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
properties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
properties.put("hibernate.enable_lazy_load_no_trans",true);
return properties;

这是我的几个实体之一的一部分:

   /**
* Car generated by hbm2java
*/
@Entity
@Table(name = "car", catalog = "ATS")
public class Car implements java.io.Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private Integer idCar;
@JsonManagedReference
private CarType carType;
@JsonManagedReference
private Fleet fleet;
private String id;
private int initialKm;
private String carChassis;
private String note;
@JsonBackReference
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;
}

}

使用查询的一种方法:

@Override
@RequestMapping(value = { "/cars/{idFleet}"}, method = RequestMethod.GET)
public @ResponseBody TableUI getCars(@PathVariable int idFleet) {
TableUI ajaxCall=new TableUI();
try {
ajaxCall.setData(fleetAndCarService.findCarsByIdFleet(idFleet));
return ajaxCall;
} catch (QueryException e) {
ErrorResponse errorResponse= ErrorResponseBuilder.buildErrorResponse(e);
LOG.error("Threw exception in FleetAndCarControllerImpl::addCar :" + errorResponse.getStacktrace());
return ajaxCall;
}
}

查询的两个类:

public interface DefRdiRepository extends JpaRepository<DefRdi, Integer>{


//@Query("SELECT CASE WHEN COUNT(c) > 0 THEN true ELSE false END FROM DefRdi c WHERE c.parName = ?1 AND c.description= ?2")
//Boolean existsByParNameAndDescription(String parName, String description);
//Query method of spring, I put findBy and then the key of research
DefRdi findByParNameAndDescription(String parName, String description);
}

public interface CarRepository extends JpaRepository<Car, Integer>, CarRepositoryCustom {

//Query method of spring, I put findBy and then the key of research
List<Car> findByFleetIdFleet(int idFleet);

}

我的错误在哪里?我不想要 Set 对象,而只想要单个引用。问题仅出现在我序列化时。谢谢

更新:我在所有设置的集合上使用 @JSonIgnore 和 Eager 而不是惰性广告,一切都很好,但是有没有办法仅在我想要的时候检索所有信息,例如有两个不同的查询?所以它不起作用

@Override
@Transactional
public List<Car> findByFleetIdFleet(int idFleet) {
List<Car> carList= carRepository.findByFleetIdFleet(idFleet);
for (Car car:carList){
Hibernate.initialize(car.getCarType());
Hibernate.initialize(car.getFleet());
}
return carList;
// return carRepository.findByFleetIdFleet(idFleet);
}

最佳答案

从数据库加载所有集合时都需要急切地获取它们,以便由 Spring 序列化。确保您急切地获取它们(例如 FetchMode.JOIN)。您还可以将@JsonManagedReference从需要的字段与@JsonIgnore交换到黑名单字段,Spring会自动序列化每个字段而无需注释。

更新:

将数据存储库更改为类似的内容应该可以工作,我不确定它是否可以编译,但我认为您会明白这一点:

@EntityGraph(value = "some.entity.graph", type = EntityGraph.EntityGraphType.FETCH)
@Query(
value = "SELECT c FROM Car c INNER JOIN FETCH c.acquisitions WHERE c.id = :idFleet"
)
public interface CarRepository extends JpaRepository<Car, Integer>, CarRepositoryCustom {

//Query method of spring, I put findBy and then the key of research
List<Car> findByFleetIdFleet(int idFleet);

}

有关更多信息,请查看 this 帖子并阅读 official documentation

解决方法:

似乎有一个 workaround ,但是像上面所示那样获取那些渴望的集合应该会对性能产生积极的影响,因为之后不需要加载代理。 Controller 级别也不需要打开事务。

关于java - 使用 Jackson 注释映射 Hibernate 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33690096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com