Simple Hibernate Database Insert Example

zhaozj2021-02-16  62

Step 5: Establish your first sustainable class.

Hibernate makes ordinary Java objects (Plain Old Java Objects, Pojos, sometimes called plain

Ordinary Java Objects becomes a persistent class. A Pojo is very like Javabean, attributes via getter and setter

Method Access, the detail of internal implementation is hidden.

Package net.sf.hibernate.examples.quickstart;

PUBLIC CLASS CAT {

Private string id;

PRIVATE STRING NAME;

Private char sex;

PRIVATE FLOAT Weight;

Public cat () {

}

Public string getId () {

Return ID;

}

Public void setid (String ID) {

THIS.ID = ID;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public char getsex () {

Return SEX;

}

Public void setsex (char sex) {

THIS.SEX = SEX;

}

Public float getWeight () {

Return weight;

}

Public void setWeight (Float Weight) {

THIS.WEIGHT = Weight;

}

}

Step 6: Map CAT

The Cat.hbm.xml mapping file contains metadata required for object / relationship mapping.

Metadata contains the declaration of persistence classes and information that maps it to the database table with its properties (attribute as the value or pointing

The association of other entities).

Public "- // hibernate / hibernate mapping dtd // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

Each persistence class requires an identification attribute (actually, which represents a class representing the one-hand object, not the class representing the value object, the latter will be mapped as one of the one-handed object). This attribute is used to distinguish the persistence object: if

Cata.GetId (). Equals (CATB.GetId ()) The result is TRUE, the two cats are the same. This concept is called

Database ID. Hiernate comes with several different identifier generators for different occasions (including database locally

Sequence generator and Hi / LO high low identification mode). We use the UUID generator here and specify CAT

The table's CAT_ID field (as the primary key of the table) stores the generated identity value

Step 7: Create a CAT table in the database:

Run the following SQL:

Create Table Cat (Cat_id Character (32) Not Null, Name Varchar (32), Weight Real, Sex Character (1), Constraint PK_CAT PRIMARY Key (CAT_ID));

Step 8: Write the test page:

<% @ page import = "Net.sf.hibernate. *"%>

<% @ page import = "Net.sf.hibernate.cfg. *"%>

<% @ page import = "Net.sf.hibernate.examples.quickstart. *"%>

<%

SESSIONFACTORY sessionFactory = new configuration (). CONFIGURE (). BuildSessionFactory ();

SESSION sess = sessionFactory.openSession ();

Transaction tx = sess.begintransaction ();

Cat princess = new cat ();

Princess.setname ("Princess");

Princess.setSex ('f');

Princess.setWeight (7.4f);

Sess.save (Princess);

TX.comMit ();

Hibernateutil.closesis ();

Out.println ("CAT SET OK!");

%>

Take a look at this page, look at your database is not a data.

Now explain this code slightly:

1. SessionFactory is responsible for a database, and only one XML configuration file (hibernate.cfg.xml).

2. Session is not a thread safe, representing an action between and the database. Session through SessionFactory

Open, after all the work is completed, you need to close: hibernateutil.closesis ();

3. In the session, each database operation is performed in a transaction, so you can separate different operations (even including read-only operations). We use Hibernate's Transaction API to get out of the underlying transaction strategy (this example is JDBC transaction). This way, if we need to deploy our program to a environment that is managed by the container (using JTA), we do not need to change the source code. Please note that we have no exceptions in the examples. Ok, visit the following. http://localhost/cat/cat.jsp

转载请注明原文地址:https://www.9cbs.com/read-22407.html

New Post(0)