🗊Презентация 3. Java Persistence API. 4. Entity Relationships

Нажмите для полного просмотра!
3. Java Persistence API. 4. Entity Relationships, слайд №13. Java Persistence API. 4. Entity Relationships, слайд №23. Java Persistence API. 4. Entity Relationships, слайд №33. Java Persistence API. 4. Entity Relationships, слайд №43. Java Persistence API. 4. Entity Relationships, слайд №53. Java Persistence API. 4. Entity Relationships, слайд №63. Java Persistence API. 4. Entity Relationships, слайд №73. Java Persistence API. 4. Entity Relationships, слайд №83. Java Persistence API. 4. Entity Relationships, слайд №93. Java Persistence API. 4. Entity Relationships, слайд №103. Java Persistence API. 4. Entity Relationships, слайд №113. Java Persistence API. 4. Entity Relationships, слайд №123. Java Persistence API. 4. Entity Relationships, слайд №133. Java Persistence API. 4. Entity Relationships, слайд №143. Java Persistence API. 4. Entity Relationships, слайд №153. Java Persistence API. 4. Entity Relationships, слайд №163. Java Persistence API. 4. Entity Relationships, слайд №173. Java Persistence API. 4. Entity Relationships, слайд №183. Java Persistence API. 4. Entity Relationships, слайд №193. Java Persistence API. 4. Entity Relationships, слайд №203. Java Persistence API. 4. Entity Relationships, слайд №213. Java Persistence API. 4. Entity Relationships, слайд №223. Java Persistence API. 4. Entity Relationships, слайд №233. Java Persistence API. 4. Entity Relationships, слайд №243. Java Persistence API. 4. Entity Relationships, слайд №253. Java Persistence API. 4. Entity Relationships, слайд №263. Java Persistence API. 4. Entity Relationships, слайд №273. Java Persistence API. 4. Entity Relationships, слайд №283. Java Persistence API. 4. Entity Relationships, слайд №293. Java Persistence API. 4. Entity Relationships, слайд №303. Java Persistence API. 4. Entity Relationships, слайд №313. Java Persistence API. 4. Entity Relationships, слайд №323. Java Persistence API. 4. Entity Relationships, слайд №333. Java Persistence API. 4. Entity Relationships, слайд №343. Java Persistence API. 4. Entity Relationships, слайд №353. Java Persistence API. 4. Entity Relationships, слайд №363. Java Persistence API. 4. Entity Relationships, слайд №373. Java Persistence API. 4. Entity Relationships, слайд №383. Java Persistence API. 4. Entity Relationships, слайд №393. Java Persistence API. 4. Entity Relationships, слайд №403. Java Persistence API. 4. Entity Relationships, слайд №413. Java Persistence API. 4. Entity Relationships, слайд №423. Java Persistence API. 4. Entity Relationships, слайд №43

Содержание

Вы можете ознакомиться и скачать презентацию на тему 3. Java Persistence API. 4. Entity Relationships. Доклад-сообщение содержит 43 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1





3. Java Persistence API
4. Entity Relationships
Описание слайда:
3. Java Persistence API 4. Entity Relationships

Слайд 2





Basics
A foreign key can be one or more columns that reference a unique key, usually the primary key, in another table
A foreign key and the primary parent key it references must have the same number and type of fields
Foreign keys represents relationships from a column or columns in one table to a column or columns in another table
Описание слайда:
Basics A foreign key can be one or more columns that reference a unique key, usually the primary key, in another table A foreign key and the primary parent key it references must have the same number and type of fields Foreign keys represents relationships from a column or columns in one table to a column or columns in another table

Слайд 3





Multiplicity in Entity Relationships
Multiplicities are of the following types:
Many-to-one: Multiple instances of an entity can be related to a single instance of the other entity
One-to-one: Each entity instance is related to a single instance of another entity
One-to-many: An entity instance can be related to multiple instances of the other entities
Many-to-many: The entity instances can be related to multiple instances of each other.
Описание слайда:
Multiplicity in Entity Relationships Multiplicities are of the following types: Many-to-one: Multiple instances of an entity can be related to a single instance of the other entity One-to-one: Each entity instance is related to a single instance of another entity One-to-many: An entity instance can be related to multiple instances of the other entities Many-to-many: The entity instances can be related to multiple instances of each other.

