hibernate和Eager fetch类型的性能问题

时间:2015-11-16 13:35:48

标签: java mysql spring performance hibernate

我正在使用Spring和Hibernate来使用MySql数据库。在我的架构中,有一个与其他类有9连接的Acquisition类,我必须检索这个对象。 所以,我将我的所有提取类型设置为EAGER,并将@JsonIgnore放在集合集合上。 问题是性能,我有一个方法可以多次检索Acquisition对象和执行时间

iupr.setAcquisition(acquisitionServices.findById(idAcquisition));

太高了。 是否渴望输入问题?我该如何解决这个问题(使用lazy类型我无法检索类的所有对象,例如Car有CarType引用,我需要这个)。 我想到的唯一方法是在ArrayList中存储我的Acquisition对象,因为我在同一个类中创建它们,但我不喜欢这种方法。 你有建议吗?谢谢

这是实体类(省略的get和set方法)

采集

@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;
    @JsonIgnore
    private Set<Rdi> rdis = new HashSet<Rdi>(0);
    @JsonIgnore
    private Set<SelfLearning> selfLearnings = new HashSet<SelfLearning>(0);
    @JsonIgnore
    private Set<Iupr> iuprs = new HashSet<Iupr>(0);

SwVersion

@Entity
@Table(name = "swversion", catalog = "ats")
public class SwVersion implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String idSwVersion;
    private String note;
    @JsonIgnore
    private Set<Acquisition> acquisitions = new HashSet<Acquisition>(0);

RDI

@Entity
@Table(name = "rdi", catalog = "ats")
public class Rdi implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer idRdi;

    private Acquisition acquisition;

    private DefRdi defRdi;
    private String value1;
    private Integer value2;
    private Float value3;

defRdi

@Entity
@Table(name = "defrdi", catalog = "ats")
public class DefRdi implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Integer idDefRdi;

    private Ecu ecu;
    private int sequence;
    private String parName;
    private String description;
    private int valueType;
    private String valueNull;
    private String excelColumn;
    private String note;
    @JsonIgnore
    private Set<Rdi> rdis = new HashSet<Rdi>(0);

    public DefRdi() {
    }

    public DefRdi(Ecu ecu, int sequence, String parName, String description, int valueType, String excelColumn) {
        this.ecu = ecu;
        this.sequence = sequence;
        this.parName = parName;
        this.description = description;
        this.valueType = valueType;
        this.excelColumn = excelColumn;
    }

    public DefRdi(Ecu ecu, int sequence, String parName, String description, int valueType, String valueNull,
            String excelColumn, String note, Set<Rdi> rdis) {
        this.ecu = ecu;
        this.sequence = sequence;
        this.parName = parName;
        this.description = description;
        this.valueType = valueType;
        this.valueNull = valueNull;
        this.excelColumn = excelColumn;
        this.note = note;
        this.rdis = rdis;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "id_defRdi", unique = true, nullable = false)
    public Integer getIdDefRdi() {
        return this.idDefRdi;
    }

    public void setIdDefRdi(Integer idDefRdi) {
        this.idDefRdi = idDefRdi;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "id_ecu", nullable = false)
    public Ecu getEcu() {
        return this.ecu;
    }

    public void setEcu(Ecu ecu) {
        this.ecu = ecu;
    }

    @Column(name = "sequence", nullable = false)
    public int getSequence() {
        return this.sequence;
    }

    public void setSequence(int sequence) {
        this.sequence = sequence;
    }

    @Column(name = "parName", nullable = false, length = 50)
    public String getParName() {
        return this.parName;
    }

    public void setParName(String parName) {
        this.parName = parName;
    }

    @Column(name = "description", nullable = false, length = 100)
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name = "valueType", nullable = false)
    public int getValueType() {
        return this.valueType;
    }

    public void setValueType(int valueType) {
        this.valueType = valueType;
    }

    @Column(name = "valueNull", length = 3)
    public String getValueNull() {
        return this.valueNull;
    }

    public void setValueNull(String valueNull) {
        this.valueNull = valueNull;
    }

    @Column(name = "excelColumn", nullable = false, length = 2)
    public String getExcelColumn() {
        return this.excelColumn;
    }

    public void setExcelColumn(String excelColumn) {
        this.excelColumn = excelColumn;
    }

    @Column(name = "note", length = 100)
    public String getNote() {
        return this.note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "defRdi")
    public Set<Rdi> getRdis() {
        return this.rdis;
    }

    public void setRdis(Set<Rdi> rdis) {
        this.rdis = rdis;
    }

}

Ecu的

@Entity
@Table(name = "ecu", catalog = "ats")
public class Ecu implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String idEcu;
    private String note;
    @JsonIgnore
    private Set<DefSelfLearning> defSelfLearnings = new HashSet<DefSelfLearning>(0);
    @JsonIgnore
    private Set<DefRdi> defRdis = new HashSet<DefRdi>(0);
    @JsonIgnore
    private Set<DefIupr> defIuprs = new HashSet<DefIupr>(0);
    @JsonIgnore
    private Set<Fleet> fleets = new HashSet<Fleet>(0);

对于DefIupr和SelfLearning也是如此 懒惰不起作用,因为我使用jackson为我的webservice序列化对象,当我创建它时,我只检索内部对象的代理然后异常。我尝试使用Hibernate.initialize并创建两个方法,一个用于非序列化查询,另一个用于所有相关对象(使用hibernate.initialize),但不能用于相同的异常

@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; 
    }

例外:

    ERROR com.controller.ErrorController - org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: 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_$$_jvst6c2_f["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: 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_$$_jvst6c2_f["handler"])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:271)
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:100)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:167)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:100)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:166)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:80)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:127)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: 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_$$_jvst6c2_f["handler"])
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)
    at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:26)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:505)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:505)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:100)
    at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21)
    at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:183)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:505)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:639)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:152)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:128)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:602)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:264)
    ... 37 more

.17:32:45.577 [http-nio-8086-exec-4] WARN  o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Handler execution resulted in exception: Could not write content: 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_$$_jvst6c2_f["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: 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_$$_jvst6c2_f["handler"]) 

0 个答案:

没有答案