org.hibernate.TypeMismatchException:提供了错误类型的 id

发布于 2024-12-13 13:43:46 字数 15389 浏览 1 评论 0原文

我第一次使用 MyEclipse For Spring,它支持休眠。

以下是 mysql 数据库的相关部分:

CREATE TABLE 
`delidete`.`DatiGeneraliVistoContabile` (
    `DatiGeneraliId` int NOT NULL,       <---- P.K., Foreign key to table Datigenerali
    `SoggettiCodice` int NOT NULL,       <---- P.K., Foreign key to table Soggetti
    `DGVCDataInizio` date NOT NULL, 
    `DGVCDataFine` date) ;

CREATE TABLE 
`delidete`.`DatiGeneraliParereTecnico` (
    `DatiGeneraliId` int NOT NULL,       <---- P.K., Foreign key to above table (DatigeneraliId)
    `TecnicoParereCodice` int NOT NULL,  <---- P.K., Foreign key to above table (SoggettiCodice)
    `DGTPDescrizione` varchar (255) NOT NULL,
    `DGTPDataInizio` date NOT NULL, 
    `DGTPDataFine` date) ;

ALTER TABLE `delidete`.`DatiGeneraliVistoContabile` 
ADD PRIMARY KEY (`DatiGeneraliId`, `SoggettiCodice`) ;

ALTER TABLE `delidete`.`DatiGeneraliParereTecnico` 
ADD CONSTRAINT `IDatiGeneraliParereTecnico` 
    FOREIGN KEY (`DatiGeneraliId`, `TecnicoParereCodice`) 
        REFERENCES `DatiGeneraliVistoContabile` (`DatiGeneraliId`, `SoggettiCodice`) ;

ALTER TABLE `delidete`.`DatiGeneraliVistoContabile` 
ADD CONSTRAINT `IDatiGeneraliVistoContabile2` 
    FOREIGN KEY (`DatiGeneraliId`) 
        REFERENCES `DatiGenerali` (`DatiGeneraliId`) ;

ALTER TABLE `delidete`.`DatiGeneraliVistoContabile` 
ADD CONSTRAINT `IDatiGeneraliVistoContabile1` 
    FOREIGN KEY (`SoggettiCodice`) 
        REFERENCES `Soggetti` (`SoggettiCodice`) ;

这是两个生成的类:

Datigeneralivistocontabile:

@IdClass(delidete.domain.DatigeneralivistocontabilePK.class)
@Entity
@Table(catalog = "delidete", name = "datigeneralivistocontabile")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "DeliDete/delidete/domain", name = "Datigeneralivistocontabile")
public class Datigeneralivistocontabile implements Serializable
{
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({@JoinColumn(name = "DatiGeneraliId", referencedColumnName = "DatiGeneraliId", nullable = false, insertable = false, updatable = false)})
    @XmlTransient
    Datigenerali datigenerali;