Слайд 4





CM Database Schema
Описание слайда:
CM Database Schema

Слайд 5





Many-to-One Mappings
A many-to-one mapping is defined by annotating the attribute in the source entity (the attribute that refers to the target entity) with the @ManyToOne  annotation
A @JoinColumn(name=Fkname) annotation discribes a foreing key of a relationship
Описание слайда:
Many-to-One Mappings A many-to-one mapping is defined by annotating the attribute in the source entity (the attribute that refers to the target entity) with the @ManyToOne annotation A @JoinColumn(name=Fkname) annotation discribes a foreing key of a relationship

Слайд 6





Exercise: Read a Payment
Create a project for read payment data by payment id with a merchant name instead of merchant id
Описание слайда:
Exercise: Read a Payment Create a project for read payment data by payment id with a merchant name instead of merchant id

Слайд 7





CM Database Schema
Описание слайда:
CM Database Schema

Слайд 8





Payment Entity (1 of 2)
@Entity
public class Payment {
	@Id
 	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	private double charge;
	private double total;
	private String goods;
	private java.sql.Date dt;
	@ManyToOne
	@JoinColumn(name="merchantId")
	private Merchant merchant;
Описание слайда:
Payment Entity (1 of 2) @Entity public class Payment { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private double charge; private double total; private String goods; private java.sql.Date dt; @ManyToOne @JoinColumn(name="merchantId") private Merchant merchant;

Слайд 9





Payment Entity (2 of 2)
	public Merchant getMerchant(){ return merchant; }
	public void setMerchant(Merchant value){ 
		merchant = value;
	}
.    .   .   .   .   .   .  . .    .   .   .   .   .   .  .
	public String getStringForPrint() {
		String txt = "id = " + id + ";   date = ";
		SimpleDateFormat dtFrm = new SimpleDateFormat("dd.MM.yyyy"); 
        	txt += dtFrm.format(dt) + ";   merchant = " + merchant.getName();
		txt += ";   total = " + total + ";    charge = " + charge;
		return txt;
	}
Описание слайда:
Payment Entity (2 of 2) public Merchant getMerchant(){ return merchant; } public void setMerchant(Merchant value){ merchant = value; } . . . . . . . . . . . . . . . . public String getStringForPrint() { String txt = "id = " + id + "; date = "; SimpleDateFormat dtFrm = new SimpleDateFormat("dd.MM.yyyy"); txt += dtFrm.format(dt) + "; merchant = " + merchant.getName(); txt += "; total = " + total + "; charge = " + charge; return txt; }

Слайд 10





DAO & Service Interfaces
public interface PaymentDao {
	public List<Payment> findByMerchantId(int id);
	public double getPaymentSum();
	public Payment findById(int id);
}
public interface PaymentService {
	public List<Payment> findByMerchantId(int id);
	public double getPaymentSum();
	public Payment findById(int id);
}
Описание слайда:
DAO & Service Interfaces public interface PaymentDao { public List<Payment> findByMerchantId(int id); public double getPaymentSum(); public Payment findById(int id); } public interface PaymentService { public List<Payment> findByMerchantId(int id); public double getPaymentSum(); public Payment findById(int id); }

Слайд 11





PaymentDaoImpl Class
public Payment findById(int id){
    return em.find(Payment.class, id);
}
Описание слайда:
PaymentDaoImpl Class public Payment findById(int id){ return em.find(Payment.class, id); }

Слайд 12





Main Class
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     PaymentService paymentService = 	context.getBean(PaymentService.class);
     Payment p = paymentService.findById(2);
     System.out.println(p.toString());
}
Описание слайда:
Main Class @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); PaymentService paymentService = context.getBean(PaymentService.class); Payment p = paymentService.findById(2); System.out.println(p.toString()); }

Слайд 13





Exercise: Read a Payment
See P351ReadPayment project for the full text
Описание слайда:
Exercise: Read a Payment See P351ReadPayment project for the full text

Слайд 14





