hibernate映射数据库表如何使表中字段默认值生效

hibernate映射数据库表如何使表中字段默认值生效,第1张

解决方法:

   在hibernate映射文件对数据库表的描述中,在当前字段处加入insert="false"语句,这时hibernate在进行插入 *** 作时,只会为那些有实值的字段赋值,而值为空白的字段就会使用数据库表中定义的默认值了。

举例说明,表person:

CREATE TABLE address (

 i_id int(11) NOT NULL auto_increment,

 c_address varchar(100) NOT NULL default '中国',

 PRIMARY KEY  (id)

)

addresshbmxml:

<hibernate-mapping package="cncomloughmodel">

   <class

       name="address "

       table="address "

       lazy="false"

   >

       <meta attribute="sync-DAO">true</meta>

       <cache usage="read-write"/>

       <id

           name="IId"

           type="integer"

           column="i_id"

       >

           <generator class="native"/>

       </id>

       <property

           name="C_Address"

           column="c_address "

           type="string"

           not-null="false"

           length="128"

       />

</hibernate-mapping>

运行程序

public regAddress(String a){   //传入的值a未在网页文本框里获得任何值(家庭地址)

Address p = new Address ();

psetAddress(a);

HiFactorysave(p);

}

此时hibernate生成的sql语句为insert into person(c_address) values('');

数据库表结果为

i_id   c_address

1      null

修改addresshbmxml为:

<hibernate-mapping package="cncomloughmodel">

   <class

       name="Address"

       table="address"

       lazy="false"

   >

       <meta attribute="sync-DAO">true</meta>

       <cache usage="read-write"/>

       <id

           name="IId"

           type="integer"

           column="i_id"

       >

           <generator class="native"/>

       </id>

       <property

           name="C_Address"

           column="c_address"

           type="string"

           not-null="false"

           length="128"

           insert="false"

       />

</hibernate-mapping>

再次运行程序,此时hibernate生成的sql语句为 insert into address() values();

大家可以参考下这个网站>

看错误:Every derived table must have its own alias 。你换了数据库,但是数据库的配置有些地方你没改,数据库方言要相应换成MYSQL的数据库方言。

<bean id="sessionFactory"

class="orgspringframeworkormhibernate3LocalSessionFactoryBean" lazy-init="true" >

<property name="dataSource">

<ref bean="dataSource" />

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernatedialect">

orghibernatedialectOracle9Dialect

</prop>(这个地方就是hibernate的数据库方言)

<prop key="hibernateshow_sql">true</prop>

</props>

</property>

<property name="mappingDirectoryLocations">

<list>

<value>classpath:/db/orm/</value>

</list>

</property>

</bean>

你把方言换成orghibernatedialect包下的orghibernatedialectMySQLDialect或者orghibernatedialectMySQL5Dialect试试,有问题再说。

如果用xml配置的话,那就写两个配置文件,可以不再用“hibernatecfgxml”做文件名,随便什么都可以,像“mysqlcfgxml”或“sqlserverxml”都行。用Configuration类获取SessionFactory的代码:

SessionFactory mysqlFactory = new Configuration()configure("mysqlcfgxml")buildSessionFactory();

SessionFactory sqlserverFactory = new Configuration()configure("sqlserverxml")buildSessionFactory();

如果你用spring,多数据库就更简单了,像这段代码可以完成所有配置:

<beans>

<bean id="mysqlDS" class="orgspringframeworkjdbcdatasourceDriverManagerDataSource">

<property name="url">

<value>jdbc:mysql://localhost:3306/test</value>

</property>

<property name="driverClassName">

<value>orggjtmmmysqlDriver</value>

</property>

<property name="username">

<value>root</value>

</property>

<property name="password">

<value>123456</value>

</property>

</bean>

<bean id="mysqlFactory" class="orgspringframeworkormhibernate3LocalSessionFactoryBean">

<property name="dataSource">

<ref local="mysqlDS"/>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernatedialect">orghibernatedialectMySQLDialect</prop>

</props>

</property>

<property name="mappingResources">

<list>

<value>testhbmxml</value>

</list>

</property>

</bean>

<bean id="sqlserverDS" class="orgspringframeworkjdbcdatasourceDriverManagerDataSource">

<property name="url">

<value>jdbc:odbc:test</value>

</property>

<property name="driverClassName">

<value>sunjdbcodbcJdbcOdbcDriver</value>

</property>

<property name="username">

<value>root</value>

</property>

<property name="password">

<value>123456</value>

</property>

</bean>

<bean id="sqlserverFactory" class="orgspringframeworkormhibernate3LocalSessionFactoryBean">

<property name="dataSource">

<ref local="sqlserverDS"/>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernatedialect">orghibernatedialectSQLServerDialect</prop>

</props>

</property>

<property name="mappingResources">

<list>

<value>testhbmxml</value>

</list>

</property>

</bean>

</beans>

hibernate和spring还有很多可行的配置,可以参考他们的references,有很详细地说明的。

hibernate向mysql插入一条数据后,得到该条数据主键的方法。我的主键是自增长的。

方法一:保存完成后,直接用该实体的getId的方法就可以得到。因为保存完成后,hibernate会自动将id赋值给实体。关键代码如下:

iUserMessageDaosave(userMessage);

int id = (int) userMessagegetId();

以上就是关于hibernate映射数据库表如何使表中字段默认值生效全部的内容,包括:hibernate映射数据库表如何使表中字段默认值生效、hibernate连接h2数据库配置怎么写、hibernate 数据库分页等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9687286.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存