A detailed description of the Hibernate hbm mapping file

Source: Internet
Author: User
Tags throw exception

Error Demo

First step: Import Jar package omitted

Part II: Creating the Person class

Package Cn.hibernate.bean;

Import Java.util.Date;

public class Person {

Private Integer pId;
Private String PName;
private int age;
Private Date brithdaydate;
private Boolean gender;
Private byte[] photo;
Private String desc; Describe




Public Integer Getpid () {
return pId;
}
public void Setpid (Integer pId) {
This.pid = pId;
}

public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public String Getpname () {
return pName;
}
public void Setpname (String pName) {
This.pname = PName;
}
Public Date getbrithdaydate () {
return brithdaydate;
}
public void Setbrithdaydate (Date brithdaydate) {
This.brithdaydate = brithdaydate;
}
public Boolean Isgender () {
return gender;
}
public void Setgender (Boolean gender) {
This.gender = gender;
}
Public byte[] Getphoto () {
return photo;
}
public void Setphoto (byte[] photo) {
This.photo = photo;
}
Public String GetDesc () {
return desc;
}
public void Setdesc (String desc) {
THIS.DESC = desc;
}
}

Step three: Create a mapping file for person

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<!--
<class> Tags: Configuring relationships between objects and tables
*name: By default, determines the fully qualified class name of the object
If you are configuring the package in *table: Table name for database
*catalog: Database name to use, default: Hibernate.cfg.xml Configure URL settings for database name
URL # jdbc:mysql://localhost:3306/minemysql
<id> label Configuration primary key, requires that each table should have a primary key
* Name: Determine the JavaBean attribute as the primary key
General properties of the <property> tag configuration JavaBean
* Name: Determines the property name
* Type: Determines the property type
Java type: type= "java.lang.String"
Hibernate type: type= "string"
Timestamp: timestamp, data change, current field contents will automatically use system time
Time:
Date: Day
Binary: Binary, Big Data type
Database type: Using sub-tags <column>
Name: Column Name
Sql-type: Database type. Example: varchar (50)
* Column: The name of the database: The default value is the same as the value of name
* Length: Determines the size of the column
* Unique: Unique (add unique constraint)
* Not-null: Not empty
-
<class name= "Person" table= "T_person" >
<id name= "PId" >
<!--PRIMARY key generation policy: fixed value--
<generator class= "native" ></generator>
</id>
<property name= "PName" >
<column name= "pName" sql-type= "varchar" ></column>
</property>
<property name= "Age" type= "integer" column= "Age" ></property>
<property name= "brithdaydate" type= "date" ></property>
<property name= "Gender" Type= "Boolean" ></property>
<property name= "photo" type= "binary" not-null= "true" Unique= "false" Length= "350000" ></property>
<property name= "desc" column= "' desc '" ></property>
</class>

Fourth step: Create a core profile Hibernate.cfg.xml file

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory>
<!--#1 Basic four items
Property.Name value if you start with ' hibernate ', you can omit
Same as "Hibernate.connection.driver_class" and "Connection.driver_class"
-
<property name= "Hibernate.connection.driver_class" >
Com.mysql.jdbc.Driver
</property>
<property name= "Hibernate.connection.url" >
Jdbc:mysql://localhost:3306/minemysql
</property>
<property name= "Hibernate.connection.username" >root</property>
<property name= "Hibernate.connection.password" >root</property>

<!--#2方言-
<property name= "Hibernate.dialect" >
Org.hibernate.dialect.MySQLDialect
</property>


<!--#3 SQL--
<!--whether to display SQL statements--
<property name= "Hibernate.show_sql" >true</property>
<!--whether formatted SQL statements do not format the display of a row formatted to display multiple lines--
<property name= "Hibernate.format_sql" >true</property>
<!--Whether a comment is displayed, providing the current SQL statement action Object--
<property name= "Hibernate.use_sql_comments" >true</property>


<!--#4如何创建表 (not important)
Create: Each time the Cfg.xml file is loaded, the table will be created, and when the program is closed, the table is not deleted [initialized, used during testing]
If the table exists, it is first deleted and then created
Create-drop: Each time the Cfg.xml file is loaded, the table is created and the table is deleted when the program closes
Factory.close () must be executed to delete
Update: If the table does not exist then create, if the table exists, first check whether the *.hbm.xml file matches the table,
If the mismatch will update the table structure (add only, do not delete)
Validate: Load cfg.xml validation, map file and data table match, if match normal operation, if not match throw exception
# # # shows the development of the first table, and then the mapping file
* Table created by DBA
-
<property name= "Hibernate.hbm2ddl.auto" >update</property>


<!--#5取消bean效验-
Omit below will explain when you need to configure <property name= "Javax.persistence.validation.mode" >none</property>

</session-factory>

Fifth step: Create a Test class

Package cn.hibernate.test;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.util.Date;

Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import org.hibernate.cfg.Configuration;
Import Org.junit.Test;

Import Cn.hibernate.bean.Person;

public class TestApp {

@Test
public void Demo01 () throws ioexception{
FileInputStream is = new FileInputStream (New File ("1.jpg"));
byte[] photo = new byte[is.available ()];
Is.read (photo);
Is.close ();

person son = new person ();
Son.setpname ("Jack 1");
Son.setage (19);
Son.setbrithdaydate (New Date ());
Son.setgender (TRUE);
Son.setphoto (photo);
Son.setdesc ("This guy has an Avatar");





Configuration configuration = new configuration (). Configure ();
Manually add
Configuration.addclass (Person.class);

Sessionfactory sessionfactory = Configuration.buildsessionfactory ();
Session session = Sessionfactory.opensession ();
Transaction Transaction = Session.begintransaction ();

Session.save (son);

Transaction.commit ();
Session.close ();
Sessionfactory.close ();


}
}

Errors that occur while testing are resolved as follows

If the picture appears too large, you can modify the size of the picture you can save in the map file.

Appears: Org.hibernate.HibernateException:Unable to get the default Bean Validation factory error

Analysis: Adding a jar to the javaweb6.0 project

FIX: Hibernate.cfg.xml in config cancel

Special characters

Analysis: SQL data cannot use keywords (desc,order, etc.)

Workaround: Add the Accent Note ("") to the left and right of the column name

A detailed description of the Hibernate hbm mapping file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.