Exercise: Group Payments
Modify P237Grouping project using entities from the previous project
Описание слайда:
Exercise: Group Payments Modify P237Grouping project using entities from the previous project

Слайд 15





PaymentDaoImp Class
public List<Result> getTotalReport(){
    String txt = "SELECT new com.bionic.edu.Result    
         (p.merchant.name, count(p), SUM(p.chargePayed))";
    txt += "FROM Payment p GROUP BY p.merchant.name"; 
    TypedQuery<Result> query = em.createQuery(txt, 	Result.class);
    return query.getResultList();
}
Описание слайда:
PaymentDaoImp Class public List<Result> getTotalReport(){ String txt = "SELECT new com.bionic.edu.Result (p.merchant.name, count(p), SUM(p.chargePayed))"; txt += "FROM Payment p GROUP BY p.merchant.name"; TypedQuery<Result> query = em.createQuery(txt, Result.class); return query.getResultList(); }

Слайд 16





Main Class
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     PaymentService paymentService = 	context.getBean(PaymentService.class);
     List<Result> list = paymentService.getTotalReport();
     for(Result r: list)
	System.out.format("%1$25s  %2$3d  %3$8.2f \n", 	  		r.getName(), r.getCount(), r.getSum()); 
}
Описание слайда:
Main Class @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); PaymentService paymentService = context.getBean(PaymentService.class); List<Result> list = paymentService.getTotalReport(); for(Result r: list) System.out.format("%1$25s %2$3d %3$8.2f \n", r.getName(), r.getCount(), r.getSum()); }

Слайд 17





Exercise: Group Payments
See P352GroupPayment project for the full text
Описание слайда:
Exercise: Group Payments See P352GroupPayment project for the full text

Слайд 18





One-to-Many Mappings
An entity instance can be related to multiple instances of the other entities
One-to-many relationships use the @OneToMany annotation on the corresponding persistent property or field
The  mappedBy element is needed to refer to the attribute annotated by ManyToOne in the corresponding entity:
 @OneToMany(mappedBy=“attribute”)
A one-to-many association needs to map the collection of entities
Описание слайда:
One-to-Many Mappings An entity instance can be related to multiple instances of the other entities One-to-many relationships use the @OneToMany annotation on the corresponding persistent property or field The mappedBy element is needed to refer to the attribute annotated by ManyToOne in the corresponding entity: @OneToMany(mappedBy=“attribute”) A one-to-many association needs to map the collection of entities

Слайд 19





Exercise: Read a Merchant’s Payments
Create a project for read payment data that correspond to a merchant given by its id
Описание слайда:
Exercise: Read a Merchant’s Payments Create a project for read payment data that correspond to a merchant given by its id

Слайд 20





CM Database Schema
Описание слайда:
CM Database Schema

Слайд 21





Merchant Entity
@Entity
public class Merchant {
	@Id
 	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	private String name;
	private double charge;
	private int period;
	private double total;
	@OneToMany(mappedBy="merchant")
	private Collection<Payment> payments;
Описание слайда:
Merchant Entity @Entity public class Merchant { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private double charge; private int period; private double total; @OneToMany(mappedBy="merchant") private Collection<Payment> payments;

Слайд 22





Main Class (1 of 2)
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     MerchantService merchantService = 	context.getBean(MerchantService.class);
     List<Merchant> list = 	merchantService.getSortedByNeedToPay();
     for(Merchant m: list){
	System.out.println("=======================");
	System.out.println(m.getName());
Описание слайда:
Main Class (1 of 2) @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); MerchantService merchantService = context.getBean(MerchantService.class); List<Merchant> list = merchantService.getSortedByNeedToPay(); for(Merchant m: list){ System.out.println("======================="); System.out.println(m.getName());

Слайд 23





Main Class (2 of 2)
	System.out.println("  ");
	Collection<Payment> payments = m.getPayments();
	for (Payment p : payments) {
	      System.out.println(p.toString());
	}
     }
}
Описание слайда:
Main Class (2 of 2) System.out.println(" "); Collection<Payment> payments = m.getPayments(); for (Payment p : payments) { System.out.println(p.toString()); } } }

Слайд 24





