Hibernate technology (3)

zhaozj2021-02-16  49

1. Write the definition of the database

Hibernate requires support for the underlying database. In order to test, we also need to establish a database of the database first, then insert some commonly used data to test.

The definition of the database is as follows:

DROP TABLE TOY;

Drop Table CHILD;

Drop Table Employers;

DROP TABLE Employment_Periods;

Drop Table Employees;

Create Table CHILD

ID Number NOT NULL Primary Key

);

Create Table Toy

Toy_id Number NOT NULL Primary Key,

Child_id Number,

POSN Number Default 0,

Name varchar (20) Default '',

ConsTRAINT FK_CHILD Foreign Key (Child_id) References Child (ID)

);

DROP SEQUENCE SEQ_TOY;

DROP SEQUENCE SEQ_CHILD;

Create sequence "seq_toy" increment by 1 start with 1 maxValue 1.0e28 minvalue 1 Nocycle Cache 20 Noorder;

Create sequence "seq_child" increment by 1 start with 1 maxValue 1.0e28 minvalue 1 Nocycle Cache 20 noorder;

Mainly do these work: Apply the primary key, declare the foreign key, create the Sequence corresponding to the primary key, pay attention to the Oracle database.

2, ok, we can write an ANT script to compile our programs.

The main Target is as follows:

Define environment variables:

Compile:

3, ok, we can use our hibernate now.

For example, we insert a child, write a function as follows:

Public int insertchild (child c) throws hibernateException {

Transaction TX = NULL;

Session s = null;

Try {

S = HibernateSessionFactory.openSession ();

Tx = s.begintransaction (); s.save (c);

TX.comMit ();

} catch (hibernateException he) {

IF (tx! = null) {

TX.rollback ();

}

Throw He;

} finally {

s.close ();

}

Return C.GetId ();

}

The key part is marked with red. In fact, we call the Save method for the Session class. There are still many other methods that can be called, as follows: Update, Load, Delete, etc. It can be saved in a reference to a class. The effect is similar to INSERT INTO ..., then our test code is as follows:

// First instantiate three TOY classes

Toy T1 = new Toy ();

T1.setname ("WAWA1");

Toy T2 = new Toy ();

T2.setname ("WAWA2");

Toy T3 = New Toy ();

T3.setname ("WAWA3");

Toy [] TOYS1 = New Toy [] {T1, T2};

Toy [] TOYS2 = New Toy [] {T1, T2, T3};

Toy [] TOYS3 = New Toy [] {T1, T2};

Child C = new child ();

C.SETTOYS (TOYS1);

Ed.Insert (c); // Insert Child containing two Toy

c = (child) ed.findbyid (c.getclass (), 1); // get this CHILD class from the database

THIS.ASSERTEQUALS (2, C.Gettoys (). Length); // should be two Toy

C.SETTOYS (TOYS2); // Reset toys attribute

Ed.Update (c); / / Update to the database

The use unit test results are as follows:

Compile:

[echo] compling ...

Test:

[echo] Run Test ...

[Java]. 15: 13: 21,818 Info Environment: 378 - Hibernate 2.0 Beta 6

[Java] 15: 13: 21,878 INFO Environment: 412 - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class = oracle.jdbc.driver.OracleDriver, hibernate.cglib.use_reflection_optimizer = true, hibernate.dialect = net. sf.hibernate.dialect.OracleDialect, hibernate.jdbc.use_streams_for_binary = true, hibernate.jdbc.batch_size = 0, hibernate.query.substitutions = true 1, false 0, yes 'Y', no 'N', hibernate.query. imports = net.sf.hibernate.test, net.sf.hibernate.eg, hibernate.connection.username = zhaocw, hibernate.connection.url = jdbc: oracle: thin: @CCSERVER: 1521: oracle8i, hibernate.show_sql = true , hibernate.connection.password = zhaocw, hibernate.statement_cache.size = 25, hibernate.connection.pool_size = 10} [java] 15: 13: 21,898 INFO Environment: 426 - using java.io streams to persist binary types

[Java] 15: 13: 21,898 Info Environment: 427 - USING CGLIB REFLECTION OPTIMizer

[Java] 15: 13: 21,928 Info Environment: 437 - JVM Proxy Support: TRUE

[Java] 15: 13: 21,938 Info Configuration: 689 - Configuration Resource: /Hibernate.cfg.xml

[Java] 15: 13: 22,929 Info Configuration: 270 - mapping resource: hibernate / child.hbm.xml

[Java] 15: 13: 23, 170 Info Collection: 174 - Mapping Class: Hibernate.Child -> CHILD

