Hibernate technology (2)

zhaozj2021-02-16  55

We can see that a child can have a lot of toys, this is a distinctive couple, there is such a label in Hibernate , then we can write mapping for these two categories .xml file, in Hibernate, each of the long-lasted classes requires the corresponding mapping file, so Hibernate knows how to correct the Java Class Property to Table Column, sometimes I don't have to indicate that Java The type of attribute, Hibernate has a Reflection mechanism, which can be perceived with a type of property at runtime, and then correspond to the corresponding database type.

1, configure hibernate.properties file

You can copy a copy, then you can use it slightly, the key is the following:

Configure the database, as follows

## Oracle

Hibernate.Dialev Net.sf.hibernate.DiaLect.OrglediaLect

Hibernate.Connection.driver_class oracle.jdbc.driver.Oracledriver

Hibernate.Connection.url JDBC: Oracle: Thin: @locahost: 1521: Oracle8i

Hibernate.Connection.username Test

Hibernate.Connection.Password Test

Set development mode

## print all generated sql to the console

Hibernate.show_sql true

In this way, you can see the actual SQL statement that is actually running.

2, let's create a file called hibernate.cfg.xml, so Hibernate knows the database's properties and the location of the mapping file to be loaded. The document content is as follows:

Net.sf.hibernate.dialect.OrcledgeAALECT

Let me explain, hibernate-configuration is the top element, then the Property Diact tells the Hibernate underlying database name. For different database hibernated, it has built corresponding classes, these classes are in net.sf.hibernate.dialet package In, why? Because each database has different O / R mapping mechanisms, and the mechanism to handle primary keys is very different, it is necessary to do this. In fact, the switching database is also very convenient for Hibernate, as long as this property is replaced. Of course, if you use the database-specific configuration in other profiles, you must also make a corresponding modification.

The next mapping resource = "" means the mapping file corresponding to each persistent class. Of course, you can also write a mapping file for each class. If possible, I suggest it to logically relevant classes or A wrapped a mapping file is ok. This looks more compact, you won't see your source code is an XML file everywhere. Of course, there is currently a tool called XDoclet, which can automatically help you generate mapping files, but I think that automation is not very flexible after all, most of which use Hibernate's use yourself to write mapping files. All I also recommend everyone when studying hibernate, don't lazy, using XDoclet with XDoclet at the beginning, so that your foundation will be very unresolved. Another point is that this resource = value is Value inside it is correct to the name of the package. Under normal circumstances, put the mapping files of the class and classes together.

3, write mapping files

Everyone may care about how hibernate does the Java Class correspond to the Table Column? In fact, in Hibernate's documentation, this is more clear, there is a chapter inside to introduce the mapping mechanism, because this is the foundation of the application of Hibernate, you will find that you will spend most of your time, and Not writing a SQL statement.

First look at the mapping file of the Child class first

// Put the Java Class to Database Table

// Primary key description

// Using Oracle's Sequence Technology Automatic Since

SEQ_CHILD

// Description and Toy's one-to-many relationship

// to write index because there is array

Need to pay attention to the following:

1) If you use one-to-man relationship, and use the session.saveorupdate () method, then you better let the property has a unsaved-value = "0" property, otherwise it will throw a violation of other keys Exception.

2) Note that the One-to-MANY relationship between Child-Toy is associated with foreign bonds. This foreign key is the child_id field inside the TOY table.

3) Since we use array to implement one-to-man relationship, we must develop an extra field to store this Array index, so that the serial number you store and you are consistent, of course, if You don't want Hibernate to remember that the sequence does not have this field, you can replace 4) 4) Note The cascade property inside, this property is important, it represents, if this property is ALL, then when Save, Update, Delete When a child class, its property TOYS will get Save, Update, or Delete, otherwise it will not. Of course, you can also implement this feature using interface Lifecycle that implements Hibernate. But there is no need. Note that this feature should be noted that hibernate has no garbage collection function. When you delete a TOY in a Child class, do not delete this record in the database, you must manually remove it. . This is a problem that Hibernate developers are thinking about improvement. They may use JDO mechanisms to achieve garbage collection.

Ok, look at the toy.hbm.xml file

SEQ_TOY

Similarly, the TOY class makes the way the primary key is generated, and a simple attribute Name, because the database's field name is consistent, so it does not use the column property in Property.

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

New Post(0)