簡體   English   中英

映射的Jackson和Hibernate異常

[英]Jackson and Hibernate exception with mapping

我在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 =“ 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.Final一起使用

當我打電話給控制器時,我收到一個異常:

com.fasterxml.jackson.databind.JsonMappingException:未找到類org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer的序列化器,也未發現創建BeanSerializer的屬性(為避免異常,請禁用SerializationFeature.FAIL_ON_EMPTY_BEANS))(通過引用鏈: .model.Response [“ body”]-> java.util.ArrayList [0]-> com.domain.Car [“ carType”]-> com.domain.CarType _ $$ _ jvst615_e [“ handler”])

我想了解是否必須從實體中刪除設置變量,以及如何解決映射上的異常。 謝謝目前我在每個實體上使用@JsonIgnoreProperties({“ hibernateLazyInitializer”,“ handler”}),但這是正確的?還是我選擇了錯誤的方式來生成實體?)

當我使用類似的存儲庫方法時

@Override
@Transactional
public List<Fleet> getFleets() {
    return fleetRepository.findAll();
}

沒問題,效果很好

更新 :我添加@JsonManagedReference@OneToMany變量和@JsonBackReference@ManyToOne變量,它似乎工作,但現在在車上我只能看到收購,而不是艦隊和CarType(所以沒有標注與JsonBackReference),而不是從我的查詢接收所有該對象,但carType和Fleet為空,我需要相反,所以我需要Fleet和carType,而不是獲取這是我在傑克遜JSON和Hibernate JPA問題中發現它的Infinite Recursion的鏈接

試加@ManyToOne(fetch = FetchType.EAGER)到您的Car實體-屬性carType (或方法getCarType -取決於你用什么)。 對於EAGER提取- Car實體應具有實體對象而非代理對象。

從應用程序返回實體不是一個好習慣,因為您無法控制將返回什么關系和數據。 而不是您應該返回VO(值對象)。 使用VO是安全的,您可以控制從應用程序返回的內容。 從entites的到VO對象(包括關系),你可以使用一些映射器,mappigs Orika例如。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM