Hibernate HQL插入查询
在Eclipse中,使用Hibernate和MySQL数据库, 我在两个表Profile和Fonctionnalite之间有多对多关系,得到的表是带有复合主键的T_Profile_Fonctionnalite。我想在T_PROFILE_FONCTIONNALITE中插入一条新记录 以下是hbm.xml文件中的相应类:Hibernate HQL插入查询,hibernate,hql,Hibernate,Hql,在Eclipse中,使用Hibernate和MySQL数据库, 我在两个表Profile和Fonctionnalite之间有多对多关系,得到的表是带有复合主键的T_Profile_Fonctionnalite。我想在T_PROFILE_FONCTIONNALITE中插入一条新记录 以下是hbm.xml文件中的相应类: <class name="Fonctionnalite" table="T_FONCTIONNALITE" dynamic-update="false" dynamic-in
<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>
另一个问题是如何编写查询本身
这是首选的方法
但我过去常常使用以下参数进行查询:
<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或会话来获取要插入的值,请帮助我,因为我在变量中有它们。
非常感谢。这就是我问题的解决方案: 无需添加实体!,无需添加查询!。使用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实体查看profile问题中的bag标记,并调用此方法
在我的安全服务界面中:
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);
}
}
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);
}
}.