1 / 25

Java Persistence Architecture

Java Persistence Architecture. Julio M. Faerman. Mapeamento O-R. P lain O ld J ava O bject. Mapeamento Objeto-Relacional. SGBDR. Convenções. Anotações. XML. Java Persistence Architecture. XML. Anotações. Convenções. Object. JPA javax.persistence. MySQL.

Download Presentation

Java Persistence Architecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Persistence Architecture Julio M. Faerman

  2. Java Persistence Architecture Mapeamento O-R Plain Old Java Object Mapeamento Objeto-Relacional SGBDR Convenções Anotações XML

  3. Java Persistence Architecture Java Persistence Architecture XML Anotações Convenções Object JPA javax.persistence MySQL Hibernate, Toplink, OpenJPA, EclipseLink Convenções Anotações XML

  4. Java Persistence Architecture Java Persistence Architecture

  5. Java Persistence Architecture API

  6. Java Persistence Architecture Ciclo de vida de entidades

  7. Java Persistence Architecture Ciclo de vida de entidades

  8. Java Persistence Architecture META-INF/persistence.xml

  9. Java Persistence Architecture Entidades • @Entity • Construtor sem argumentos public ou protected • Não final • Serializable*

  10. Java Persistence Architecture Chaves Primárias • @Id • @GeneratedValue • GenerationType.IDENTITY • GenerationType.SEQUENCE • GenerationType.TABLE • GenerationType.AUTO • @SequenceGenerator

  11. Java Persistence Architecture PKs Compostas • @EmbeddedID @Embeddable class PkEntidade{ long pk1; long pk2; } @Entity Class Entidade{ @EmbeddedId PkEntidade id; } • @IdClass class PkEntidade{ long pk1; long pk2; } @Entity @IdClass(PkEntidade.class) Class Entidade{ @Id pk1; @Id pk2; }

  12. Java Persistence Architecture Propriedades • Primitivos e Wrappers • Numericos • @Column(scale = 4, precision = 15) • Temporais • @Temporal(TemporalType.TIMESTAMP) • Enumerados • @Enumerated(EnumType.STRING) • @Transient / transient

  13. Java Persistence Architecture Hello World JPA

  14. Java Persistence Architecture Relacionamentos @OneToOne Pessoa Passaporte pessoa.getPassaporte(); Pessoa Passaporte passaporte.getPessoa(); Pessoa Passaporte passaporte.getPessoa(); pessoa.getPassaporte(); • @OneToOne • Dono (owner) da associação • Tabela que vai ter a FK @OneToOne(mappedBy=“passaporte”)

  15. Java Persistence Architecture @OneToMany / @ManyToOne @ManyToOne Departamento dpt = pessoa.getDepartamento(); @OneToMany List<Peca> peacas = carro.getPecas(); @OneToMany( cascade=CascadeType.ALL) O lado many sempre é owner e não declara mappedBy.

  16. Java Persistence Architecture Relacionamentos @ManyToMany Escola Predio N N Set<Escola> escolas = predio.getEscolas(); Set<Predio> predios = escola.getPredios(); - Qualquer lado pode ser owner

  17. Java Persistence Architecture Herança @Inheritance( strategy=InheritanceType.SINGLE_TABLE) @Inheritance( strategy=InheritanceType.JOINED) @Inheritance( strategy=InheritanceType.TABLE_PER_CLASS)

  18. Java Persistence Architecture Controle do esquema do banco • @Table • @SecondaryTables • @Column • @JoinTable • @JoinColumn • @UniqueConstraint

  19. Java Persistence Architecture Consultas JPQL • SELECT select p from PessoaFisica as p where p.passaporte.numero = :numero • UPDATE update Filial f set f.status = ’especial' where f.vendas > 1000000 • DELETE delete from filial f where f.vendas = 0 AND f.funcionarios IS EMPTY

  20. Java Persistence Architecture Consultas Nomeadas @Entity @NamedQueries({ @NamedQuery(name="pf.todas",query="select pf from PessoaFisica pf"), @NamedQuery(name="pf.porPassaporte", query="select p from PessoaFisica p where p.passaporte.numero = :numero")}) public class PessoaFisica extends Pessoa {… Query query = em.createNamedQuery("pf.porPassaporte"); query.setParameter("numero", "123456"); PessoaFisica p = (PessoaFisica) query.getSingleResult();

  21. Java Persistence Architecture Consultas Nativas (SQL) @Entity public class Order { @Id protected int id; protected long quantity; @ManyToOne protected Item item; ... } .. em.createNativeQuery( “select * from Order”,”OrderResults”); @SqlResultSetMapping( name="OrderResults", entities={ @EntityResult( entityClass=Order.class, fields={ @FieldResult(name="id", column="order_id"), @FieldResult(name="quantity", column="order_quantity"), @FieldResult(name="item", column="order_item") } ), @EntityResult( entityClass=Item.class, fields={ @FieldResult(name="id", column="item_id"), @FieldResult(name="name", column="item_name"), })})

  22. Java Persistence Architecture orm.xml (JSR 220, chap. 10) <?xml version="1.0" encoding="UTF-8" ?> <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0"> <description>JPOX JPA tutorial</description> <package>org.jpox.tutorial.jpa</package> <entity class="org.jpox.samples.metadata.store.Product" name="Product"> <table name="JPA_PRODUCTS"/> <attributes> <id name="id"> <generated-value strategy="TABLE"/> </id> <basic name="name"> <column name="PRODUCT_NAME" length="100"/> </basic> <basic name="description"> <column length="255"/> </basic> </attributes> </entity> </entity-mappings>

  23. Java Persistence Architecture hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="events.Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="native"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title"/> </class> </hibernate-mapping> http://www.hibernate.org/hib_docs/reference/en/html/

  24. Java Persistence Architecture Persistencia completa usando JPA

  25. Java Persistence Architecture Dúvidas ?

More Related