A summary of Hibernate knowledge points (iv)--JPA

Source: Internet
Author: User

I. Introduction and Introduction to JPA

Jpa:java Persistence API, interface Specification
Hinernate internally provides implementations for the JPA specification
    
Development Steps (Entitymanager):
1. Import Additional packages: Hibernate-release-5.0.7.final\lib\jpa\hibernate-entitymanager-5.0.7.final.jar
2. Configure entities with JPA Annotations (entity-to-table mapping)
3. Configure the Eneitymanager core configuration file
Create a folder under the class load path (SRC), name Meta-inf
Create a core configuration file in the Meta-inf directory Persistence.xml
4. Use the Entitymanager API to manipulate entities



Two, Hibernate JPA one-to-many configuration
Create entity
Configure mapping Relationships
Customer side:
//Current customer who has contact
//One side: Config one-to-many, on the other side maintain foreign keys (a party that abandons foreign key maintenance rights)
//targetentity: Each other's bytecode object
//mappedby: Which property the other party uses to maintain the relationship with us (Mappedby is configured on the party that abandons the foreign key maintenance rights)
@OneToMany (targetentity=linkman.class,mappedby= "customer")
private Set<linkman> Linkmans = new hashset<> ();
        
Linkman Party:
//Current contact belongs to which customer
//Configure many-to-one
//targetentity: Each other's bytecode object
@ManyToOne (Targetentity=customer.class)
//Maintain foreign key joincolumn: Connection field information (foreign key information)
//name: Foreign key name referencedcolumnname: Primary key name corresponding to foreign key
@JoinColumn (name= "lkm_cust_id", Referencedcolumnname= "cust_id")
private customer customer;
        
manipulating Entities


Three, Hibernate JPA multi-to-many configuration
Create entity
Configure mapping Relationships
User side:
//What roles the user currently has
//Configure many-to-many
//targetentity: Each other's bytecode object
//mappedby: Which property the other party uses to maintain the relationship with us (the party that abandons the foreign key maintenance rights is configured)
@ManyToMany (targetentity=role.class,mappedby= "users")
private set<role> roles = new hashset<> ();
Role side:
//What users are currently using this role
//Configure many-to-many
//targetentity: Each other's bytecode object
@ManyToMany (Targetentity=user.class)
//user Discard foreign key maintenance rights role to maintain foreign keys
//configuration Relationship
//name: Name of the intermediate table
@JoinTable (
name= "Sys_user_role",
Name of//joincolumns: foreign key names generated by the party (Role) in the intermediate table
//joincolumns Referencedcolumnname: The name of the primary key corresponding to the foreign key generated by the party in the intermediate table
joincolumns={@JoinColumn (name= "rid", referencedcolumnname= "role_id")},
//inversejoincolumns Name: The other party (User) produces the foreign key names in the intermediate table
//inversejoincolumns Referencedcolumnname: The other party (User) produces the primary key name corresponding to the foreign key in the intermediate table
inversejoincolumns={@JoinColumn (name= "UID", referencedcolumnname= "user_id")}
            )
Private set<user> users = new hashset<> ();
manipulating Entities


The relationship of Orm-->jpa-->hibernate













Hibernate Knowledge Point Summary (iv)--JPA

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.