Hibernate gossip: Custom insert, update, and delete

Source: Internet
Author: User
The <SQL-Insert>, <SQL-Update>, and <SQL-delete> labels are added to the hibernate ing file of hibernate 3, you can use SQL in these three tags to customize your insert, update, and delete actions, that is, when storing, updating, or deleting data. For example:
  • User. HBM. xml
<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">


<class name="onlyfun.caterpillar.User" table="user">

<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>

<property name="name" column="name" type="java.lang.String"/>

<property name="age" column="age" type="java.lang.Integer"/>

<sql-insert>
INSERT INTO user (name, age) VALUES (?, ?)
</sql-insert>
<sql-update>
UPDATE user SET name=?, age=?, WHERE id=?
</sql-update>
<sql-delete>
DELETE FROM user WHERE id=?
</sql-delete>

</class>

? The corresponding order is the order in which attributes appear in the ing file. Assume that you store the object: Session = sessionfactory. opensession ();
Tx = session. begintransaction ();

Session. Save (User );
TX. Commit ();
Session. Close ();

The SQL statement displayed in the execution result will be your custom statement, instead of the statement automatically generated by hibernate:

Hibernate:
Insert into user (name, age) values (?, ?)

 

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.