[Java] 15: 13: 23,340 Info Configuration: 270 - mapping resource: hibernate / toy.hbm.xml

[Java] 15: 13: 23,420 Info Collection: 174 - Mapping Class: Hibernate.Toy -> Toy

[Java] 15: 13: 24,411 Info Configuration: 871 - Configured SessionFactory: Null

[Java] 15: 13: 24, 411 Info Collection: 976 - mapping collection: hibernate.child.toys -> Toy

[Java] 15: 13: 25,242 Info SessionFactoryImpl: 140 - Building Session Factory

[Java] 15: 13: 25,262 Info Diagect: 37 - Using Diagect: Net.sf.hibernate.DiaLect.OraclediaAlaforct [Java] 15: 13: 25,272 Info DriverManagerConnectionProvider: 41 - Hibernate Connection Pool Size: 10

[Java] 15: 13: 25,292 Info DriverManagerConnectionProvider: 70 - USING DRIVER: Oracle.jdbc.driver.Orcledriver At URL: JDBC: Oracle: Thin: @ccserver: 1521: Oracle8i

[Java] 15: 13: 25,292 Info DriverManagerConnectionProvider: 71 - Connection Properties: {user = zhaocw, password = zhaocw}

[Java] 15: 13: 25, 323 Info PreparedStatementCache: 60 - Prepared Statement Cache Size: 25

[Java] 15: 13: 25,333 Info sessionFactoryImpl: 170 - Use Outer Join Fetching: True

[Java] 15: 13: 26,033 Info SessionFactoryImpl: 193 - Use Scrollable Result Sets: true

[Java] 15: 13: 26,033 Info sessionFactoryImpl: 202 - Echoing all sql to stdout

[Java] 15: 13: 27,145 Info sessionFactoryObjectFactory: 82 - No Jdni Name Configured

[Java] 15: 13: 27,145 Info sessionFactoryImpl: 287 - Query Language Substitutions: {no = 'n', true = 1, yes = 'y', false = 0}

[java] Hibernate: SELECT SEQ_CHILD.NEXTVAL from DUAL

[java] Hibernate: SELECT SEQ_TOY.NEXTVAL from DUAL

[java] Hibernate: SELECT SEQ_TOY.NEXTVAL from DUAL

[java] Hibernate: INSERT INTO CHILD (ID) VALUES (?)

[java] Hibernate: INSERT INTO TOY (Name, Toy_ID) VALUES (?,?)

[java] hibernate: Update Toy SET CHILD_ID =?, POSN =? Where Toy_ID =?

[java] hibernate: SELECT CHILD0_.ID AS ID from child product_where child0_.id =?

[java] hibernate: select toy0_.toy_id as Toy_ID__, TOY0_.POSN AS POSN__ID, TOY0_.NAME AS NAME from Toy Toy0_ Where Toy0_.child_id =?

[java] Hibernate: SELECT SEQ_TOY.NEXTVAL from DUAL

[java] hibernate: Insert Into Toy (Name, Toy_ID) VALUES (?,?) [java] hibernate: Update Toy Set Name =? Where Toy_ID =?

[java] hibernate: Update Toy Set Child_ID = NULL, POSN = NULL Where Child_ID =?

[java] hibernate: Update Toy SET CHILD_ID =?, POSN =? Where Toy_ID =?

[java] hibernate: SELECT CHILD0_.ID AS ID from child product_where child0_.id =?

[java] hibernate: select toy0_.toy_id as Toy_ID__, TOY0_.POSN AS POSN__ID, TOY0_.NAME AS NAME from Toy Toy0_ Where Toy0_.child_id =?

[java] Hibernate: delete from Toy Where Toy_ID =?

[java] hibernate: Update Toy Set Name =? Where Toy_ID =?

[java] hibernate: Update Toy Set Child_ID = NULL, POSN = NULL Where Child_ID =?

[java] hibernate: Update Toy SET CHILD_ID =?, POSN =? Where Toy_ID =?

[java] Time: 6.269

[java] OK (1 test)

You can see the SQL statement that actually executes Hibernate, the test results are successful.

Five, summary

In fact, Hibernate can achieve multi-level inheritance architecture, often use of , , , , and many more. There is also a component function allows a property class to store in the parent class. In short, Hibernate features are very powerful and very efficient. It is recommended that you have continuously summarized experience in the process of use, it is best to share your experience.

Related connections:

Hibernate Forum: http://sourceforge.net/forum/forum.php? Forum_ID = 128638

Hibernate Home: http://hibernate.sourceforge.net

FAQ: http://hibernate.bluemars.net/14.html?cowiki=c39f9c2408fc3b0ae5d9eeadd0953905#9

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

New Post(0)