    @Column(name = "DatiGeneraliId", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    @XmlElement
    Integer datiGeneraliId;

    ----------------------------------------------------------------------
    //don't know why MyEclipse put this link into this class
    @OneToOne(mappedBy = "datigeneralivistocontabile", fetch = FetchType.LAZY)
    @XmlElement(name = "", namespace = "")
    Datigeneralipareretecnico datigeneralipareretecnico;
    ----------------------------------------------------------------------

    @Temporal(TemporalType.DATE)
    @Column(name = "DGVCDataFine")
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    Calendar dgvcdataFine;

    @Temporal(TemporalType.DATE)
    @Column(name = "DGVCDataInizio", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    Calendar dgvcdataInizio;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({@JoinColumn(name = "SoggettiCodice", referencedColumnName = "SoggettiCodice", nullable = false, insertable = false, updatable = false)})
    @XmlTransient
    Soggetti soggetti;

    @Column(name = "SoggettiCodice", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    @XmlElement
    Integer soggettiCodice;

    constructors, getters, setters, toString, other stuff
}

另一个类

Datigeneralipareretecnico:

@IdClass(delidete.domain.DatigeneralipareretecnicoPK.class)
@Entity
@Table(catalog = "delidete", name = "datigeneralipareretecnico")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "DeliDete/delidete/domain", name = "Datigeneralipareretecnico")
public class Datigeneralipareretecnico implements Serializable
{
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({@JoinColumn(name = "IDEnte", referencedColumnName = "DatiGeneraliId", nullable = false)})
    @XmlTransient
    Datigenerali datigenerali;

    @Column(name = "DatiGeneraliId", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    @XmlElement
    Integer datiGeneraliId;

    @PrimaryKeyJoinColumn
    @OneToOne(fetch = FetchType.LAZY)
    @XmlElement(name = "", namespace = "")
    Datigeneralivistocontabile datigeneralivistocontabile;

    @Temporal(TemporalType.DATE)
    @Column(name = "DGTPDataFine")
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    Calendar dgtpdataFine;

    @Temporal(TemporalType.DATE)
    @Column(name = "DGTPDataInizio", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    Calendar dgtpdataInizio;

    @Column(name = "DGTPDescrizione", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    String dgtpdescrizione;

    @Column(name = "TecnicoParereCodice", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    @XmlElement
    Integer tecnicoParereCodice;

    constructors, getters, setters, toString, other stuff
}

和两个 PK 类:

public class DatigeneralivistocontabilePK implements Serializable
{
    @Column(name = "DatiGeneraliId", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    public Integer datiGeneraliId;

    @Column(name = "SoggettiCodice", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    public Integer soggettiCodice;
}

public class DatigeneralipareretecnicoPK implements Serializable
{
    @Column(name = "DatiGeneraliId", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    public Integer datiGeneraliId;

    @Column(name = "TecnicoParereCodice", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    public Integer tecnicoParereCodice;
}

现在,当我尝试保存 Datigeneralivistocontabile 实体时,一切正常,但是当我尝试打开一个 jsp 来显示时我得到的实体:

GRAVE: Servlet.service() for servlet [DeliDete Servlet] in context with path  [/DeliDete] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.TypeMismatchException: Provided id of the   wrong type for class delidete.domain.Datigeneralipareretecnico. Expected: class   delidete.domain.DatigeneralipareretecnicoPK, got class   delidete.domain.DatigeneralivistocontabilePK] with root cause
 org.hibernate.TypeMismatchException: Provided id of the wrong type for class delidete.domain.Datigeneralipareretecnico. Expected: class delidete.domain.DatigeneralipareretecnicoPK, got class delidete.domain.DatigeneralivistocontabilePK
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:135)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1028)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:623)
at org.hibernate.type.EntityType.resolve(EntityType.java:431)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:140)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
at org.hibernate.loader.Loader.doQuery(Loader.java:773)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
at org.hibernate.loader.Loader.list(Loader.java:2187)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:241)
-----------------------------------------------------------------------
at delidete.dao.DatigeneralivistocontabileDAOImpl.findAllDatigeneralivistocontabiles(DatigeneralivistocontabileDAOImpl.java:109)
at delidete.dao.DatigeneralivistocontabileDAOImpl.findAllDatigeneralivistocontabiles(DatigeneralivistocontabileDAOImpl.java:93)
-----------------------------------------------------------------------
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy61.findAllDatigeneralivistocontabiles(Unknown Source)
----------------------------------------------------------------------- 
at delidete.service.DatigeneralivistocontabileServiceImpl.loadDatigeneralivistocontabiles(DatigeneralivistocontabileServiceImpl.java:226)
-----------------------------------------------------------------------
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy88.loadDatigeneralivistocontabiles(Unknown Source)
-----------------------------------------------------------------------
at delidete.web.DatigeneralivistocontabileController.listDatigeneralivistocontabile(DatigeneralivistocontabileController.java:466)
-----------------------------------------------------------------------
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

我的类位于“delidete”包中。 抛出此异常的方法是:

@Transactional
public Set<Datigeneralivistocontabile> findAllDatigeneralivistocontabiles(
        int startResult, int maxRows) throws DataAccessException
{
    Query query = createNamedQuery("findAllDatigeneralivistocontabiles",
            startResult, maxRows);
    return new LinkedHashSet<Datigeneralivistocontabile>(
            query.getResultList());   <---- this line throws the exception
}

并且它调用的命名查询是:

@NamedQuery(name = "findAllDatigeneralivistocontabiles", query = "select myDatigeneralivistocontabile from Datigeneralivistocontabile myDatigeneralivistocontabile")

查询“select * from datigeneralivistocontabile;”在 MySQL 提示中很好:

mysql> select * from datigeneralivistocontabile;
+----------------+----------------+----------------+--------------+
| DatiGeneraliId | SoggettiCodice | DGVCDataInizio | DGVCDataFine |
+----------------+----------------+----------------+--------------+
|              1 |              1 | 2011-11-02     | 2011-11-03   |
+----------------+----------------+----------------+--------------+
1 row in set (0.00 sec)

mysql>

我不知道该怎么办,因为我管理了数十个像这两个这样的实体,没有出现任何问题。我注意到的是,这是我第一次获得 @OneToOne 注释。 也许正是这个导致了异常...

提前感谢大家

编辑:尝试将列名称 TecnicoParereCodice 更改为 SoggettiCodice 以匹配第一个表中的列的名称,但没有任何改变:我重新搭建了所有内容,运行相同测试并得到同样的错误

I'm working for the first time with MyEclipse For Spring, which has hibernate support.

Following the relevant part of the mysql db:

CREATE TABLE 
`delidete`.`DatiGeneraliVistoContabile` (
    `DatiGeneraliId` int NOT NULL,       <---- P.K., Foreign key to table Datigenerali
    `SoggettiCodice` int NOT NULL,       <---- P.K., Foreign key to table Soggetti
    `DGVCDataInizio` date NOT NULL, 
    `DGVCDataFine` date) ;

CREATE TABLE 
`delidete`.`DatiGeneraliParereTecnico` (
    `DatiGeneraliId` int NOT NULL,       <---- P.K., Foreign key to above table (DatigeneraliId)
    `TecnicoParereCodice` int NOT NULL,  <---- P.K., Foreign key to above table (SoggettiCodice)
    `DGTPDescrizione` varchar (255) NOT NULL,
    `DGTPDataInizio` date NOT NULL, 
    `DGTPDataFine` date) ;

ALTER TABLE `delidete`.`DatiGeneraliVistoContabile` 
ADD PRIMARY KEY (`DatiGeneraliId`, `SoggettiCodice`) ;

ALTER TABLE `delidete`.`DatiGeneraliParereTecnico` 
ADD CONSTRAINT `IDatiGeneraliParereTecnico` 
    FOREIGN KEY (`DatiGeneraliId`, `TecnicoParereCodice`) 
        REFERENCES `DatiGeneraliVistoContabile` (`DatiGeneraliId`, `SoggettiCodice`) ;

ALTER TABLE `delidete`.`DatiGeneraliVistoContabile` 
ADD CONSTRAINT `IDatiGeneraliVistoContabile2` 
    FOREIGN KEY (`DatiGeneraliId`) 
        REFERENCES `DatiGenerali` (`DatiGeneraliId`) ;

ALTER TABLE `delidete`.`DatiGeneraliVistoContabile` 
ADD CONSTRAINT `IDatiGeneraliVistoContabile1` 
    FOREIGN KEY (`SoggettiCodice`) 
        REFERENCES `Soggetti` (`SoggettiCodice`) ;

These are the two generated classes:

Datigeneralivistocontabile:

@IdClass(delidete.domain.DatigeneralivistocontabilePK.class)
@Entity
@Table(catalog = "delidete", name = "datigeneralivistocontabile")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "DeliDete/delidete/domain", name = "Datigeneralivistocontabile")
public class Datigeneralivistocontabile implements Serializable
{
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({@JoinColumn(name = "DatiGeneraliId", referencedColumnName = "DatiGeneraliId", nullable = false, insertable = false, updatable = false)})
    @XmlTransient
    Datigenerali datigenerali;

    @Column(name = "DatiGeneraliId", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    @XmlElement
    Integer datiGeneraliId;

    ----------------------------------------------------------------------
    //don't know why MyEclipse put this link into this class
    @OneToOne(mappedBy = "datigeneralivistocontabile", fetch = FetchType.LAZY)
    @XmlElement(name = "", namespace = "")
    Datigeneralipareretecnico datigeneralipareretecnico;
    ----------------------------------------------------------------------

    @Temporal(TemporalType.DATE)
    @Column(name = "DGVCDataFine")
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    Calendar dgvcdataFine;

    @Temporal(TemporalType.DATE)
    @Column(name = "DGVCDataInizio", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    Calendar dgvcdataInizio;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({@JoinColumn(name = "SoggettiCodice", referencedColumnName = "SoggettiCodice", nullable = false, insertable = false, updatable = false)})
    @XmlTransient
    Soggetti soggetti;

    @Column(name = "SoggettiCodice", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    @XmlElement
    Integer soggettiCodice;

    constructors, getters, setters, toString, other stuff
}

The other class

Datigeneralipareretecnico:

@IdClass(delidete.domain.DatigeneralipareretecnicoPK.class)
@Entity
@Table(catalog = "delidete", name = "datigeneralipareretecnico")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "DeliDete/delidete/domain", name = "Datigeneralipareretecnico")
public class Datigeneralipareretecnico implements Serializable
{
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({@JoinColumn(name = "IDEnte", referencedColumnName = "DatiGeneraliId", nullable = false)})
    @XmlTransient
    Datigenerali datigenerali;

    @Column(name = "DatiGeneraliId", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    @XmlElement
    Integer datiGeneraliId;

    @PrimaryKeyJoinColumn
    @OneToOne(fetch = FetchType.LAZY)
    @XmlElement(name = "", namespace = "")
    Datigeneralivistocontabile datigeneralivistocontabile;

    @Temporal(TemporalType.DATE)
    @Column(name = "DGTPDataFine")
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    Calendar dgtpdataFine;

    @Temporal(TemporalType.DATE)
    @Column(name = "DGTPDataInizio", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    Calendar dgtpdataInizio;

    @Column(name = "DGTPDescrizione", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @XmlElement
    String dgtpdescrizione;

    @Column(name = "TecnicoParereCodice", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    @XmlElement
    Integer tecnicoParereCodice;

    constructors, getters, setters, toString, other stuff
}

And the two PK classes:

public class DatigeneralivistocontabilePK implements Serializable
{
    @Column(name = "DatiGeneraliId", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    public Integer datiGeneraliId;

    @Column(name = "SoggettiCodice", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    public Integer soggettiCodice;
}

public class DatigeneralipareretecnicoPK implements Serializable
{
    @Column(name = "DatiGeneraliId", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    public Integer datiGeneraliId;

    @Column(name = "TecnicoParereCodice", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    public Integer tecnicoParereCodice;
}

Now when I try to save a Datigeneralivistocontabile entity it all goes ok, but when I try to open a jsp to show that entity I get:

GRAVE: Servlet.service() for servlet [DeliDete Servlet] in context with path  [/DeliDete] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.TypeMismatchException: Provided id of the   wrong type for class delidete.domain.Datigeneralipareretecnico. Expected: class   delidete.domain.DatigeneralipareretecnicoPK, got class   delidete.domain.DatigeneralivistocontabilePK] with root cause
 org.hibernate.TypeMismatchException: Provided id of the wrong type for class delidete.domain.Datigeneralipareretecnico. Expected: class delidete.domain.DatigeneralipareretecnicoPK, got class delidete.domain.DatigeneralivistocontabilePK
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:135)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1028)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:623)
at org.hibernate.type.EntityType.resolve(EntityType.java:431)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:140)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
at org.hibernate.loader.Loader.doQuery(Loader.java:773)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
at org.hibernate.loader.Loader.list(Loader.java:2187)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:241)
-----------------------------------------------------------------------
at delidete.dao.DatigeneralivistocontabileDAOImpl.findAllDatigeneralivistocontabiles(DatigeneralivistocontabileDAOImpl.java:109)
at delidete.dao.DatigeneralivistocontabileDAOImpl.findAllDatigeneralivistocontabiles(DatigeneralivistocontabileDAOImpl.java:93)
-----------------------------------------------------------------------
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy61.findAllDatigeneralivistocontabiles(Unknown Source)
----------------------------------------------------------------------- 
at delidete.service.DatigeneralivistocontabileServiceImpl.loadDatigeneralivistocontabiles(DatigeneralivistocontabileServiceImpl.java:226)
-----------------------------------------------------------------------
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy88.loadDatigeneralivistocontabiles(Unknown Source)
-----------------------------------------------------------------------
at delidete.web.DatigeneralivistocontabileController.listDatigeneralivistocontabile(DatigeneralivistocontabileController.java:466)
-----------------------------------------------------------------------
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

My classes are located in the package "delidete".
The method throwing this exception is:

@Transactional
public Set<Datigeneralivistocontabile> findAllDatigeneralivistocontabiles(
        int startResult, int maxRows) throws DataAccessException
{
    Query query = createNamedQuery("findAllDatigeneralivistocontabiles",
            startResult, maxRows);
    return new LinkedHashSet<Datigeneralivistocontabile>(
            query.getResultList());   <---- this line throws the exception
}

and the namedquery it's calling is:

@NamedQuery(name = "findAllDatigeneralivistocontabiles", query = "select myDatigeneralivistocontabile from Datigeneralivistocontabile myDatigeneralivistocontabile")

The query "select * from datigeneralivistocontabile;" in the MySQL prompt is fine:

mysql> select * from datigeneralivistocontabile;
+----------------+----------------+----------------+--------------+
| DatiGeneraliId | SoggettiCodice | DGVCDataInizio | DGVCDataFine |
+----------------+----------------+----------------+--------------+
|              1 |              1 | 2011-11-02     | 2011-11-03   |
+----------------+----------------+----------------+--------------+
1 row in set (0.00 sec)

mysql>

I don't know what to do, because I managed tens of entities like these two without problems. The thing I'm noticing is that this is the first time I get a @OneToOne annotation.
Maybe it's this that's causing the exception...

Thanks in advance to you all

EDIT: tried changing column name TecnicoParereCodice to SoggettiCodice to match also in the name the column in the first table but nothing changed: I re-scaffolded everything, run the same test and got the same error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2024-12-20 13:43:46

由于主键相同但属性名称不同,我像这样修改了数据库:

CREATE TABLE 
`delidete`.`DatiGeneraliVistoContabile` (
`DatiGeneraliId` int NOT NULL,       <---- P.K., Foreign key to table Datigenerali
`SoggettiCodice` int NOT NULL,       <---- P.K., Foreign key to table Soggetti
`DGVCDataInizio` date NOT NULL, 
`DGVCDataFine` date) ;

CREATE TABLE 
`delidete`.`DatiGeneraliParereTecnico` (
`DatiGeneraliId` int NOT NULL,       <---- P.K., Foreign key to above table (DatigeneraliId)
`SoggettiCodice` int NOT NULL,       <---- P.K., Foreign key to above table (SoggettiCodice)
`DGTPDescrizione` varchar (255) NOT NULL,
`DGTPDataInizio` date NOT NULL, 
`DGTPDataFine` date) ;

重新搭建两个实体并更改 Datigeneralipareretecnico 类,如下所示:

**@IdClass(delidete.domain.DatigeneralivistocontabilePK.class)**
@Entity
@Table(catalog = "delidete", name = "datigeneralipareretecnico")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "DeliDete/delidete/domain", name = "Datigeneralipareretecnico")
public class Datigeneralipareretecnico implements Serializable
{
    ....
}

正如您在 Datigeneralipareretecnico 类中看到的,我为该类使用了 PK 类Datigeneralivistocontabile 一切顺利。
我知道这不是解决问题的正确方法,但是嘿它有效!
希望能帮助别人..

Since the primary key was the same but with different property name, I modified the db like this:

CREATE TABLE 
`delidete`.`DatiGeneraliVistoContabile` (
`DatiGeneraliId` int NOT NULL,       <---- P.K., Foreign key to table Datigenerali
`SoggettiCodice` int NOT NULL,       <---- P.K., Foreign key to table Soggetti
`DGVCDataInizio` date NOT NULL, 
`DGVCDataFine` date) ;

CREATE TABLE 
`delidete`.`DatiGeneraliParereTecnico` (
`DatiGeneraliId` int NOT NULL,       <---- P.K., Foreign key to above table (DatigeneraliId)
`SoggettiCodice` int NOT NULL,       <---- P.K., Foreign key to above table (SoggettiCodice)
`DGTPDescrizione` varchar (255) NOT NULL,
`DGTPDataInizio` date NOT NULL, 
`DGTPDataFine` date) ;

re-scaffolded the two entities and changed the class Datigeneralipareretecnico as following:

**@IdClass(delidete.domain.DatigeneralivistocontabilePK.class)**
@Entity
@Table(catalog = "delidete", name = "datigeneralipareretecnico")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "DeliDete/delidete/domain", name = "Datigeneralipareretecnico")
public class Datigeneralipareretecnico implements Serializable
{
    ....
}

As you can see in the class Datigeneralipareretecnico I used the PK class for the class Datigeneralivistocontabile and everything went ok.
I know it's not the correct way to solve it but hey it works!
Hope to help someone..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文