Hibernate 5.2:如何使用HBM文件按名称映射枚举集

时间:2018-07-09 08:05:08

标签: java hibernate enums hbm

我正在寻找一种解决方案(或文档)来按hbm文件映射枚举,但是我只找到带有注释的解决方案。我想知道是否可以使用hbm文件。

这是我的hbm文件。

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-access="field">
    <typedef name="CantonEnum" class="org.hibernate.type.EnumType">
        <param name="enumClass">ch.globaz.avs.affiliation.domaine.commun.valuetype.Canton</param>
        <param name="useNamed">true</param>
    </typedef>
    <class name="ch.globaz.avs.affiliation.domaine.assurance.valuetype.Caisse" table="CAISSE">

        <id name="id" column="ID" type="java.lang.Long">
            <generator class="org.hibernate.id.IdentityGenerator"/>
        </id>

        <property name="numeroFederaleCaisse" column="NUMERO_FEDERALE_CAISSE" type="java.lang.String"/>

        <property name="numeroInterneCaisse" column="NUMERO_INTENRE_CAISSE" type="java.lang.String"/>

        <property name="genreAssurance" column="GENRE_ASSURANCE">
            <type name="org.hibernate.type.EnumType">
                <param name="enumClass">ch.globaz.avs.affiliation.domaine.assurance.valuetype.CaisseType</param>
                <param name="useNamed">true</param>
            </type>
        </property>
        <property name="designation" column="DESIGNATGION" type="java.lang.String"/>
        <property name="position" column="POSITION" type="java.lang.Long"/>

        <set name="cantons" table="CAISSE_CANTON">
            <key column="FK_CAISSE" not-null="true"/>
            <element
                    column="canton"
                    type="CantonEnum"
                    not-null="true"
            />
        </set>

        <list name="codesNogas" cascade="all">
            <key column="FK_CAISSE"/>
            <index column="IDX_CAISSE_CODENOGACAISSE"/>
            <one-to-many class="ch.globaz.avs.affiliation.domaine.assurance.valuetype.CodeNogaCaisse"/>
        </list>
    </class>

但是我有这个例外:

    Caused by: org.h2.jdbc.JdbcSQLException: Erreur de syntaxe dans linstruction SQL {0}; attendu {1}
Syntax error in SQL statement {0}; expected {1}; SQL statement:
select caisse0_.ID as ID1_6_, caisse0_.NUMERO_FEDERALE_CAISSE as NUMERO_F2_6_, caisse0_.NUMERO_INTENRE_CAISSE as NUMERO_I3_6_, caisse0_.GENRE_ASSURANCE as GENRE_AS4_6_, caisse0_.DESIGNATGION as DESIGNAT5_6_, caisse0_.POSITION as POSITION6_6_ from CAISSE caisse0_ inner join CODE_NOGA_CAISSE codesnogas1_ on caisse0_.ID=codesnogas1_.FK_CAISSE cross join CAISSE_CANTON cantons2_ where caisse0_.ID=cantons2_.FK_CAISSE and .=? and caisse0_.GENRE_ASSURANCE=? and (codesnogas1_.CODE in (?)) [42001-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
    at org.h2.message.DbException.getSyntaxError(DbException.java:217)
    at org.h2.command.Parser.getSyntaxError(Parser.java:555)
    at org.h2.command.Parser.readTerm(Parser.java:3078)
    at org.h2.command.Parser.readFactor(Parser.java:2587)
    at org.h2.command.Parser.readSum(Parser.java:2574)
    at org.h2.command.Parser.readConcat(Parser.java:2544)
    at org.h2.command.Parser.readCondition(Parser.java:2370)
    at org.h2.command.Parser.readAnd(Parser.java:2344)
    at org.h2.command.Parser.readExpression(Parser.java:2334)
    at org.h2.command.Parser.parseSelectSimple(Parser.java:2291)
    at org.h2.command.Parser.parseSelectSub(Parser.java:2133)
    at org.h2.command.Parser.parseSelectUnion(Parser.java:1946)
    at org.h2.command.Parser.parseSelect(Parser.java:1919)
    at org.h2.command.Parser.parsePrepared(Parser.java:463)
    at org.h2.command.Parser.parse(Parser.java:335)
    at org.h2.command.Parser.parse(Parser.java:311)
    at org.h2.command.Parser.prepareCommand(Parser.java:278)
    at org.h2.engine.Session.prepareLocal(Session.java:611)
    at org.h2.engine.Session.prepareCommand(Session.java:549)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1247)
    at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:76)
    at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:304)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)
    at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
    at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:75)
    at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)
    at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)
    at com.sun.proxy.$Proxy67.prepareStatement(Unknown Source)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:146)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)
    ... 74 more

我找到了此解决方案,但似乎不起作用:Hibernate 3.3: how to map list of enums by name

1 个答案:

答案 0 :(得分:1)

我意识到异常来自criteriabuilder,在这里我们忘记添加联接(Join cantonJoin = root.join(“ cantons”);)现在sql是正确的。

映射完全正确。