Hibernate implementation JPA Specification configuration

Source: Internet
Author: User

JAP (Java Persistence API) Sun launches a set of ORM-based specifications
Hibernate implements this set of specifications
Hibernate has its own independent ORM operation database, as well as the operation database method implemented by JPA specification.
The jar package is:Hibernate-entitymanager-5.0.7.final.jar
Created under SRCMeta-infFolder to create a file called Persistence.xml




Persistence.xml:
<?xml version= "1.0" encoding= "UTF-8"?>
<persistence xmlns= "Http://java.sun.com/xml/ns/persistence"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/persistence
Http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd "
version= "2.0" >
<!--at least one persistence unit (connection information for a database) is present under the root tag.
<persistence-unit name= "AAA" >
<properties>
<!--configure more than one database--
<property name= "Hibernate.connection.driver_class" value= "Com.mysql.jdbc.Driver"/>
<property name= "Hibernate.connection.url" value= "JDBC:MYSQL:///JPA" ></property>
<property name= "Hibernate.connection.username" value= "root" ></property>
<property name= "Hibernate.connection.password" value= "1234" ></property>
<!--dialect---
<property name= "Hibernate.dialect" value= "Org.hibernate.dialect.MySQLDialect" ></property>

<property name= "Hibernate.show_sql" value= "true" ></property>
<property name= "Hibernate.format_sql" value= "true" ></property>
<property name= "Hibernate.hbm2ddl.auto" value= "Update" ></property>

<!--configuration C3p0--
<property name= "Hibernate.connection.provider_class" value= "Org.hibernate.connection.C3P0ConnectionProvider" ></property>
</properties>
</persistence-unit>
</persistence>
The declaration of the constraint can be in hibernate-entitymanager-5.0.7.final.jar--org.hibernate.jpa--persistence_2_0.xsd (lines 24th through 26)




(one-to-many relationship configuration case:)
Customer:
PS: All JPA annotations are under the Javax.persistence package
@Entity
@Table (name= "Cst_customer")
public class Customer
{
@Id
@Column (name= "cust_id")
@GeneratedValue (strategy=generationtype.identity)
Private Long cust_id; ' Customer number (primary key) ', 0 null

@Column (name= "Cust_name")
Private String Cust_name; ' Customer name (company name) ',

@Column (name= "Cust_source")
Private String Cust_source; ' Customer information sources ',

@Column (name= "Cust_industry")
Private String cust_industry; ' Customer industry ',

@Column (name= "Cust_level")
Private String Cust_level; ' Customer level ',

@Column (name= "cust_address")
Private String cust_address; ' Customer contact address ',

@Column (name= "Cust_phone")
Private String Cust_phone; ' Customer Contact phone ',

A collection of contact persons
/* targetentity: type of each other
* Mappedby: The attribute name in each other (Ps:mappedby appears on which side, which means to abandon the foreign key maintenance)
* */
@OneToMany (targetentity=linkman.class,mappedby= "Customer", Cascade=cascadetype.all)
Private set<linkman> linkmans=new hashset<linkman> ();

Linkman:
@Entity
@Table (name= "Cst_linkman")
public class Linkman
{
@Id
@Column (name= "lkm_id")
@GeneratedValue (strategy=generationtype.identity)
Private Long lkm_id;//' contact number (primary key) ',

@Column (name= "Lkm_name")
Private String lkm_name;//' contact name ',

@Column (name= "Lkm_gender")
Private String lkm_gender;//' contact gender ',

@Column (name= "Lkm_phone")
Private String lkm_phone;//' contact office phone ',

@Column (name= "Lkm_mobile")
Private String lkm_mobile;//' contact phone ',

@Column (name= "Lkm_email")
Private String lkm_email;//' contact mailbox ',

@Column (name= "LKM_QQ")
Private String lkm_qq;//' contact QQ ',

@Column (name= "Lkm_position")
Private String lkm_position;//' contact position ',

@Column (name= "Lkm_memo")
Private String lkm_memo;//' contact Notes ',

On the other side of the party that has one of the objects--foreign key
@ManyToOne (Targetentity=customer.class,cascade=cascadetype.all)
/* Name: Field name for foreign key
Referencedcolumnname: Primary key field name pointing to */
@JoinColumn (name= "lkm_cust_id", Referencedcolumnname= "cust_id")
Private customer customer;






(Many-to-many relationship cases:)
Role:
@Entity
@Table (name= "Sys_role")
public class Role
{
@Id
@Column (name= "role_id")
@GeneratedValue (strategy=generationtype.identity)
Private Long role_id;//ID

@Column (name= "Role_name")
Private String role_name;//' role name ',

@Column (name= "Role_memo")
Private String role_memo;//' remarks ',

Have a collection of users
/*
* Targetentity: type of each other
* Mappedby: Name of the property in each other
* */
@ManyToMany (targetentity=user.class,mappedby= "Roles", cascade=cascadetype.persist)
Private set<user> users=new hashset<user> ();

User:
@Entity
@Table (name= "Sys_user")
public class User
{
@Id
@Column (name= "user_id")
@GeneratedValue (strategy=generationtype.identity)
Private Long user_id;//' user ID ',

@Column (name= "User_code")
Private String user_code;//' user account ',

@Column (name= "user_name")
Private String user_name;//' user name ',

@Column (name= "User_password")
Private String user_password;//' user password ',

@Column (name= "User_state")
Private String user_state;//' 1: normal, 0: Pause ',

A collection of roles
/*targetentity: type of each other
*
* */
@ManyToMany (Targetentity=role.class,cascade=cascadetype.all)
/*name: Name of the intermediate table
Joincolumns: Yourself in the middle of some configuration
Inversejoincolumns: Some configuration of the other side in the middle table */
@JoinTable (name= "Sys_user_role",
joincolumns={
/*name: The field name of your own in the middle table
Referencedcolumnname: The name of the field that points to its primary key */
@JoinColumn (name= "user_id", Referencedcolumnname= "user_id")
},
inversejoincolumns={
/*name: The field name of the other party in the middle table
Referencedcolumnname: Field name that points to the other's primary key */
@JoinColumn (name= "role_id", Referencedcolumnname= "role_id")
})
Private set<role> roles=new hashset<role> ();

1.1 Methods for manipulating data in Jap and hibernate

Operation

Hibernate the methods in

JPA the methods in

Description

Save operation

Save (Object entity)

Persist (Object entity)

Common denominator: all the temporary state objects are turned into a persistent state.

Difference:

Providers are not the same:

The Save method is provided by Hibernate.

The Persist method is provided by the JPA specification.

In case of no transaction:

Save will go to the database and Hibernate provides a built-in transaction to execute.

Persist will do nothing.

Update action

Update (Object entity)

Merge (Object entity)

Both hibernate and JPA can take advantage of the snapshot mechanism and do not invoke any method to update.

When the Update method is updated, an error occurs if an object with a level cache that already contains an identical OID is encountered. The merge can perform successfully.

Delete operation

Delete (Object entity)

Remove (Object entity)

is to delete an entity

Querying an action

Get (Class clazz,serializable ID)

Load (Class clazz,serializable ID)

Find (Class clazz,object ID)

Getreerence (Class clazz,object ID)

Both get and find are loaded immediately. Load and getreference are just as lazy to load.

Querying all operations

Query: Querying with the HQL statement

Query: Using JPQL queries

Query statements do not form the same.

The query returns a unique result operation

Uniqueresult ()

Getsingleresult ()

The query returns a unique result.

Hibernate implementation JPA Specification configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.