Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify default value in Hibernate's XML configuration file

I'm configuring Hibernate via the mapping configuration file.

<class name="Person" table="person">
    <id name="id" column="id" type="long"/>
    <property name="name" column="name" type="string"/>
    <property name="age" column="age" type="integer"/>
</class>

How do I set age to be nullable and default to null?

like image 332
Steve Kuo Avatar asked May 15 '12 23:05

Steve Kuo


People also ask

How do I add a default value to my entity class?

Inserting a default value can be done in various ways such as Default entity property value using constructor or setter. Other ways like using JPA with columnDefinition have the drawback that they insert a null by default and the default value of the DBMS does not precede.

What is the name of default configuration file in Hibernate?

Hibernate also requires a set of configuration settings related to database and other related parameters. All such information is usually supplied as a standard Java properties file called hibernate. properties, or as an XML file named hibernate. cfg.

Do I need Hibernate CFG XML?

Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate. cfg. xml file which is exactly what the configure() method does.


1 Answers

<property name="age" type="integer">
  <column name="age" not-null="false" default="null" />
</property>
like image 174
Firo Avatar answered Sep 20 '22 22:09

Firo