Exercise: Read a Merchant’s Payments
See P353MerchantPayments project for the full text
Описание слайда:
Exercise: Read a Merchant’s Payments See P353MerchantPayments project for the full text

Слайд 25





Many-to-Many Mappings
The entity instances can be related to multiple instances of each other
Many-to-many relationships use the @ManyToMany annotation on the corresponding persistent property or field
We must use a third table to associate the two entity types (join table)
Описание слайда:
Many-to-Many Mappings The entity instances can be related to multiple instances of each other Many-to-many relationships use the @ManyToMany annotation on the corresponding persistent property or field We must use a third table to associate the two entity types (join table)

Слайд 26





@JoinTable Annotation
@JoinTable(name=“table name”,
   joinColumns=@JoinColumn(name=“c1Id”),
   inverseJoinColumns=
		@JoinColumn(name=“c2Id”))
Описание слайда:
@JoinTable Annotation @JoinTable(name=“table name”, joinColumns=@JoinColumn(name=“c1Id”), inverseJoinColumns= @JoinColumn(name=“c2Id”))

Слайд 27





Exercise: Read Customer’s Merchants
Create a project to read data about merchants whose goods were bought by a given customer
Описание слайда:
Exercise: Read Customer’s Merchants Create a project to read data about merchants whose goods were bought by a given customer

Слайд 28





Customer Entity (1 of 2)
@Entity
public class Customer {
	@Id
 	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;
	private java.sql.Date maturity;
	private String ccType;
	private String ccNo;
	private String eMail;
	private String address;
	private String name;
Описание слайда:
Customer Entity (1 of 2) @Entity public class Customer { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private java.sql.Date maturity; private String ccType; private String ccNo; private String eMail; private String address; private String name;

Слайд 29





Customer Entity (2 of 2)
@ManyToMany
@JoinTable(name="Payment", joinColumns=@JoinColumn(name="customerId"),
   inverseJoinColumns=@JoinColumn(name="merchantId"))
private Collection<Merchant> merchants;
Описание слайда:
Customer Entity (2 of 2) @ManyToMany @JoinTable(name="Payment", joinColumns=@JoinColumn(name="customerId"), inverseJoinColumns=@JoinColumn(name="merchantId")) private Collection<Merchant> merchants;

Слайд 30





Main Class
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     CustomerService customerService = 	context.getBean(CustomerService.class);
     Customer customer = customerService.findById(1);
     if (customer != null){
     System.out.println(customer.toString());
      Collection<Merchant> merchants = customer.getMerchants();
     for (Merchant m : merchants) {	System.out.println(m.getName());}}}
Описание слайда:
Main Class @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); CustomerService customerService = context.getBean(CustomerService.class); Customer customer = customerService.findById(1); if (customer != null){ System.out.println(customer.toString()); Collection<Merchant> merchants = customer.getMerchants(); for (Merchant m : merchants) { System.out.println(m.getName());}}}

Слайд 31





Exercise: Read Customer’s Merchants
See P354CustAndMerch project for the full text
Описание слайда:
Exercise: Read Customer’s Merchants See P354CustAndMerch project for the full text

Слайд 32





Lazy Relationships
The fetch mode can be specified on any of the four relationship mapping types:
	@ManyToOne(fetch=FetchType.LAZY)
Lazy loading can essential enhance performance
Описание слайда:
Lazy Relationships The fetch mode can be specified on any of the four relationship mapping types: @ManyToOne(fetch=FetchType.LAZY) Lazy loading can essential enhance performance

Слайд 33





One-to-One Mappings
Each entity instance is related to a single instance of another entity
One-to-one relationships use the @OneToOne annotation on the corresponding persistent property or field
Example of One-To-One mapping: Employee and ParkingSpace entities
Описание слайда:
One-to-One Mappings Each entity instance is related to a single instance of another entity One-to-one relationships use the @OneToOne annotation on the corresponding persistent property or field Example of One-To-One mapping: Employee and ParkingSpace entities

Слайд 34





