Yesterday we did hibernate configuration, and we have to query the corresponding data based on the ID of the operation, today we look at hibernate in a few configuration files, in the end there is something.
First, Hibernate.cfg.xml
| 1234567891011121314151617 |
<hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <propertyname="connection.url"> jdbc:mysql://127.0.0.1:3306/superktv </property> <propertyname="connection.username">root</property> <propertyname="connection.password">root</property> <propertyname="connection.driver_class"> com.mysql.jdbc.Driver </property> <propertyname="myeclipse.connection.profile">mysql</property> <mappingresource="com/pxy/dao/Author.hbm.xml"/> </session-factory></hibernate-configuration> |
There should be no bad understand it, pay attention to a little,<mapping> the path of the tag do not refer to the wrong, each time after the reference, remember to hold down the CTRL key, and then move the mouse to the value of resource, if the contents of double quotes into a hyperlink, That means no problem. Of course, if you're generating files through Dbbrowser, that's certainly not a reference error.
Second, Author.hbm.xml
| 1234567891011121314151617181920212223 |
<hibernate-mapping> <classname="com.pxy.entity.Author"table="author" catalog="blogdb"> <idname="id"type="java.lang.Integer"> <columnname="id"/> <generatorclass="native"/> </id> <propertyname="username"type="java.lang.String"> <columnname="username" length="20"not-null="true"/> </property> <propertyname="password"type="java.lang.String"> <columnname="password"length="10"not-null="true" /> </property> <propertyname="email"type="java.lang.String"> <columnname="email"length="20"/> </property> <property name="address"type="java.lang.String"> <columnname="address"length="20"/> </property> <propertyname="phone"type="java.lang.String"> <columnname="phone"length="20"/> </property> </class></hibernate-mapping> |
The name of the class tag represents the corresponding entity class, table represents the corresponding data table, and catalog represents the corresponding database.
ID Tag: Represents the primary key.
Generator tag: The generation strategy of the primary key.
Property Label: Represents the attribute of the entity class.
Column Label: Represents the field in the corresponding table.
For the operation of a single object, we have used the Load method last time, in addition, the following four are commonly used:
Get () method, Save () method, Update () method, delete () method
From the name should be able to guess the corresponding usage, here is not one by one demo, you crossing self-solve it ·
Finally, simply say the difference between load () and get ():
The load () method is a deferred load, which only returns a proxy object (containing only the ID) when invoked, and throws an Objectnotfoundexception exception if no data corresponding to that ID is thrown.
The Get () method is loaded in real time, returning a record of the ID corresponding to the call and returning a null object if the data does not exist.
To test the effect, we added <propertyname= "Show_sql" >true</property> " <property name=" to Hibernate's configuration file Format_sql ">true</property>.
Note the difference between the following two code and the result.
As for the data that does not exist, let's try it out for ourselves. Let's call here today. Next we will learn a little bit more advanced and a little bit of knowledge.
Getting Started from Java to quitting: basic usage of Hibernate