We can see that a child can have a lot of toys, this is a distinctive couple, there is such a label in Hibernate
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:
session-factory>
Hibernate-Configuration>
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
SEQ_CHILD param>
generator>
id>
array>
clas>
hibernate-maping>
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
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
Ok, look at the toy.hbm.xml file
SEQ_TOY param>
generator>
id>
clas>
hibernate-maping>
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.