Hibernate HQL插入查询

时间:2015-02-17 10:00:57

标签: hibernate hql

在Eclipse中使用Hibernate和MySQL数据库, 我在两个表ProfileFonctionnalite之间有多对多的关系,结果表是T_PROFILE_FONCTIONNALITE,带有复合主键。我想在T_PROFILE_FONCTIONNALITE中插入新记录。

这些是hbm.xml文件中的相应类:

<class name="Fonctionnalite" table="T_FONCTIONNALITE" dynamic-update="false" dynamic-insert ="false">
   <id name="code" column="CODE" type="java.lang.String"/>
   <property name="libelle"  column="LIBELLE"  type="java.lang.String"/>
   <property name="url"  column="URL"  type="java.lang.String"/>
  <bag name="profiles" cascade="none" table="T_PROFILE_FONCTIONNALITE" lazy ="true" inverse="true">
        <key column="CODE_FONCTIONNALITE" />
        <many-to-many class="Profile" column="CODE_PROFILE" unique="true"/>
   </bag>
</class>

<class name="Profile" table="T_PROFILE" dynamic-update="true">
   <id name="code"  column="CODE"  type="java.lang.String"/>
   <property name="libelle"  column="LIBELLE"  type="java.lang.String"/>
   <property name="estFonctionnalitesUpdate"  column="UPDATED"  type="yes_no"/>
   <bag name="menus" cascade="all" table="T_PROFILE_FONCTIONNALITE" lazy ="true" inverse="true">
        <key column="CODE_PROFILE" />
        <many-to-many class="Fonctionnalite" column="CODE_FONCTIONNALITE" unique="true" where="URL is not null" />
   </bag>
</class>

我尝试为表 T_PROFILE_FONCTIONNALITE 添加以下实体:

<class name="proFonc" table="T_PROFILE_FONCTIONNALITE" dynamic-update="false" dynamic-insert ="false">
   <composite-id>
       <key-many-to-one name="codepro" entity-name="Profile" foreign-key="code" column="CODE_PROFILE"></key-many-to-one>
      <key-many-to-one name="codefonc" entity-name="Fonctionnalite" foreign-key="code" column="CODE_FONCTIONNALITE"></key-many-to-one>
       </composite-id>
</class>

我遇到了这个例外:

An association from the table T_PROFILE_FONCTIONNALITE refers to an unmapped class: Fonctionnalite

另一个问题是如何编写查询本身。

这是首选的方法here

String hql = "INSERT INTO Employee(firstName, lastName, salary)"  + 
             "SELECT firstName, lastName, salary FROM old_employee";
Query query = session.createQuery(hql);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);

但我曾经用以下参数进行查询:

 <query name="insertpf" type="hql" >
    <body>
    <![CDATA[
        insert into  proFonc values 
    ]]>
    </body>
    <body param="proid"> 
    <![CDATA[
        (proid ,
          ]]>
    </body>
    <body param="foncid"> 
    <![CDATA[
        foncid)
          ]]>
    </body>
</query>

当然,这在语法上是错误的,因为我创建了它,如果我不使用select,请帮助我,也不要使用session来获取要插入的值,因为我将它们放在变量中。 非常感谢你。

1 个答案:

答案 0 :(得分:0)

这是解决我问题的方法:

无需添加实体!,无需添加查询!使用saveOrUpdate

在dao / hibernate / modeldaoimpl.java中我有这个方法:

    public void saveOrUpdate(final Object entity) throws DaoException{
    try {
        getHibernateTemplate().saveOrUpdate(entity);
    } catch (Exception e) {
        throw new DaoException(e);
    }

}

此方法更新考虑与其他实体关系的每个实体。因此,只需使用profile填充fonctionalities实体(查看bag问题中的profile标记,然后调用此方法。

在我的安全服务界面中:

public ResultatDTO saveProfil(ProfileDTO profilDTO, Integer idUser)
        throws ServiceExecutionException;

实现:

public ResultatDTO saveProfil(Profile pr, Integer idUser)
        throws ServiceExecutionException {
    ResultatDTO resultatDTO = new ResultatDTO();
    try {
        pr.addAllMenus(getfucs(pr)); //fill your entity (`bag` menus).

        modelDao.saveOrUpdate(profil);// call saveorupdate

        resultatDTO.setResult(//obj);
        return resultatDTO;

    } catch (Exception e) {
        throw new ServiceExecutionException(e);
    }
}

体:

public void saveOrUpdate(final Object entity) throws DaoException{
    try {
        getHibernateTemplate().saveOrUpdate(entity);
    } catch (Exception e) {
        throw new DaoException(e);
    }

}.