Embedded Objects (1 of 2)
An embedded object is one that is dependent on an entity for its identity
It has no identity of its own, but is merely part of the entity state
The state of the embedded object is stored with the rest of the entity state in the database row, with no distinction between the state in the Java entity and that in its embedded object
Описание слайда:
Embedded Objects (1 of 2) An embedded object is one that is dependent on an entity for its identity It has no identity of its own, but is merely part of the entity state The state of the embedded object is stored with the rest of the entity state in the database row, with no distinction between the state in the Java entity and that in its embedded object

Слайд 35





Embedded Objects (2 of 2)
We can share the same embedded object type with other entities that also have the same internal representation
Example: the home address of the employee (contains Street, City, State, ZipCode fields)
Описание слайда:
Embedded Objects (2 of 2) We can share the same embedded object type with other entities that also have the same internal representation Example: the home address of the employee (contains Street, City, State, ZipCode fields)

Слайд 36





Embedded Type
An embedded type is marked as such by adding the @Embeddable annotation to the class definition
Its fields and properties will be persistable as part of an entity
We might also want to define the access type of the embeddable object so it is accessed the same way regardless of which entity it is embedded in
Описание слайда:
Embedded Type An embedded type is marked as such by adding the @Embeddable annotation to the class definition Its fields and properties will be persistable as part of an entity We might also want to define the access type of the embeddable object so it is accessed the same way regardless of which entity it is embedded in

Слайд 37





Address Embedded Type
@Embeddable 
@Access(AccessType.FIELD) 
public class Address { 
          private String street; 
          private String city; 
          private String state; 
		private String zipCode; 
          // ... 
      }
Описание слайда:
Address Embedded Type @Embeddable @Access(AccessType.FIELD) public class Address { private String street; private String city; private String state; private String zipCode; // ... }

Слайд 38





Using an Embedded Object
@Entity 
public class Employee { 
          @Id 
		 private int id; 
          private String name; 
          private long salary; 
          @Embedded 
		 private Address address; 
          // ... 
}
Описание слайда:
Using an Embedded Object @Entity public class Employee { @Id private int id; private String name; private long salary; @Embedded private Address address; // ... }

Слайд 39





Reuse of Embedded Type
An Address class could be reused in both Employee and Company entities
Описание слайда:
Reuse of Embedded Type An Address class could be reused in both Employee and Company entities

Слайд 40





Database Schema Generation
Automatically generation the tables and database schema for a persistence unit can be done through the "eclipselink.ddl-generation" persistence unit property
The tables and constraints will be generated for all of the classes defined in that persistence unit.
Описание слайда:
Database Schema Generation Automatically generation the tables and database schema for a persistence unit can be done through the "eclipselink.ddl-generation" persistence unit property The tables and constraints will be generated for all of the classes defined in that persistence unit.

Слайд 41





eclipselink.ddl-generation Values
create-tables – if the table already exists then it will not be dropped or replaced, the existing table will be used
drop-and-create-tables - will first drop the existing table, and then create the new table. Note that this will lose all of the data in the tables when they are dropped!!
Описание слайда:
eclipselink.ddl-generation Values create-tables – if the table already exists then it will not be dropped or replaced, the existing table will be used drop-and-create-tables - will first drop the existing table, and then create the new table. Note that this will lose all of the data in the tables when they are dropped!!

Слайд 42





Schema Generation Practice
One of the complaints around schema generation is that you can’t specify everything that you need to be able to finely tune the schema. 
There are too many differences between databases and too many different settings to try to put in options for every database type.
Описание слайда:
Schema Generation Practice One of the complaints around schema generation is that you can’t specify everything that you need to be able to finely tune the schema. There are too many differences between databases and too many different settings to try to put in options for every database type.

Слайд 43





Schema Generation Practice
If every database-tuning option were exposed through JPA then we would end up duplicating the features of Data Definition Language (DDL) in an API
The final schema will typically be tuned by a database administrator or someone with the appropriate level of database experience.
Описание слайда:
Schema Generation Practice If every database-tuning option were exposed through JPA then we would end up duplicating the features of Data Definition Language (DDL) in an API The final schema will typically be tuned by a database administrator or someone with the appropriate level of database experience.



Похожие презентации
Mypresentation.ru
Загрузить презентацию