Statistics
65
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2025-11-23

AuthorAmit K.

No description

Tags
No tags
Publish Year: 2024
Language: 英文
File Format: PDF
File Size: 14.3 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

' » i Aifriitj K f, Th PROGRAMMING CRASH COURSE DAYS With Hands-On Exercises and Code Snippets
JPA JAVA PERSISTENCE FOR BEGINNERS In This Book You Will Learn Explanation with Diagramme and Working Exercises with Successfull Execution of Exercises with Output Readymade Solution With Step By Step Explanation
AMITK Contents Beginners Guide to Learn JPA Programming Step by Step Introduction Chapter 1: JPA One to One Mapping 1 Example 1: JPA OneToOne Mapping 2 Example 2 : JPA OneToOne Lazy Load Mapping 3 Example 3 : JPA OneToOne Join Column Mapping 4 Example 4 : JPA OneToOne Primary Key Join Column Mapping 5 Example 5 : JPA OneToOne Map Cascade Mapping 6 Example 6 : JPA OneToOne Unidirectional Mapping
7 Example 7 : JPA OneToOne Bidirectional Mapping Chapter 2 : JPA One to Many Mapping 1 Example 8 : JPA OneToMany Mapping 2 Example 9 : JPA OneToMany Join Table Mapping 3 Example 10: JPA OneToMany Unidirectional Mapping 4 Example 11: JPA OneToMany OrderBy Mapping 5 Example 12: JPA Persist OrderedList Mapping 6 Example 13: JPA OneToMany Map Key Mapping 7 Example 14: JPA OneToMany Untyped Mapping Chapter 3 : JPA Many to One Mapping 1 Example 15: JPA ManyToOne Mapping 2 Example 16: JPA ManyToOne Join Columns Mapping 3 Example 17: JPA ManyToOne Join Column Mapping Chapter 4: JPA Many to Many Mapping 1 Example 18: JPA ManyToMany Mapping 2 Example 19: JPA ManyToMany Join Table Mapping
3 Example 20: JPA ManyToMany Bidirectional Mapping 4 Example 21: JPA ManyToMany Embeddable Mapping Introduction Learning JPA Programming step by step. JPA (Java Persistence API) is a powerful framework that can be used to quickly build robust data- driven application. Most of the data that our applications manipulate have to be stored in data stores, retrieved, processed and analyzed. If this data store is a relational database and you use an object-oriented programming language such as Java, then you should have a look at JPA.
JPA is an Object-Relational Mapping tool that maps Java objects to relational databases and allows query operations. In this book, you will learn Java Persistence API, its annotations for mapping entities. JPA Integration with spring frameworks. Different Scenario (To Apply > Different Types of Mapping) Suppose There is to Two Object , Student and dress Student and Addr Want to Communicate to each other. Student want to Access -> Details of Address object. Student want to Access -> Details of AND Also want to Access-> Details of Student object Then How to do this , How to Achieve this Scenario Then there is a Mechanism to Achieve this Scenario , through Primary Key and <ey. between Primary Key and then onlyIf any how we will Establish Relationship Student Top i Uni Directional Mapping ▼ Address Bottom Student Address 4 i Bi ♦ Top Directional Mapping Bo* Bottom
Chapter 1: JPA One to One Mapping 1 JPA OneToOne Mapping 2 JPA OneToOne Lazy Load Mapping 3 JPA OneToOne Join Column Mapping 4 JPA OneToOne Primary Key Join Column Mapping 5 JPA OneToOne Map Cascade Mapping 6 JPA OneToOne Unidirectional Mapping
7 JPA OneToOne Bidirectional Mapping 1 JPA OneToOne Mapping Person t Department Here you will learn how to do one to one map in JPA mapping.
This example assumes that one Person can only be part of one department and one department can only have one person. In Person entity we mark the Department reference property with ©OneToOne annotation. ©Entity public class Person { • • • ©OneToOne private Department department; Here is the simple code to setup the two entities and save them to database. Person pl = new Person("Tom"); p 1 .setName("Tom"); Department d = new Department); d.setName("Design"); p 1 .setDepartment(d); em.persist(pl);
em.persist(d); Example 1: In This example assumes that one Person can only be part of one department and one department can only have one person. Person.java package com.abd.jpa; import javax.persistence.Entity; import j avax.persistence.GeneratedValue; import j avax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.One ToOne; ©Entity public class Person { ©Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id;
private String name; ©OneToOne private Department department; public Person() {} public Person(String name) { this.name = name; public Department getDepartment() { return department; public void setDepartment(Department department) { this.department = department; public Long getld() { return id; public void set!d(Long id) { this.id = id;
public String getNameQ { return name; public void setName(String name) { this.name = name; ©Override public String toStringO { return "Person [id=" + id + ", name=" + name + "]"; Department.java package com.abd.jpa; import javax.persistence.Entity; import j avax.persistence.GeneratedValue; import j avax.persistence.GenerationType; import javax.persistence.Id;
©Entity public class Department { ©Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; public long getld() { return id; public void setld(long id) { this.id = id; public String getNameQ { return name; public void setName(String name) { this.name = name;
PersonDaoImpl.java package com.abd.jpa; import javax.persistence.EntityManager; import javax.persistence.Persistencecontext; import org.springframework.transaction.annotation.Transactional; ©Transactional public class PersonDaoImpl { public void test(){ Person pl = new Person("Tom"); p 1 .setName("Tom"); Department d = new Department); d.setName("Design"); p 1 .setDepartment(d); em.persist(pl);
em.persist(d); ©Persistencecontext private EntityManager em; Helper.java package com.abd.jpa; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; public class Helper { public static void checkData() { try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/dbl", "root", "root"); Statement st = conn.createStatementQ;
ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String!] {"TABLE"}); while (mrs.next()) { String tableName = mrs.getString(3); System.out.println("\n\n\n\nTable Name:"+ tableName); ResultSet rs = st.executeQueryf'select * from" + tableName); ResultSetMetaData metadata = rs.getMetaDataQ; while (rs.nextQ) { System.out.println(" Row:"); for (int i = 0; i < metadata.getColumnCount(); i++) { System.out.println(" Column Name:"+ metadata.getColumnLabel(i + 1)+ ", "); System.out.println(" Column Type:"+ metadata.getColumnTypeName(i + 1)+ ": " Object value = rs.getObject(i + 1); System.out.println(" Column Value:"+value+"\n"); }catch(Exception e){ e.printStackTrace();
persistence.xml < ?xml version=" 1.0" encoding="UTF-8"? > persistence version="1.0" 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/per- sistence/persistence_l_O.xsd"> < persistence-unit name-'jpaData" /> </persistence > applicationContext.xml < ?xml version=" 1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http:// www.springframework.org/schema/context" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http:// www.w3 .org/ 2001 /XMLSchema-instance" xsi: schemaLocation=" http://www.springframework.Org/schema/aophttp://www.springframework.org/schema/aop/ spring-aop- 3.2 .xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/ beans/spring-beans- 3.2 .xsd http://www.springframew0rk.0rg/schema/c0ntexthttp://www.springframew0rk.0rg/schema/ context/spring-context-3.2 .xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ spring-tx- 3.2 .xsd" > < tx: annotation-driven /> <context:annotation-config /> <bean id="personDao" class="com.abd.jpa.PersonDaoImpl" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" > <value>com.mysql.jdbc.Driver</value> < /property > < property name="url" > < value >jdbc:mysql://localhost/db 1 </value> < /property > < property name="username" > < value > root < / value > < /property > < property name="password" > < value > root < / value > < /property > </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref = "dataSource" /> <property name="persistenceUnitName" value="jpaData" /> < property name="jpaVendorAdapter" > <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendor Adapter" I > < /property > < property name="jpaProperties" >
< props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> < prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> < /props > < /property > </bean> <bean id="transactionManager" class="org.springframework.orm.jpaJpaTransactionManager"> < property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> </beans> Client.java package com.abd.jpa; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) {