1 / 20

قسمتهای خوب JPA و Hibernate

قسمتهای خوب JPA و Hibernate. امیر مقیمی مهندس نرم افزار، شرکت Veda، استرالیا. مقدمه. ابزار Hibernate و استاندارد JPA: برای اتصال به پایگاه داده های رابطه ای در جاوا طراحی شده اند بی مقدمه می رویم سر اصل مطلب: تعریف یک Entity عملیات CRUD : Create Retrieve Update Delete. Entity.

ian-young
Download Presentation

قسمتهای خوب JPA و Hibernate

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. قسمتهای خوب JPA و Hibernate امیر مقیمی مهندس نرم افزار، شرکت Veda، استرالیا

  2. مقدمه • ابزار Hibernate و استاندارد JPA: • برای اتصال به پایگاه داده های رابطه ای در جاوا طراحی شده اند • بی مقدمه می رویم سر اصل مطلب: • تعریف یک Entity • عملیات CRUD: Create Retrieve Update Delete

  3. Entity importjavax.persistence.*;@Entitypublicclass Employee { @Idprivatelong id; privateString firstName;privateString lastName; // getters and setters // Never put annotations on getters @Idpubliclong getId() { return id; }}

  4. Create/Update EntityManager em= getEntityManager();Employee employee =new Employee();employee.setFirstName("Bob");employee.setLastName("Dylan"); employee =em.merge(employee); // Never use merge without assignment of return value em.merge(employee); // Never use persist em.persist(employee);

  5. Retrieve/Delete EntityManager em= getEntityManager();Employee employee =em.find(Employee.class, id); em.remove(employee);

  6. Transactions // Never create EntityManager or EntityManagerFactory yourself EntityManager em =createEntityManager(); Employee detached= em.find(Employee.class, id); // Never close EntityManager yourself em.close();EntityManager newEm =createEntityManager(); // Never manage (begin/commit/rollback) transactions using EntityManager newEm.getTransaction().begin();Employee managed = newEm.merge(detached);newEm.getTransaction().commit();

  7. Detached vs Managed • Managed: • A managed object is the one read in the current persistence context (EntityManager/JTA transaction). • The persistence context will track changes to every managed object and maintain its object identity. • If the same object is read again, in the same persistence context, or traversed through another managed object's relationship, the same identical (==) object will be returned. • Calling persist() on a new object will also make it become managed. • Calling merge() on a detached object will return the managed copy of the object. • An object should never be managed by more than one persistence context. • An object will be managed by its persistence context until the persistence context is cleared through clear(), or the object is forced to be detached through detach(). • A removed object will no longer be managed after a flush() or commit(). • On a rollback, all managed objects will become detached. In a JTA managed EntityManager all managed objects will be detached on any JTA commit or rollback. http://en.wikibooks.org/wiki/Java_Persistence/Persisting

  8. Detached vs Managed • Detached: • A detached object is one that is not managed in the current persistence context. • This could be an object read through a different persistence context, or an object that was cloned or serialized. • A new object is also considered detached until persist() is called on it. • An object that was removed and flushed or committed, will become detached. • An object could be considered both managed in the context of one persistence context, and detached in the context of another persistence context. • A managed object should only ever reference other managed objects, and a detached object should only reference other detached objects. • Incorrectly relating managed and detached objects is one of the most common problems users run into in JPA. http://en.wikibooks.org/wiki/Java_Persistence/Persisting

  9. Even 2 more states! http://openjpa.apache.org/builds/2.3.0/apache-openjpa/docs/manual.html

  10. Best Practice • Always assume an object is detached/new unless found/merged in the same method • Make it managed as soon as possible • Only set/add managed objects into each other • Never use Cascade

  11. Relationships @Entitypublicclass Phone { @Idprivatelong id;privateString number; // Never ever use eager!! @ManyToOne @ManyToOne(fetch=FetchType.EAGER) @ManyToOne(fetch=FetchType.LAZY)// @JoinColumn(name="OWNER_ID")private Employee owner;}

  12. Other Relationships @Entitypublicclass Employee { @Idprivatelong id; ...// Never use @OneToMany @OneToManyprivateList<Phone> phones; ...}

  13. Other Relationships @Entitypublicclass Employee { @Idprivatelong id; ...// Never use @ManyToMany @ManyToManyprivateList<Project> projects; .....}

  14. Inheritance @MappedSuperclasspublicabstractclass Project { @Idprivatelong id; @Column(name="NAME")privateString name; ...} @Entity@Table(name="LARGEPROJECT")@AttributeOverride(name="name", column= @Column(name="PROJECT_NAME"))publicclass LargeProject extends Project {privateBigDecimal budget;} @Entity@Table("SMALLPROJECT")publicclass SmallProject extends Project {}

  15. Inheritance • Never use @Inheritance @Entity@Inheritance(strategy=InheritanceType….) • Just use @MappedSuperclass if OO inheritance is required

  16. Querying Query query = em.createQuery("select e from Employee e where e.name = :name");query.setParameter("name", "Bob Dylan"); // Do NOT execute this without a very restrictive where condition List<Employee> employees = query.getResultList(); Query query = em.createQuery("select e from Employee e order by e.birthDate");query.setFirstResult(100);query.setMaxResults(200);// Now it is safe to execute this because we have pagination in place List<Employee> page = query.getResultList();

  17. Advanced Queries // When you need address for all of the queried employees (This is to avoid infamous N+1 queries!) SELECT e FROM Employee e JOINFETCHe.address // Do NOT write it like this (it does not instantiate relationships; so, e.address will not be loaded!): SELECT e, a FROM Employee e, Address a WHERE e.address = a // To include employees with no address (null address field) SELECT e FROM Employee e LEFTOUTERJOINFETCH e.address

  18. موضوعاتی برای آینده • EntityManager/Session management • LazyInitializationException !!! • Transaction management • OneToOne • XToMany alternatives • Bulk update/delete • Hibernate Caching: First/Second/Query level

  19. موضوعاتی برای آینده • Criteria API • Advanced proxy details: • instanceof issues • accessing id for lazy entities • Equals and hashCode for Entity • The good, the bad, the ugly: Native SQL queries • DDL support (schema creation and migration) • Hibernate/JPA alternatives! • Specifically jOOQ

  20. سوالات شما ؟

More Related