├── GS_Hibernate__CRUD ├── pom.xml ├── readme.txt └── src │ └── main │ ├── java │ └── edu │ │ └── javacourse │ │ └── hibernate │ │ ├── City.java │ │ ├── HibernateSimple.java │ │ └── Region.java │ └── resources │ ├── hibernate.cfg.xml │ └── log4j.properties ├── GS_JPA_10_Cascade ├── pom.xml └── src │ └── main │ ├── java │ └── edu │ │ └── javacourse │ │ └── jpa │ │ ├── SimpleExample.java │ │ ├── entity │ │ ├── Author.java │ │ ├── Book.java │ │ ├── City.java │ │ └── Region.java │ │ └── manager │ │ └── RegionManager.java │ └── resources │ └── META-INF │ └── persistence.xml ├── GS_JPA_1_Toplink ├── pom.xml └── src │ └── main │ ├── java │ └── edu │ │ └── javacourse │ │ └── jpa │ │ ├── SimpleExample.java │ │ ├── entity │ │ ├── City.java │ │ └── Region.java │ │ └── manager │ │ └── RegionManager.java │ └── resources │ └── META-INF │ └── persistence.xml ├── GS_JPA_2_EclipseLink ├── pom.xml └── src │ └── main │ ├── java │ └── edu │ │ └── javacourse │ │ └── jpa │ │ ├── SimpleExample.java │ │ ├── entity │ │ ├── City.java │ │ └── Region.java │ │ └── manager │ │ └── RegionManager.java │ └── resources │ └── META-INF │ └── persistence.xml ├── GS_JPA_3_OpenJpa ├── pom.xml └── src │ └── main │ ├── java │ └── edu │ │ └── javacourse │ │ └── jpa │ │ ├── SimpleExample.java │ │ ├── entity │ │ ├── City.java │ │ └── Region.java │ │ └── manager │ │ └── RegionManager.java │ └── resources │ └── META-INF │ └── persistence.xml ├── GS_JPA_4_Hibernate ├── pom.xml └── src │ └── main │ ├── java │ └── edu │ │ └── javacourse │ │ └── jpa │ │ ├── SimpleExample.java │ │ ├── entity │ │ ├── City.java │ │ └── Region.java │ │ └── manager │ │ └── RegionManager.java │ └── resources │ └── META-INF │ └── persistence.xml ├── GS_JPA_5_CRUD ├── pom.xml └── src │ └── main │ ├── java │ └── edu │ │ └── javacourse │ │ └── jpa │ │ ├── SimpleExample.java │ │ ├── entity │ │ ├── City.java │ │ └── Region.java │ │ └── manager │ │ └── RegionManager.java │ └── resources │ └── META-INF │ └── persistence.xml ├── GS_JPA_6_JPQL ├── pom.xml ├── readme.txt └── src │ └── main │ ├── java │ └── edu │ │ └── javacourse │ │ └── jpa │ │ ├── SimpleExample.java │ │ ├── entity │ │ ├── Author.java │ │ ├── Book.java │ │ ├── City.java │ │ └── Region.java │ │ └── manager │ │ └── RegionManager.java │ └── resources │ └── META-INF │ └── persistence.xml ├── GS_JPA_7_NativeQuery ├── pom.xml └── src │ └── main │ ├── java │ └── edu │ │ └── javacourse │ │ └── jpa │ │ ├── SimpleExample.java │ │ ├── entity │ │ ├── Author.java │ │ ├── Book.java │ │ ├── City.java │ │ └── Region.java │ │ └── manager │ │ └── RegionManager.java │ └── resources │ └── META-INF │ └── persistence.xml ├── GS_JPA_8_NamedQuery ├── pom.xml ├── readme.txt └── src │ └── main │ ├── java │ └── edu │ │ └── javacourse │ │ └── jpa │ │ ├── SimpleExample.java │ │ ├── entity │ │ ├── Author.java │ │ ├── Book.java │ │ ├── City.java │ │ └── Region.java │ │ └── manager │ │ └── RegionManager.java │ └── resources │ └── META-INF │ └── persistence.xml └── GS_JPA_9_Criteria ├── pom.xml ├── readme.txt ├── readme.xml └── src └── main ├── java └── edu │ └── javacourse │ └── jpa │ ├── SimpleExample.java │ ├── entity │ ├── Author.java │ ├── Book.java │ ├── City.java │ └── Region.java │ └── manager │ └── RegionManager.java └── resources └── META-INF └── persistence.xml /GS_Hibernate__CRUD/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | hibernate-crud 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | org.hibernate 19 | hibernate-core 20 | 3.6.9.Final 21 | 22 | 23 | 24 | mysql 25 | mysql-connector-java 26 | 5.1.18 27 | 28 | 29 | javax.persistence 30 | persistence-api 31 | 1.0.2 32 | provided 33 | 34 | 35 | org.hibernate 36 | hibernate-annotations 37 | 3.5.6-Final 38 | 39 | 40 | 41 | 42 | javassist 43 | javassist 44 | 3.12.1.GA 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /GS_Hibernate__CRUD/readme.txt: -------------------------------------------------------------------------------- 1 | openSession vs CurrentSession 2 | 3 | When you create a hibernate session using any of the sessionFactory.openSession(...) methods the session factory will 'bind' the session to the current context. The default context is 'thread' which means the sesion factory will bind the session to the thread from which openSession(...) is called. 4 | This is useful because you can later call sessionFactory.getCurrentSession() which will return the session that is bound to the currently running thread. 5 | 6 | http://stackoverflow.com/questions/8046662/hibernate-opensession-vs-getcurrentsession 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | get vs load 15 | 16 | 1. session.load() 17 | It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object. 18 | If no row found , it will throws an ObjectNotFoundException. 19 | Get method never returns a proxy, it either returns null or fully initialized Object, while load() method may return proxy, which is the object with ID but without initializing other properties, which is lazily initialized. If you are just using returned object for creating relationship and only need Id then load() is the way to go. 20 | Read more: http://javarevisited.blogspot.com/2012/07/hibernate-get-and-load-difference-interview-question.html#ixzz2WzTySJvX 21 | 22 | 2. session.get() 23 | It always hit the database and return the real object, an object that represent the database row, not proxy. 24 | If no row found , it return null. 25 | -------------------------------------------------------------------------------- /GS_Hibernate__CRUD/src/main/java/edu/javacourse/hibernate/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.hibernate; 2 | 3 | import javax.persistence.CascadeType; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.FetchType; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.JoinColumn; 11 | import javax.persistence.ManyToOne; 12 | import javax.persistence.Table; 13 | 14 | /** 15 | * 16 | * @author Demo 17 | */ 18 | @Entity 19 | @Table(name = "jc_city") 20 | public class City { 21 | 22 | @Id 23 | @GeneratedValue(strategy = GenerationType.IDENTITY) 24 | @Column(name = "city_id") 25 | private Integer cityId; 26 | 27 | @Column(name = "city_name") 28 | private String cityName; 29 | 30 | @ManyToOne( fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}) 31 | @JoinColumn(name = "region_id") // 32 | private Region region; 33 | 34 | public City() { 35 | } 36 | 37 | 38 | public City(Integer cityId, String cityName) { 39 | this.cityId = cityId; 40 | this.cityName = cityName; 41 | } 42 | 43 | 44 | public City(String cityName) { 45 | this.cityName = cityName; 46 | } 47 | 48 | public City(String name, Region region) { 49 | this.cityName = name; 50 | this.region = region; 51 | } 52 | 53 | public Integer getCityId() { 54 | return cityId; 55 | } 56 | 57 | public void setCityId(Integer cityId) { 58 | this.cityId = cityId; 59 | } 60 | 61 | public String getCityName() { 62 | return cityName; 63 | } 64 | 65 | public void setCityName(String cityName) { 66 | this.cityName = cityName; 67 | } 68 | 69 | public Region getRegion() { 70 | return region; 71 | } 72 | 73 | public void setRegion(Region region) { 74 | this.region = region; 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return "City{" + "cityId=" + cityId + ", name=" + cityName + '}'; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /GS_Hibernate__CRUD/src/main/java/edu/javacourse/hibernate/HibernateSimple.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.util.Iterator; 5 | import java.util.LinkedList; 6 | import java.util.List; 7 | 8 | import org.hibernate.EmptyInterceptor; 9 | import org.hibernate.Session; 10 | import org.hibernate.SessionFactory; 11 | import org.hibernate.Transaction; 12 | import org.hibernate.cfg.Configuration; 13 | import org.hibernate.type.Type; 14 | 15 | /** 16 | * Простой пример для interceptor 17 | * 18 | * @author ASaburov 19 | * @author Georgy Gobozov 20 | */ 21 | public class HibernateSimple { 22 | 23 | static HibernateSimple hs = new HibernateSimple(); 24 | 25 | static Serializable id = null; 26 | 27 | public static void main(String[] args) { 28 | create(); 29 | 30 | } 31 | 32 | private static void create() { 33 | System.out.println("==============CREATE================="); 34 | // Create hibernate session 35 | Session session = hs.getSessionFactory().openSession(); 36 | // begin transaction 37 | session.beginTransaction(); 38 | // create object 39 | Region region = new Region("Len Oblast!"); 40 | 41 | 42 | 43 | //save object 44 | List cities = new LinkedList(); 45 | cities.add(new City(123, "Vyborg")); 46 | cities.add(new City(124, "Priozersk")); 47 | region.setCityList(cities); 48 | 49 | session.save(region); 50 | session.getTransaction().commit(); 51 | session.close(); 52 | } 53 | 54 | 55 | 56 | 57 | private static void update() { 58 | System.out.println("==============UPDATE================="); 59 | Session session = hs.getSessionFactory().openSession(); 60 | session.beginTransaction(); 61 | City city = (City) session.load(City.class, id); 62 | city.setCityName("nefteugansk"); 63 | session.saveOrUpdate(city); 64 | System.out.println("city = " + city); 65 | 66 | session.getTransaction().commit(); 67 | session.close(); 68 | 69 | } 70 | 71 | 72 | private static void delete() { 73 | System.out.println("==============DELETE================="); 74 | Session session = hs.getSessionFactory().openSession(); 75 | session.beginTransaction(); 76 | City city = (City) session.load(City.class, id); 77 | 78 | session.delete(city); 79 | 80 | session.getTransaction().commit(); 81 | session.close(); 82 | 83 | } 84 | 85 | 86 | 87 | private SessionFactory getSessionFactory() { 88 | Configuration cfg = new Configuration(); 89 | return cfg.configure().buildSessionFactory(); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /GS_Hibernate__CRUD/src/main/java/edu/javacourse/hibernate/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.hibernate; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.*; 6 | 7 | @Entity 8 | @Table(name = "jc_region") 9 | public class Region implements Serializable { 10 | 11 | @Id 12 | @GeneratedValue(strategy = GenerationType.IDENTITY) 13 | @Column(name = "region_id") 14 | private Long regionId; 15 | 16 | @Column(name = "region_name", nullable = true) 17 | private String regionName; 18 | 19 | @OneToMany( mappedBy = "region", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) 20 | private List cityList; 21 | 22 | public List getCityList() { 23 | return cityList; 24 | } 25 | 26 | public void setCityList(List cityList) { 27 | this.cityList = cityList; 28 | } 29 | 30 | public Region() { 31 | } 32 | 33 | public Region(String regionName) { 34 | this.regionName = regionName; 35 | } 36 | 37 | public Long getRegionId() { 38 | return regionId; 39 | } 40 | 41 | public void setRegionId(Long regionId) { 42 | this.regionId = regionId; 43 | } 44 | 45 | public String getRegionName() { 46 | return regionName; 47 | } 48 | 49 | public void setRegionName(String regionName) { 50 | this.regionName = regionName; 51 | } 52 | 53 | @Override 54 | public String toString() { 55 | return regionId + ":" + regionName; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /GS_Hibernate__CRUD/src/main/resources/hibernate.cfg.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | com.mysql.jdbc.Driver 12 | jdbc:mysql://127.0.0.1:3306/java_backend 13 | root 14 | root 15 | 16 | 17 | 1 18 | 19 | 20 | org.hibernate.dialect.MySQLInnoDBDialect 21 | 22 | 23 | thread 24 | 25 | 26 | org.hibernate.cache.NoCacheProvider 27 | 28 | 29 | true 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /GS_Hibernate__CRUD/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=ERROR, console 2 | log4j.logger.org.hibernate=ERROR 3 | 4 | # Console appender 5 | log4j.appender.console=org.apache.log4j.ConsoleAppender 6 | log4j.appender.console.layout=org.apache.log4j.PatternLayout 7 | log4j.appender.console.layout.ConversionPattern=%5p | %m%n 8 | 9 | -------------------------------------------------------------------------------- /GS_JPA_10_Cascade/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | jpa-cascade 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 5.1.18 20 | 21 | 22 | 23 | org.eclipse.persistence 24 | org.eclipse.persistence.jpa 25 | 2.5.0 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /GS_JPA_10_Cascade/src/main/java/edu/javacourse/jpa/SimpleExample.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa; 2 | 3 | import edu.javacourse.jpa.entity.Author; 4 | import edu.javacourse.jpa.entity.Book; 5 | import edu.javacourse.jpa.entity.City; 6 | import edu.javacourse.jpa.entity.Region; 7 | import edu.javacourse.jpa.manager.RegionManager; 8 | 9 | import javax.persistence.EntityManager; 10 | import javax.persistence.Query; 11 | import javax.persistence.TypedQuery; 12 | import javax.persistence.criteria.*; 13 | import java.util.Collection; 14 | import java.util.List; 15 | 16 | public class SimpleExample { 17 | 18 | public static void main(String[] args) { 19 | RegionManager rm = new RegionManager(); 20 | rm.init(); 21 | EntityManager em = rm.getEntityManager(); 22 | 23 | //test1(em); 24 | test2(em); 25 | 26 | 27 | 28 | } 29 | 30 | private static void test2(EntityManager em) { 31 | // create city and region 32 | em.getTransaction().begin(); 33 | // region should have @OneToMany( mappedBy = "region", fetch = FetchType.LAZY) 34 | Region region = new Region("New region 3.02.2015"); 35 | em.persist(region); 36 | 37 | 38 | City city = new City("New city 03.02.2015"); 39 | city.setRegion(region); 40 | em.persist(city); 41 | em.getTransaction().commit(); 42 | 43 | int id = region.getRegionId(); 44 | System.out.println("id = " + id); 45 | 46 | // take a look in database region and city be sure that new values exists 47 | // take a look on new city region_id 48 | try { 49 | Thread.sleep(15000); 50 | } catch (InterruptedException e) { 51 | e.printStackTrace(); 52 | } 53 | 54 | // take a look on new city region_id again, must change 55 | 56 | em.getTransaction().begin(); 57 | region.setRegionId(region.getRegionId() + 1000); 58 | System.out.println("new id = " + region.getRegionId()); 59 | em.merge(region); 60 | em.getTransaction().commit(); 61 | // then change to @OneToMany( mappedBy = "region", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 62 | } 63 | 64 | private static void test1(EntityManager em) { 65 | // create city and region 66 | em.getTransaction().begin(); 67 | // region should have @OneToMany( mappedBy = "region", fetch = FetchType.LAZY) 68 | Region region = new Region("Test region 2015"); 69 | em.persist(region); 70 | City city = new City("Test city 2015"); 71 | city.setRegion(region); 72 | em.persist(city); 73 | em.getTransaction().commit(); 74 | 75 | // take a look in database region and city be sure that new values exists 76 | 77 | try { 78 | Thread.sleep(15000); 79 | } catch (InterruptedException e) { 80 | e.printStackTrace(); 81 | } 82 | 83 | //try to delete region 84 | em.getTransaction().begin(); 85 | em.remove(region); 86 | em.getTransaction().commit(); 87 | // then change to @OneToMany( mappedBy = "region", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 88 | } 89 | 90 | 91 | private static void test3(EntityManager em, CriteriaBuilder cb) { 92 | CriteriaQuery criteriaQuery = cb.createQuery(Region.class); 93 | Root root = criteriaQuery.from(Region.class); 94 | criteriaQuery.select(root); 95 | 96 | 97 | Expression> cities = root.get("cityList"); 98 | 99 | criteriaQuery.where(cb.greaterThan(cb.size(cities), 1)); 100 | 101 | // create query via EM 102 | Query query = em.createQuery(criteriaQuery); 103 | List regions = query.getResultList(); 104 | printRegions(regions); 105 | } 106 | 107 | private static void test2(EntityManager em, CriteriaBuilder cb) { 108 | 109 | CriteriaQuery criteriaQuery = cb.createQuery(Book.class); 110 | Root root = criteriaQuery.from(Book.class); 111 | criteriaQuery.select(root); 112 | criteriaQuery.where(cb.equal(root.get("bookId"), 1)); 113 | TypedQuery q = em.createQuery(criteriaQuery); 114 | List books = q.getResultList(); 115 | printBooks(books); 116 | } 117 | 118 | private static void test1(EntityManager em, CriteriaBuilder cb) { 119 | CriteriaQuery criteriaQuery = cb.createQuery(Region.class); 120 | Root root = criteriaQuery.from(Region.class); 121 | criteriaQuery.select(root); 122 | TypedQuery q = em.createQuery(criteriaQuery); 123 | List allRegions = q.getResultList(); 124 | printRegions(allRegions); 125 | } 126 | 127 | private static void printBooks(List books) { 128 | for (Book book : books) { 129 | System.out.println(book.getBookName()); 130 | } 131 | } 132 | 133 | private static void printAuthors(List authors) { 134 | for (Author author : authors) { 135 | System.out.println(author.getAuthorName()); 136 | } 137 | System.out.println("============================================"); 138 | } 139 | 140 | private static void printCities(List cities) { 141 | for (City city : cities) { 142 | System.out.println(city.getRegion().getRegionName() + " " + city.getCityName()); 143 | } 144 | System.out.println("============================================"); 145 | } 146 | 147 | private static void printRegions(List regions) { 148 | for (Region region : regions) { 149 | System.out.println(region.getRegionName()); 150 | } 151 | System.out.println("============================================"); 152 | } 153 | 154 | 155 | } 156 | -------------------------------------------------------------------------------- /GS_JPA_10_Cascade/src/main/java/edu/javacourse/jpa/entity/Author.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import javax.persistence.*; 4 | import java.io.Serializable; 5 | import java.util.HashSet; 6 | import java.util.Set; 7 | 8 | @Entity 9 | @Table(name = "jc_author") 10 | @NamedQueries({ 11 | @NamedQuery(name = "Author.ById", query = "select a from Author as a where a.authorId =:id") 12 | }) 13 | public class Author implements Serializable { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | @Column(name = "author_id") 18 | private Long authorId; 19 | @Column(name = "author_name") 20 | private String authorName; 21 | 22 | @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY) 23 | @JoinTable(name = "jc_book_author", 24 | joinColumns = @JoinColumn(name = "author_id"), 25 | inverseJoinColumns = @JoinColumn(name = "book_id")) 26 | @OrderBy(value="bookName") 27 | private Set bookList; 28 | 29 | public Long getAuthorId() { 30 | return authorId; 31 | } 32 | 33 | public void setAuthorId(Long authorId) { 34 | this.authorId = authorId; 35 | } 36 | 37 | public String getAuthorName() { 38 | return authorName; 39 | } 40 | 41 | public void setAuthorName(String authorName) { 42 | this.authorName = authorName; 43 | } 44 | 45 | public Set getBookList() { 46 | return bookList; 47 | } 48 | 49 | public void setBookList(Set bookList) { 50 | this.bookList = bookList; 51 | } 52 | 53 | public void addBook(Book book) { 54 | if (bookList == null) { 55 | bookList = new HashSet(); 56 | } 57 | bookList.add(book); 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "Author{" + "authorId=" + authorId + ", authorName=" + authorName + '}'; 63 | } 64 | 65 | @Override 66 | public boolean equals(Object obj) { 67 | if (obj == null) { 68 | return false; 69 | } 70 | if (getClass() != obj.getClass()) { 71 | return false; 72 | } 73 | final Author other = (Author) obj; 74 | if (this.authorId != other.authorId && (this.authorId == null || !this.authorId.equals(other.authorId))) { 75 | return false; 76 | } 77 | return true; 78 | } 79 | 80 | @Override 81 | public int hashCode() { 82 | int hash = 7; 83 | hash = 71 * hash + (this.authorId != null ? this.authorId.hashCode() : 0); 84 | return hash; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /GS_JPA_10_Cascade/src/main/java/edu/javacourse/jpa/entity/Book.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | import javax.persistence.*; 7 | 8 | @Entity 9 | @Table(name = "jc_book") 10 | @NamedQueries({ 11 | @NamedQuery(name = "Book.ByName", query = "select b from Book as b where b.bookName =:name"), 12 | @NamedQuery(name = "Book.ByAuthor", query = "select b from Book as b, Author a where b member of a.bookList and a.authorName =:Author") 13 | }) 14 | public class Book implements Serializable { 15 | 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.IDENTITY) 18 | @Column(name = "book_id") 19 | private Long bookId; 20 | @Column(name = "book_name") 21 | private String bookName; 22 | 23 | @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY) 24 | @JoinTable(name = "jc_book_author", 25 | joinColumns = @JoinColumn(name = "book_id"), 26 | inverseJoinColumns = @JoinColumn(name = "author_id")) 27 | @OrderBy(value="authorName") 28 | private Set authorList; 29 | 30 | public Set getAuthorList() { 31 | return authorList; 32 | } 33 | 34 | public void setAuthorList(Set authorList) { 35 | this.authorList = authorList; 36 | } 37 | 38 | public void addAuthor(Author author) { 39 | if (authorList == null) { 40 | authorList = new HashSet(); 41 | } 42 | authorList.add(author); 43 | } 44 | 45 | public Long getBookId() { 46 | return bookId; 47 | } 48 | 49 | public void setBookId(Long bookId) { 50 | this.bookId = bookId; 51 | } 52 | 53 | public String getBookName() { 54 | return bookName; 55 | } 56 | 57 | public void setBookName(String bookName) { 58 | this.bookName = bookName; 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | return "Book{" + "bookId=" + bookId + ", bookName=" + bookName + '}'; 64 | } 65 | 66 | @Override 67 | public boolean equals(Object obj) { 68 | if (obj == null) { 69 | return false; 70 | } 71 | if (getClass() != obj.getClass()) { 72 | return false; 73 | } 74 | final Book other = (Book) obj; 75 | if (this.bookId != other.bookId && (this.bookId == null || !this.bookId.equals(other.bookId))) { 76 | return false; 77 | } 78 | return true; 79 | } 80 | 81 | @Override 82 | public int hashCode() { 83 | int hash = 7; 84 | hash = 71 * hash + (this.bookId != null ? this.bookId.hashCode() : 0); 85 | return hash; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /GS_JPA_10_Cascade/src/main/java/edu/javacourse/jpa/entity/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import javax.persistence.*; 5 | 6 | @Entity 7 | @Table(name = "jc_city") 8 | public class City implements Serializable { 9 | 10 | @Id 11 | @GeneratedValue(strategy = GenerationType.IDENTITY) 12 | @Column(name = "city_id") 13 | private Integer cityId; 14 | @Column(name = "city_name") 15 | private String cityName; 16 | 17 | @JoinColumn(name = "region_id", referencedColumnName = "region_id") 18 | @ManyToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.LAZY) 19 | private Region region; 20 | 21 | public City() { 22 | } 23 | 24 | public City(String cityName) { 25 | this.cityName = cityName; 26 | } 27 | 28 | public City(Integer cityId) { 29 | this.cityId = cityId; 30 | } 31 | 32 | public City(Integer cityId, String cityName) { 33 | this.cityId = cityId; 34 | this.cityName = cityName; 35 | } 36 | 37 | public Integer getCityId() { 38 | return cityId; 39 | } 40 | 41 | public void setCityId(Integer cityId) { 42 | this.cityId = cityId; 43 | } 44 | 45 | public String getCityName() { 46 | return cityName; 47 | } 48 | 49 | public void setCityName(String cityName) { 50 | this.cityName = cityName; 51 | } 52 | 53 | public Region getRegion() { 54 | return region; 55 | } 56 | 57 | public void setRegion(Region region) { 58 | this.region = region; 59 | } 60 | 61 | @Override 62 | public int hashCode() { 63 | int hash = 0; 64 | hash += (cityId != null ? cityId.hashCode() : 0); 65 | return hash; 66 | } 67 | 68 | @Override 69 | public boolean equals(Object object) { 70 | // TODO: Warning - this method won't work in the case the id fields are not set 71 | if (!(object instanceof City)) { 72 | return false; 73 | } 74 | City other = (City) object; 75 | if ((this.cityId == null && other.cityId != null) || (this.cityId != null && !this.cityId.equals(other.cityId))) { 76 | return false; 77 | } 78 | return true; 79 | } 80 | 81 | @Override 82 | public String toString() { 83 | return "edu.javacourse.jpa.entity.City[ cityId=" + cityId + " ]"; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /GS_JPA_10_Cascade/src/main/java/edu/javacourse/jpa/entity/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.*; 6 | 7 | @Entity 8 | @Table(name = "jc_region") 9 | @NamedQueries({ 10 | @NamedQuery(name = "Region.DelebeById", query = "delete from Region r where r.regionId =:id"), 11 | @NamedQuery(name = "Region.Count", query = "select count (r) from Region r") 12 | }) 13 | public class Region implements Serializable { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | @Column(name = "region_id") 18 | private Integer regionId; 19 | @Column(name = "region_name") 20 | private String regionName; 21 | 22 | //@OneToMany( mappedBy = "region", fetch = FetchType.LAZY) 23 | @OneToMany( mappedBy = "region", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) 24 | private List cityList; 25 | 26 | public Region() { 27 | } 28 | 29 | public Region(Integer regionId) { 30 | this.regionId = regionId; 31 | } 32 | 33 | public Region(String regionName) { 34 | this.regionName = regionName; 35 | } 36 | 37 | public Region(Integer regionId, String regionName) { 38 | this.regionId = regionId; 39 | this.regionName = regionName; 40 | } 41 | 42 | public Integer getRegionId() { 43 | return regionId; 44 | } 45 | 46 | public void setRegionId(Integer regionId) { 47 | this.regionId = regionId; 48 | } 49 | 50 | public String getRegionName() { 51 | return regionName; 52 | } 53 | 54 | public void setRegionName(String regionName) { 55 | this.regionName = regionName; 56 | } 57 | 58 | public List getCityList() { 59 | return cityList; 60 | } 61 | 62 | public void setCityList(List cityList) { 63 | this.cityList = cityList; 64 | } 65 | 66 | @Override 67 | public int hashCode() { 68 | int hash = 0; 69 | hash += (regionId != null ? regionId.hashCode() : 0); 70 | return hash; 71 | } 72 | 73 | @Override 74 | public boolean equals(Object object) { 75 | // TODO: Warning - this method won't work in the case the id fields are not set 76 | if (!(object instanceof Region)) { 77 | return false; 78 | } 79 | Region other = (Region) object; 80 | if ((this.regionId == null && other.regionId != null) || (this.regionId != null && !this.regionId.equals(other.regionId))) { 81 | return false; 82 | } 83 | return true; 84 | } 85 | 86 | @Override 87 | public String toString() { 88 | return "Region{" + 89 | "regionId=" + regionId + 90 | ", regionName='" + regionName + '\'' + 91 | '}'; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /GS_JPA_10_Cascade/src/main/java/edu/javacourse/jpa/manager/RegionManager.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.manager; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import java.util.List; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.EntityManagerFactory; 7 | import javax.persistence.Persistence; 8 | import javax.persistence.criteria.CriteriaBuilder; 9 | import javax.persistence.criteria.CriteriaQuery; 10 | import javax.persistence.criteria.Root; 11 | 12 | public class RegionManager { 13 | 14 | private EntityManagerFactory entityManagerFactory; 15 | private EntityManager entityManager; 16 | 17 | public void init() { 18 | entityManagerFactory = Persistence.createEntityManagerFactory("unitEclipseLink", new java.util.HashMap()); 19 | System.out.println(entityManagerFactory.getClass().getSimpleName()); 20 | entityManager = entityManagerFactory.createEntityManager(); 21 | System.out.println(entityManager.getClass().getSimpleName()); 22 | } 23 | 24 | public List getRegionList() { 25 | return entityManager.createQuery("select r from Region r").getResultList(); 26 | } 27 | 28 | public EntityManager getEntityManager() { 29 | return entityManager; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /GS_JPA_10_Cascade/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | org.eclipse.persistence.jpa.PersistenceProvider 7 | edu.javacourse.jpa.entity.City 8 | edu.javacourse.jpa.entity.Region 9 | edu.javacourse.jpa.entity.Author 10 | edu.javacourse.jpa.entity.Book 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /GS_JPA_1_Toplink/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | jpa-toplink 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 5.1.18 20 | 21 | 22 | 23 | toplink.essentials 24 | toplink-essentials 25 | 2.1-60 26 | 27 | 28 | 29 | javax.persistence 30 | persistence-api 31 | 1.0.2 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /GS_JPA_1_Toplink/src/main/java/edu/javacourse/jpa/SimpleExample.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import edu.javacourse.jpa.manager.RegionManager; 5 | import java.util.List; 6 | 7 | public class SimpleExample { 8 | 9 | public static void main(String[] args) { 10 | RegionManager rm = new RegionManager(); 11 | rm.init(); 12 | firstSelect(rm); 13 | } 14 | 15 | private static void firstSelect(RegionManager rm) { 16 | System.out.println("First Select ===>"); 17 | List result = rm.getRegionList(); 18 | for(Region r : result) { 19 | System.out.println(r); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /GS_JPA_1_Toplink/src/main/java/edu/javacourse/jpa/entity/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import javax.persistence.Basic; 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.FetchType; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.JoinColumn; 12 | import javax.persistence.ManyToOne; 13 | import javax.persistence.Table; 14 | 15 | @Entity 16 | @Table(name = "jc_city") 17 | public class City implements Serializable { 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | @Column(name = "city_id") 22 | private Integer cityId; 23 | 24 | @Column(name = "city_name") 25 | private String cityName; 26 | 27 | @JoinColumn(name = "region_id", referencedColumnName = "region_id") 28 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 29 | private Region regionId; 30 | 31 | public City() { 32 | } 33 | 34 | public City(Integer cityId) { 35 | this.cityId = cityId; 36 | } 37 | 38 | public City(Integer cityId, String cityName) { 39 | this.cityId = cityId; 40 | this.cityName = cityName; 41 | } 42 | 43 | public Integer getCityId() { 44 | return cityId; 45 | } 46 | 47 | public void setCityId(Integer cityId) { 48 | this.cityId = cityId; 49 | } 50 | 51 | public String getCityName() { 52 | return cityName; 53 | } 54 | 55 | public void setCityName(String cityName) { 56 | this.cityName = cityName; 57 | } 58 | 59 | public Region getRegionId() { 60 | return regionId; 61 | } 62 | 63 | public void setRegionId(Region regionId) { 64 | this.regionId = regionId; 65 | } 66 | 67 | @Override 68 | public int hashCode() { 69 | int hash = 0; 70 | hash += (cityId != null ? cityId.hashCode() : 0); 71 | return hash; 72 | } 73 | 74 | @Override 75 | public boolean equals(Object object) { 76 | // TODO: Warning - this method won't work in the case the id fields are not set 77 | if (!(object instanceof City)) { 78 | return false; 79 | } 80 | City other = (City) object; 81 | if ((this.cityId == null && other.cityId != null) || (this.cityId != null && !this.cityId.equals(other.cityId))) { 82 | return false; 83 | } 84 | return true; 85 | } 86 | 87 | @Override 88 | public String toString() { 89 | return "edu.javacourse.jpa.entity.City[ cityId=" + cityId + " ]"; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /GS_JPA_1_Toplink/src/main/java/edu/javacourse/jpa/entity/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.Basic; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | 16 | @Entity 17 | @Table(name = "jc_region") 18 | public class Region implements Serializable { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | @Column(name = "region_id") 23 | private Integer regionId; 24 | 25 | @Column(name = "region_name") 26 | private String regionName; 27 | 28 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "regionId", fetch = FetchType.LAZY) 29 | private List cityList; 30 | 31 | public Region() { 32 | } 33 | 34 | public Region(Integer regionId) { 35 | this.regionId = regionId; 36 | } 37 | 38 | public Region(Integer regionId, String regionName) { 39 | this.regionId = regionId; 40 | this.regionName = regionName; 41 | } 42 | 43 | public Integer getRegionId() { 44 | return regionId; 45 | } 46 | 47 | public void setRegionId(Integer regionId) { 48 | this.regionId = regionId; 49 | } 50 | 51 | public String getRegionName() { 52 | return regionName; 53 | } 54 | 55 | public void setRegionName(String regionName) { 56 | this.regionName = regionName; 57 | } 58 | 59 | public List getCityList() { 60 | return cityList; 61 | } 62 | 63 | public void setCityList(List cityList) { 64 | this.cityList = cityList; 65 | } 66 | 67 | @Override 68 | public int hashCode() { 69 | int hash = 0; 70 | hash += (regionId != null ? regionId.hashCode() : 0); 71 | return hash; 72 | } 73 | 74 | @Override 75 | public boolean equals(Object object) { 76 | // TODO: Warning - this method won't work in the case the id fields are not set 77 | if (!(object instanceof Region)) { 78 | return false; 79 | } 80 | Region other = (Region) object; 81 | if ((this.regionId == null && other.regionId != null) || (this.regionId != null && !this.regionId.equals(other.regionId))) { 82 | return false; 83 | } 84 | return true; 85 | } 86 | 87 | @Override 88 | public String toString() { 89 | return "edu.javacourse.jpa.entity.JcRegion[ regionId=" + regionId + " " + regionName +"]"; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /GS_JPA_1_Toplink/src/main/java/edu/javacourse/jpa/manager/RegionManager.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.manager; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import java.util.List; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.EntityManagerFactory; 7 | import javax.persistence.Persistence; 8 | 9 | public class RegionManager { 10 | 11 | private EntityManagerFactory entityManagerFactory; 12 | private EntityManager entityManager; 13 | 14 | public void init() { 15 | entityManagerFactory = Persistence.createEntityManagerFactory("unitTopLink", new java.util.HashMap()); 16 | System.out.println(entityManagerFactory.getClass().getSimpleName()); 17 | entityManager = entityManagerFactory.createEntityManager(); 18 | System.out.println(entityManager.getClass().getName()); 19 | } 20 | 21 | public List getRegionList() { 22 | return entityManager.createQuery("select r from Region r").getResultList(); 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /GS_JPA_1_Toplink/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | oracle.toplink.essentials.PersistenceProvider 10 | 11 | edu.javacourse.jpa.entity.City 12 | edu.javacourse.jpa.entity.Region 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /GS_JPA_2_EclipseLink/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | jpa-eclipselink 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 5.1.18 20 | 21 | 22 | 23 | org.eclipse.persistence 24 | org.eclipse.persistence.jpa 25 | 2.5.0 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /GS_JPA_2_EclipseLink/src/main/java/edu/javacourse/jpa/SimpleExample.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import edu.javacourse.jpa.manager.RegionManager; 5 | import java.util.List; 6 | 7 | public class SimpleExample { 8 | 9 | public static void main(String[] args) { 10 | RegionManager rm = new RegionManager(); 11 | rm.init(); 12 | firstSelect(rm); 13 | secondSelect(rm); 14 | } 15 | 16 | private static void firstSelect(RegionManager rm) { 17 | System.out.println("First Select ===>"); 18 | List result = rm.getRegionList(); 19 | for(Region r : result) { 20 | System.out.println(r); 21 | } 22 | } 23 | private static void secondSelect(RegionManager rm) { 24 | System.out.println("Second Select ===>"); 25 | List result = rm.getRegionList2(); 26 | for(Region r : result) { 27 | System.out.println(r); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /GS_JPA_2_EclipseLink/src/main/java/edu/javacourse/jpa/entity/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import javax.persistence.Basic; 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.FetchType; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.JoinColumn; 12 | import javax.persistence.ManyToOne; 13 | import javax.persistence.Table; 14 | 15 | @Entity 16 | @Table(name = "jc_city") 17 | public class City implements Serializable { 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | @Column(name = "city_id") 22 | private Integer cityId; 23 | @Column(name = "city_name") 24 | private String cityName; 25 | @JoinColumn(name = "region_id", referencedColumnName = "region_id") 26 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 27 | private Region regionId; 28 | 29 | public City() { 30 | } 31 | 32 | public City(Integer cityId) { 33 | this.cityId = cityId; 34 | } 35 | 36 | public City(Integer cityId, String cityName) { 37 | this.cityId = cityId; 38 | this.cityName = cityName; 39 | } 40 | 41 | public Integer getCityId() { 42 | return cityId; 43 | } 44 | 45 | public void setCityId(Integer cityId) { 46 | this.cityId = cityId; 47 | } 48 | 49 | public String getCityName() { 50 | return cityName; 51 | } 52 | 53 | public void setCityName(String cityName) { 54 | this.cityName = cityName; 55 | } 56 | 57 | public Region getRegionId() { 58 | return regionId; 59 | } 60 | 61 | public void setRegionId(Region regionId) { 62 | this.regionId = regionId; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (cityId != null ? cityId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof City)) { 76 | return false; 77 | } 78 | City other = (City) object; 79 | if ((this.cityId == null && other.cityId != null) || (this.cityId != null && !this.cityId.equals(other.cityId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.City[ cityId=" + cityId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_2_EclipseLink/src/main/java/edu/javacourse/jpa/entity/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.Basic; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | 16 | @Entity 17 | @Table(name = "jc_region") 18 | public class Region implements Serializable { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | @Column(name = "region_id") 23 | private Integer regionId; 24 | @Column(name = "region_name") 25 | private String regionName; 26 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "regionId", fetch = FetchType.LAZY) 27 | private List cityList; 28 | 29 | public Region() { 30 | } 31 | 32 | public Region(Integer regionId) { 33 | this.regionId = regionId; 34 | } 35 | 36 | public Region(Integer regionId, String regionName) { 37 | this.regionId = regionId; 38 | this.regionName = regionName; 39 | } 40 | 41 | public Integer getRegionId() { 42 | return regionId; 43 | } 44 | 45 | public void setRegionId(Integer regionId) { 46 | this.regionId = regionId; 47 | } 48 | 49 | public String getRegionName() { 50 | return regionName; 51 | } 52 | 53 | public void setRegionName(String regionName) { 54 | this.regionName = regionName; 55 | } 56 | 57 | public List getCityList() { 58 | return cityList; 59 | } 60 | 61 | public void setCityList(List cityList) { 62 | this.cityList = cityList; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (regionId != null ? regionId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof Region)) { 76 | return false; 77 | } 78 | Region other = (Region) object; 79 | if ((this.regionId == null && other.regionId != null) || (this.regionId != null && !this.regionId.equals(other.regionId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.JcRegion[ regionId=" + regionId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_2_EclipseLink/src/main/java/edu/javacourse/jpa/manager/RegionManager.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.manager; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import java.util.List; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.EntityManagerFactory; 7 | import javax.persistence.Persistence; 8 | import javax.persistence.criteria.CriteriaBuilder; 9 | import javax.persistence.criteria.CriteriaQuery; 10 | import javax.persistence.criteria.Root; 11 | 12 | public class RegionManager { 13 | 14 | private EntityManagerFactory entityManagerFactory; 15 | private EntityManager entityManager; 16 | 17 | public void init() { 18 | entityManagerFactory = Persistence.createEntityManagerFactory("unitEclipseLink", new java.util.HashMap()); 19 | System.out.println(entityManagerFactory.getClass().getSimpleName()); 20 | entityManager = entityManagerFactory.createEntityManager(); 21 | System.out.println(entityManager.getClass().getSimpleName()); 22 | } 23 | 24 | public List getRegionList() { 25 | return entityManager.createQuery("select r from Region r").getResultList(); 26 | } 27 | 28 | public List getRegionList2() { 29 | CriteriaBuilder cb = entityManager.getCriteriaBuilder(); 30 | CriteriaQuery cq = cb.createQuery(Region.class); 31 | return entityManager.createQuery(cq).getResultList(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /GS_JPA_2_EclipseLink/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | org.eclipse.persistence.jpa.PersistenceProvider 7 | edu.javacourse.jpa.entity.City 8 | edu.javacourse.jpa.entity.Region 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /GS_JPA_3_OpenJpa/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | jpa-openjpa 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 5.1.18 20 | 21 | 22 | 23 | org.apache.openjpa 24 | openjpa-all 25 | 2.2.2 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /GS_JPA_3_OpenJpa/src/main/java/edu/javacourse/jpa/SimpleExample.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import edu.javacourse.jpa.manager.RegionManager; 5 | import java.util.List; 6 | 7 | public class SimpleExample { 8 | 9 | public static void main(String[] args) { 10 | RegionManager rm = new RegionManager(); 11 | rm.init(); 12 | firstSelect(rm); 13 | secondSelect(rm); 14 | } 15 | 16 | private static void firstSelect(RegionManager rm) { 17 | System.out.println("First Select ===>"); 18 | List result = rm.getRegionList(); 19 | for(Region r : result) { 20 | System.out.println(r); 21 | } 22 | } 23 | private static void secondSelect(RegionManager rm) { 24 | System.out.println("Second Select ===>"); 25 | List result = rm.getRegionList2(); 26 | for(Region r : result) { 27 | System.out.println(r); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /GS_JPA_3_OpenJpa/src/main/java/edu/javacourse/jpa/entity/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import javax.persistence.Basic; 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.FetchType; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.JoinColumn; 12 | import javax.persistence.ManyToOne; 13 | import javax.persistence.Table; 14 | 15 | @Entity 16 | @Table(name = "jc_city") 17 | public class City implements Serializable { 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | @Column(name = "city_id") 22 | private Integer cityId; 23 | @Column(name = "city_name") 24 | private String cityName; 25 | @JoinColumn(name = "region_id", referencedColumnName = "region_id") 26 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 27 | private Region regionId; 28 | 29 | public City() { 30 | } 31 | 32 | public City(Integer cityId) { 33 | this.cityId = cityId; 34 | } 35 | 36 | public City(Integer cityId, String cityName) { 37 | this.cityId = cityId; 38 | this.cityName = cityName; 39 | } 40 | 41 | public Integer getCityId() { 42 | return cityId; 43 | } 44 | 45 | public void setCityId(Integer cityId) { 46 | this.cityId = cityId; 47 | } 48 | 49 | public String getCityName() { 50 | return cityName; 51 | } 52 | 53 | public void setCityName(String cityName) { 54 | this.cityName = cityName; 55 | } 56 | 57 | public Region getRegionId() { 58 | return regionId; 59 | } 60 | 61 | public void setRegionId(Region regionId) { 62 | this.regionId = regionId; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (cityId != null ? cityId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof City)) { 76 | return false; 77 | } 78 | City other = (City) object; 79 | if ((this.cityId == null && other.cityId != null) || (this.cityId != null && !this.cityId.equals(other.cityId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.City[ cityId=" + cityId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_3_OpenJpa/src/main/java/edu/javacourse/jpa/entity/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.Basic; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | 16 | @Entity 17 | @Table(name = "jc_region") 18 | public class Region implements Serializable { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | @Column(name = "region_id") 23 | private Integer regionId; 24 | @Column(name = "region_name") 25 | private String regionName; 26 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "regionId", fetch = FetchType.LAZY) 27 | private List cityList; 28 | 29 | public Region() { 30 | } 31 | 32 | public Region(Integer regionId) { 33 | this.regionId = regionId; 34 | } 35 | 36 | public Region(Integer regionId, String regionName) { 37 | this.regionId = regionId; 38 | this.regionName = regionName; 39 | } 40 | 41 | public Integer getRegionId() { 42 | return regionId; 43 | } 44 | 45 | public void setRegionId(Integer regionId) { 46 | this.regionId = regionId; 47 | } 48 | 49 | public String getRegionName() { 50 | return regionName; 51 | } 52 | 53 | public void setRegionName(String regionName) { 54 | this.regionName = regionName; 55 | } 56 | 57 | public List getCityList() { 58 | return cityList; 59 | } 60 | 61 | public void setCityList(List cityList) { 62 | this.cityList = cityList; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (regionId != null ? regionId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof Region)) { 76 | return false; 77 | } 78 | Region other = (Region) object; 79 | if ((this.regionId == null && other.regionId != null) || (this.regionId != null && !this.regionId.equals(other.regionId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.JcRegion[ regionId=" + regionId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_3_OpenJpa/src/main/java/edu/javacourse/jpa/manager/RegionManager.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.manager; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import java.util.List; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.EntityManagerFactory; 7 | import javax.persistence.Persistence; 8 | import javax.persistence.criteria.CriteriaBuilder; 9 | import javax.persistence.criteria.CriteriaQuery; 10 | import javax.persistence.criteria.Root; 11 | 12 | public class RegionManager { 13 | 14 | private EntityManagerFactory entityManagerFactory; 15 | private EntityManager entityManager; 16 | 17 | public void init() { 18 | entityManagerFactory = Persistence.createEntityManagerFactory("unitOpenJpa", new java.util.HashMap()); 19 | System.out.println(entityManagerFactory.getClass().getSimpleName()); 20 | entityManager = entityManagerFactory.createEntityManager(); 21 | System.out.println(entityManager.getClass().getSimpleName()); 22 | } 23 | 24 | public List getRegionList() { 25 | return entityManager.createQuery("select r from Region r").getResultList(); 26 | } 27 | 28 | public List getRegionList2() { 29 | CriteriaBuilder cb = entityManager.getCriteriaBuilder(); 30 | CriteriaQuery cq = cb.createQuery(); 31 | cq.from(Region.class); 32 | return entityManager.createQuery(cq).getResultList(); 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /GS_JPA_3_OpenJpa/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | org.apache.openjpa.persistence.PersistenceProviderImpl 8 | edu.javacourse.jpa.entity.City 9 | edu.javacourse.jpa.entity.Region 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /GS_JPA_4_Hibernate/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | jpa-hibernate 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 5.1.18 20 | 21 | 22 | 23 | org.hibernate.javax.persistence 24 | hibernate-jpa-2.0-api 25 | 1.0.1.Final 26 | 27 | 28 | 29 | org.hibernate 30 | hibernate-entitymanager 31 | 4.2.2.Final 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /GS_JPA_4_Hibernate/src/main/java/edu/javacourse/jpa/SimpleExample.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import edu.javacourse.jpa.manager.RegionManager; 5 | import java.util.List; 6 | 7 | public class SimpleExample { 8 | 9 | public static void main(String[] args) { 10 | RegionManager rm = new RegionManager(); 11 | rm.init(); 12 | firstSelect(rm); 13 | secondSelect(rm); 14 | } 15 | 16 | private static void firstSelect(RegionManager rm) { 17 | System.out.println("First Select ===>"); 18 | List result = rm.getRegionList(); 19 | for(Region r : result) { 20 | System.out.println(r); 21 | } 22 | } 23 | private static void secondSelect(RegionManager rm) { 24 | System.out.println("Second Select ===>"); 25 | List result = rm.getRegionList2(); 26 | for(Region r : result) { 27 | System.out.println(r); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /GS_JPA_4_Hibernate/src/main/java/edu/javacourse/jpa/entity/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import javax.persistence.Basic; 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.FetchType; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.JoinColumn; 12 | import javax.persistence.ManyToOne; 13 | import javax.persistence.Table; 14 | 15 | @Entity 16 | @Table(name = "jc_city") 17 | public class City implements Serializable { 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | @Column(name = "city_id") 22 | private Integer cityId; 23 | @Column(name = "city_name") 24 | private String cityName; 25 | @JoinColumn(name = "region_id", referencedColumnName = "region_id") 26 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 27 | private Region regionId; 28 | 29 | public City() { 30 | } 31 | 32 | public City(Integer cityId) { 33 | this.cityId = cityId; 34 | } 35 | 36 | public City(Integer cityId, String cityName) { 37 | this.cityId = cityId; 38 | this.cityName = cityName; 39 | } 40 | 41 | public Integer getCityId() { 42 | return cityId; 43 | } 44 | 45 | public void setCityId(Integer cityId) { 46 | this.cityId = cityId; 47 | } 48 | 49 | public String getCityName() { 50 | return cityName; 51 | } 52 | 53 | public void setCityName(String cityName) { 54 | this.cityName = cityName; 55 | } 56 | 57 | public Region getRegionId() { 58 | return regionId; 59 | } 60 | 61 | public void setRegionId(Region regionId) { 62 | this.regionId = regionId; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (cityId != null ? cityId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof City)) { 76 | return false; 77 | } 78 | City other = (City) object; 79 | if ((this.cityId == null && other.cityId != null) || (this.cityId != null && !this.cityId.equals(other.cityId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.City[ cityId=" + cityId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_4_Hibernate/src/main/java/edu/javacourse/jpa/entity/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.Basic; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | 16 | @Entity 17 | @Table(name = "jc_region") 18 | public class Region implements Serializable { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | @Column(name = "region_id") 23 | private Integer regionId; 24 | @Column(name = "region_name") 25 | private String regionName; 26 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "regionId", fetch = FetchType.LAZY) 27 | private List cityList; 28 | 29 | public Region() { 30 | } 31 | 32 | public Region(Integer regionId) { 33 | this.regionId = regionId; 34 | } 35 | 36 | public Region(Integer regionId, String regionName) { 37 | this.regionId = regionId; 38 | this.regionName = regionName; 39 | } 40 | 41 | public Integer getRegionId() { 42 | return regionId; 43 | } 44 | 45 | public void setRegionId(Integer regionId) { 46 | this.regionId = regionId; 47 | } 48 | 49 | public String getRegionName() { 50 | return regionName; 51 | } 52 | 53 | public void setRegionName(String regionName) { 54 | this.regionName = regionName; 55 | } 56 | 57 | public List getCityList() { 58 | return cityList; 59 | } 60 | 61 | public void setCityList(List cityList) { 62 | this.cityList = cityList; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (regionId != null ? regionId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof Region)) { 76 | return false; 77 | } 78 | Region other = (Region) object; 79 | if ((this.regionId == null && other.regionId != null) || (this.regionId != null && !this.regionId.equals(other.regionId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.JcRegion[ regionId=" + regionId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_4_Hibernate/src/main/java/edu/javacourse/jpa/manager/RegionManager.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.manager; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import java.util.List; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.EntityManagerFactory; 7 | import javax.persistence.Persistence; 8 | import javax.persistence.criteria.CriteriaBuilder; 9 | import javax.persistence.criteria.CriteriaQuery; 10 | import javax.persistence.criteria.Root; 11 | 12 | public class RegionManager { 13 | 14 | private EntityManagerFactory entityManagerFactory; 15 | private EntityManager entityManager; 16 | 17 | public void init() { 18 | entityManagerFactory = Persistence.createEntityManagerFactory("unitHibernate", new java.util.HashMap()); 19 | System.out.println(entityManagerFactory.getClass().getSimpleName()); 20 | entityManager = entityManagerFactory.createEntityManager(); 21 | System.out.println(entityManager.getClass().getSimpleName()); 22 | } 23 | 24 | public List getRegionList() { 25 | return entityManager.createQuery("select r from Region r").getResultList(); 26 | } 27 | 28 | public List getRegionList2() { 29 | CriteriaBuilder cb = entityManager.getCriteriaBuilder(); 30 | CriteriaQuery cq = cb.createQuery(Region.class); 31 | cq.from(Region.class); 32 | return entityManager.createQuery(cq).getResultList(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /GS_JPA_4_Hibernate/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | org.hibernate.ejb.HibernatePersistence 9 | edu.javacourse.jpa.entity.City 10 | edu.javacourse.jpa.entity.Region 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /GS_JPA_5_CRUD/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | jpa-crud 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 5.1.18 20 | 21 | 22 | 23 | org.eclipse.persistence 24 | org.eclipse.persistence.jpa 25 | 2.5.0 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /GS_JPA_5_CRUD/src/main/java/edu/javacourse/jpa/SimpleExample.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import edu.javacourse.jpa.manager.RegionManager; 5 | 6 | import javax.persistence.EntityManager; 7 | import java.util.List; 8 | 9 | public class SimpleExample { 10 | 11 | public static void main(String[] args) { 12 | RegionManager rm = new RegionManager(); 13 | rm.init(); 14 | EntityManager em = rm.getEntityManager(); 15 | 16 | selectRegions(em); 17 | 18 | Region region = new Region("Super Region"); 19 | int id = createRegion(em, region); 20 | 21 | selectRegions(em); 22 | 23 | Region fromDb = getRegion(em, id); 24 | System.out.println("fromDb = " + fromDb); 25 | 26 | fromDb.setRegionName("Updated Super Region"); 27 | 28 | fromDb = updateRegion(em, fromDb); 29 | System.out.println("fromDb updated = " + fromDb); 30 | 31 | removeRegion(em, fromDb); 32 | selectRegions(em); 33 | 34 | em.close(); 35 | 36 | 37 | 38 | 39 | 40 | } 41 | 42 | private static int createRegion(EntityManager em, Region region) { 43 | em.getTransaction().begin(); 44 | em.persist(region); 45 | em.getTransaction().commit(); 46 | return region.getRegionId(); 47 | } 48 | 49 | private static Region getRegion(EntityManager em, Integer id) { 50 | Region region = em.find(Region.class, id); 51 | return region; 52 | } 53 | 54 | private static Region updateRegion(EntityManager em, Region region) { 55 | em.getTransaction().begin(); 56 | em.merge(region); 57 | em.getTransaction().commit(); 58 | return region; 59 | } 60 | 61 | private static void removeRegion(EntityManager em, Region region) { 62 | em.getTransaction().begin(); 63 | em.remove(region); 64 | em.getTransaction().commit(); 65 | } 66 | 67 | private static void selectRegions(EntityManager em) { 68 | System.out.println("First Select ===>"); 69 | List result = em.createQuery("select r from Region r").getResultList(); 70 | for(Region r : result) { 71 | System.out.println(r); 72 | } 73 | System.out.println("============================="); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /GS_JPA_5_CRUD/src/main/java/edu/javacourse/jpa/entity/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import javax.persistence.Basic; 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.FetchType; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.GenerationType; 10 | import javax.persistence.Id; 11 | import javax.persistence.JoinColumn; 12 | import javax.persistence.ManyToOne; 13 | import javax.persistence.Table; 14 | 15 | @Entity 16 | @Table(name = "jc_city") 17 | public class City implements Serializable { 18 | 19 | @Id 20 | @GeneratedValue(strategy = GenerationType.IDENTITY) 21 | @Column(name = "city_id") 22 | private Integer cityId; 23 | @Column(name = "city_name") 24 | private String cityName; 25 | @JoinColumn(name = "region_id", referencedColumnName = "region_id") 26 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 27 | private Region regionId; 28 | 29 | public City() { 30 | } 31 | 32 | public City(Integer cityId) { 33 | this.cityId = cityId; 34 | } 35 | 36 | public City(Integer cityId, String cityName) { 37 | this.cityId = cityId; 38 | this.cityName = cityName; 39 | } 40 | 41 | public Integer getCityId() { 42 | return cityId; 43 | } 44 | 45 | public void setCityId(Integer cityId) { 46 | this.cityId = cityId; 47 | } 48 | 49 | public String getCityName() { 50 | return cityName; 51 | } 52 | 53 | public void setCityName(String cityName) { 54 | this.cityName = cityName; 55 | } 56 | 57 | public Region getRegionId() { 58 | return regionId; 59 | } 60 | 61 | public void setRegionId(Region regionId) { 62 | this.regionId = regionId; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (cityId != null ? cityId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof City)) { 76 | return false; 77 | } 78 | City other = (City) object; 79 | if ((this.cityId == null && other.cityId != null) || (this.cityId != null && !this.cityId.equals(other.cityId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.City[ cityId=" + cityId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_5_CRUD/src/main/java/edu/javacourse/jpa/entity/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.Basic; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | 16 | @Entity 17 | @Table(name = "jc_region") 18 | public class Region implements Serializable { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | @Column(name = "region_id") 23 | private Integer regionId; 24 | @Column(name = "region_name") 25 | private String regionName; 26 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "regionId", fetch = FetchType.LAZY) 27 | private List cityList; 28 | 29 | public Region() { 30 | } 31 | 32 | public Region(Integer regionId) { 33 | this.regionId = regionId; 34 | } 35 | 36 | public Region(String regionName) { 37 | this.regionName = regionName; 38 | } 39 | 40 | public Region(Integer regionId, String regionName) { 41 | this.regionId = regionId; 42 | this.regionName = regionName; 43 | } 44 | 45 | public Integer getRegionId() { 46 | return regionId; 47 | } 48 | 49 | public void setRegionId(Integer regionId) { 50 | this.regionId = regionId; 51 | } 52 | 53 | public String getRegionName() { 54 | return regionName; 55 | } 56 | 57 | public void setRegionName(String regionName) { 58 | this.regionName = regionName; 59 | } 60 | 61 | public List getCityList() { 62 | return cityList; 63 | } 64 | 65 | public void setCityList(List cityList) { 66 | this.cityList = cityList; 67 | } 68 | 69 | @Override 70 | public int hashCode() { 71 | int hash = 0; 72 | hash += (regionId != null ? regionId.hashCode() : 0); 73 | return hash; 74 | } 75 | 76 | @Override 77 | public boolean equals(Object object) { 78 | // TODO: Warning - this method won't work in the case the id fields are not set 79 | if (!(object instanceof Region)) { 80 | return false; 81 | } 82 | Region other = (Region) object; 83 | if ((this.regionId == null && other.regionId != null) || (this.regionId != null && !this.regionId.equals(other.regionId))) { 84 | return false; 85 | } 86 | return true; 87 | } 88 | 89 | @Override 90 | public String toString() { 91 | return "Region{" + 92 | "regionId=" + regionId + 93 | ", regionName='" + regionName + '\'' + 94 | '}'; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /GS_JPA_5_CRUD/src/main/java/edu/javacourse/jpa/manager/RegionManager.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.manager; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import java.util.List; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.EntityManagerFactory; 7 | import javax.persistence.Persistence; 8 | import javax.persistence.criteria.CriteriaBuilder; 9 | import javax.persistence.criteria.CriteriaQuery; 10 | import javax.persistence.criteria.Root; 11 | 12 | public class RegionManager { 13 | 14 | private EntityManagerFactory entityManagerFactory; 15 | private EntityManager entityManager; 16 | 17 | public void init() { 18 | entityManagerFactory = Persistence.createEntityManagerFactory("unitEclipseLink", new java.util.HashMap()); 19 | System.out.println(entityManagerFactory.getClass().getSimpleName()); 20 | entityManager = entityManagerFactory.createEntityManager(); 21 | System.out.println(entityManager.getClass().getSimpleName()); 22 | } 23 | 24 | public List getRegionList() { 25 | return entityManager.createQuery("select r from Region r").getResultList(); 26 | } 27 | 28 | public EntityManager getEntityManager() { 29 | return entityManager; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /GS_JPA_5_CRUD/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | org.eclipse.persistence.jpa.PersistenceProvider 7 | edu.javacourse.jpa.entity.City 8 | edu.javacourse.jpa.entity.Region 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /GS_JPA_6_JPQL/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | jpa-jpql 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 5.1.18 20 | 21 | 22 | 23 | org.eclipse.persistence 24 | org.eclipse.persistence.jpa 25 | 2.5.0 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /GS_JPA_6_JPQL/readme.txt: -------------------------------------------------------------------------------- 1 | http://docs.oracle.com/javaee/6/tutorial/doc/bnbtl.html -------------------------------------------------------------------------------- /GS_JPA_6_JPQL/src/main/java/edu/javacourse/jpa/SimpleExample.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa; 2 | 3 | import edu.javacourse.jpa.entity.Author; 4 | import edu.javacourse.jpa.entity.Book; 5 | import edu.javacourse.jpa.entity.City; 6 | import edu.javacourse.jpa.entity.Region; 7 | import edu.javacourse.jpa.manager.RegionManager; 8 | 9 | import javax.persistence.EntityManager; 10 | import javax.persistence.Query; 11 | import java.util.List; 12 | 13 | public class SimpleExample { 14 | 15 | public static void main(String[] args) { 16 | RegionManager rm = new RegionManager(); 17 | rm.init(); 18 | EntityManager em = rm.getEntityManager(); 19 | 20 | 21 | // cities where region name contains 'o' character 22 | // Query query = em.createQuery("select c from City c where c.region.regionName like :r"); 23 | // query.setParameter("r", "%o%"); 24 | // printCities(query.getResultList()); 25 | // 26 | // Query query2 = em.createQuery("select c from City c where c.region.regionName in ('Voronezh', 'Moscow')"); 27 | // printCities(query2.getResultList()); 28 | // 29 | // Query query3 = em.createQuery("select c from Region c where c.cityList is empty"); 30 | // printRegions(query3.getResultList()); 31 | // 32 | // Query query4 = em.createQuery("select a from Author a where size(a.bookList) > 2 "); 33 | // printAuthors(query4.getResultList()); 34 | // 35 | //Query query5 = em.createQuery("select b from Book b, Author a where b member of a.bookList and a.authorName =:Author"); 36 | Query query5 = em.createQuery("select b from Book b inner join b.authorList al where al.authorName=:Author"); 37 | query5.setParameter("Author", "Small Author"); 38 | printBooks(query5.getResultList()); 39 | 40 | 41 | } 42 | 43 | private static void printBooks(List books) { 44 | for (Book book : books) { 45 | System.out.println(book.getBookName()); 46 | } 47 | } 48 | 49 | private static void printAuthors(List authors) { 50 | for (Author author : authors) { 51 | System.out.println(author.getAuthorName()); 52 | } 53 | System.out.println("============================================"); 54 | } 55 | 56 | private static void printCities(List cities) { 57 | for (City city : cities) { 58 | System.out.println(city.getRegion().getRegionName() + " " + city.getCityName()); 59 | } 60 | System.out.println("============================================"); 61 | } 62 | 63 | private static void printRegions(List regions) { 64 | for (Region region : regions) { 65 | System.out.println(region.getRegionName()); 66 | } 67 | System.out.println("============================================"); 68 | } 69 | 70 | 71 | } 72 | -------------------------------------------------------------------------------- /GS_JPA_6_JPQL/src/main/java/edu/javacourse/jpa/entity/Author.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinTable; 15 | import javax.persistence.ManyToMany; 16 | import javax.persistence.OrderBy; 17 | import javax.persistence.Table; 18 | import javax.persistence.Transient; 19 | 20 | @Entity 21 | @Table(name = "jc_author") 22 | public class Author implements Serializable { 23 | 24 | @Id 25 | @GeneratedValue(strategy = GenerationType.IDENTITY) 26 | @Column(name = "author_id") 27 | private Long authorId; 28 | @Column(name = "author_name") 29 | private String authorName; 30 | 31 | @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY) 32 | @JoinTable(name = "jc_book_author", 33 | joinColumns = @JoinColumn(name = "author_id"), 34 | inverseJoinColumns = @JoinColumn(name = "book_id")) 35 | @OrderBy(value="bookName") 36 | private Set bookList; 37 | 38 | public Long getAuthorId() { 39 | return authorId; 40 | } 41 | 42 | public void setAuthorId(Long authorId) { 43 | this.authorId = authorId; 44 | } 45 | 46 | public String getAuthorName() { 47 | return authorName; 48 | } 49 | 50 | public void setAuthorName(String authorName) { 51 | this.authorName = authorName; 52 | } 53 | 54 | public Set getBookList() { 55 | return bookList; 56 | } 57 | 58 | public void setBookList(Set bookList) { 59 | this.bookList = bookList; 60 | } 61 | 62 | public void addBook(Book book) { 63 | if (bookList == null) { 64 | bookList = new HashSet(); 65 | } 66 | bookList.add(book); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "Author{" + "authorId=" + authorId + ", authorName=" + authorName + '}'; 72 | } 73 | 74 | @Override 75 | public boolean equals(Object obj) { 76 | if (obj == null) { 77 | return false; 78 | } 79 | if (getClass() != obj.getClass()) { 80 | return false; 81 | } 82 | final Author other = (Author) obj; 83 | if (this.authorId != other.authorId && (this.authorId == null || !this.authorId.equals(other.authorId))) { 84 | return false; 85 | } 86 | return true; 87 | } 88 | 89 | @Override 90 | public int hashCode() { 91 | int hash = 7; 92 | hash = 71 * hash + (this.authorId != null ? this.authorId.hashCode() : 0); 93 | return hash; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /GS_JPA_6_JPQL/src/main/java/edu/javacourse/jpa/entity/Book.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinTable; 15 | import javax.persistence.ManyToMany; 16 | import javax.persistence.OrderBy; 17 | import javax.persistence.Table; 18 | 19 | @Entity 20 | @Table(name = "jc_book") 21 | public class Book implements Serializable { 22 | 23 | @Id 24 | @GeneratedValue(strategy = GenerationType.IDENTITY) 25 | @Column(name = "book_id") 26 | private Long bookId; 27 | @Column(name = "book_name") 28 | private String bookName; 29 | 30 | @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY) 31 | @JoinTable(name = "jc_book_author", 32 | joinColumns = @JoinColumn(name = "book_id"), 33 | inverseJoinColumns = @JoinColumn(name = "author_id")) 34 | @OrderBy(value="authorName") 35 | private Set authorList; 36 | 37 | public Set getAuthorList() { 38 | return authorList; 39 | } 40 | 41 | public void setAuthorList(Set authorList) { 42 | this.authorList = authorList; 43 | } 44 | 45 | public void addAuthor(Author author) { 46 | if (authorList == null) { 47 | authorList = new HashSet(); 48 | } 49 | authorList.add(author); 50 | } 51 | 52 | public Long getBookId() { 53 | return bookId; 54 | } 55 | 56 | public void setBookId(Long bookId) { 57 | this.bookId = bookId; 58 | } 59 | 60 | public String getBookName() { 61 | return bookName; 62 | } 63 | 64 | public void setBookName(String bookName) { 65 | this.bookName = bookName; 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return "Book{" + "bookId=" + bookId + ", bookName=" + bookName + '}'; 71 | } 72 | 73 | @Override 74 | public boolean equals(Object obj) { 75 | if (obj == null) { 76 | return false; 77 | } 78 | if (getClass() != obj.getClass()) { 79 | return false; 80 | } 81 | final Book other = (Book) obj; 82 | if (this.bookId != other.bookId && (this.bookId == null || !this.bookId.equals(other.bookId))) { 83 | return false; 84 | } 85 | return true; 86 | } 87 | 88 | @Override 89 | public int hashCode() { 90 | int hash = 7; 91 | hash = 71 * hash + (this.bookId != null ? this.bookId.hashCode() : 0); 92 | return hash; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /GS_JPA_6_JPQL/src/main/java/edu/javacourse/jpa/entity/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.FetchType; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.JoinColumn; 11 | import javax.persistence.ManyToOne; 12 | import javax.persistence.Table; 13 | 14 | @Entity 15 | @Table(name = "jc_city") 16 | public class City implements Serializable { 17 | 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | @Column(name = "city_id") 21 | private Integer cityId; 22 | @Column(name = "city_name") 23 | private String cityName; 24 | 25 | @JoinColumn(name = "region_id", referencedColumnName = "region_id") 26 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 27 | private Region region; 28 | 29 | public City() { 30 | } 31 | 32 | public City(Integer cityId) { 33 | this.cityId = cityId; 34 | } 35 | 36 | public City(Integer cityId, String cityName) { 37 | this.cityId = cityId; 38 | this.cityName = cityName; 39 | } 40 | 41 | public Integer getCityId() { 42 | return cityId; 43 | } 44 | 45 | public void setCityId(Integer cityId) { 46 | this.cityId = cityId; 47 | } 48 | 49 | public String getCityName() { 50 | return cityName; 51 | } 52 | 53 | public void setCityName(String cityName) { 54 | this.cityName = cityName; 55 | } 56 | 57 | public Region getRegion() { 58 | return region; 59 | } 60 | 61 | public void setRegion(Region region) { 62 | this.region = region; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (cityId != null ? cityId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof City)) { 76 | return false; 77 | } 78 | City other = (City) object; 79 | if ((this.cityId == null && other.cityId != null) || (this.cityId != null && !this.cityId.equals(other.cityId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.City[ cityId=" + cityId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_6_JPQL/src/main/java/edu/javacourse/jpa/entity/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.Basic; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | 16 | @Entity 17 | @Table(name = "jc_region") 18 | public class Region implements Serializable { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | @Column(name = "region_id") 23 | private Integer regionId; 24 | @Column(name = "region_name") 25 | private String regionName; 26 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "region", fetch = FetchType.LAZY) 27 | private List cityList; 28 | 29 | public Region() { 30 | } 31 | 32 | public Region(Integer regionId) { 33 | this.regionId = regionId; 34 | } 35 | 36 | public Region(String regionName) { 37 | this.regionName = regionName; 38 | } 39 | 40 | public Region(Integer regionId, String regionName) { 41 | this.regionId = regionId; 42 | this.regionName = regionName; 43 | } 44 | 45 | public Integer getRegionId() { 46 | return regionId; 47 | } 48 | 49 | public void setRegionId(Integer regionId) { 50 | this.regionId = regionId; 51 | } 52 | 53 | public String getRegionName() { 54 | return regionName; 55 | } 56 | 57 | public void setRegionName(String regionName) { 58 | this.regionName = regionName; 59 | } 60 | 61 | public List getCityList() { 62 | return cityList; 63 | } 64 | 65 | public void setCityList(List cityList) { 66 | this.cityList = cityList; 67 | } 68 | 69 | @Override 70 | public int hashCode() { 71 | int hash = 0; 72 | hash += (regionId != null ? regionId.hashCode() : 0); 73 | return hash; 74 | } 75 | 76 | @Override 77 | public boolean equals(Object object) { 78 | // TODO: Warning - this method won't work in the case the id fields are not set 79 | if (!(object instanceof Region)) { 80 | return false; 81 | } 82 | Region other = (Region) object; 83 | if ((this.regionId == null && other.regionId != null) || (this.regionId != null && !this.regionId.equals(other.regionId))) { 84 | return false; 85 | } 86 | return true; 87 | } 88 | 89 | @Override 90 | public String toString() { 91 | return "Region{" + 92 | "regionId=" + regionId + 93 | ", regionName='" + regionName + '\'' + 94 | '}'; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /GS_JPA_6_JPQL/src/main/java/edu/javacourse/jpa/manager/RegionManager.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.manager; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import java.util.List; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.EntityManagerFactory; 7 | import javax.persistence.Persistence; 8 | import javax.persistence.criteria.CriteriaBuilder; 9 | import javax.persistence.criteria.CriteriaQuery; 10 | import javax.persistence.criteria.Root; 11 | 12 | public class RegionManager { 13 | 14 | private EntityManagerFactory entityManagerFactory; 15 | private EntityManager entityManager; 16 | 17 | public void init() { 18 | entityManagerFactory = Persistence.createEntityManagerFactory("unitEclipseLink", new java.util.HashMap()); 19 | System.out.println(entityManagerFactory.getClass().getSimpleName()); 20 | entityManager = entityManagerFactory.createEntityManager(); 21 | System.out.println(entityManager.getClass().getSimpleName()); 22 | } 23 | 24 | public List getRegionList() { 25 | return entityManager.createQuery("select r from Region r").getResultList(); 26 | } 27 | 28 | public EntityManager getEntityManager() { 29 | return entityManager; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /GS_JPA_6_JPQL/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | org.eclipse.persistence.jpa.PersistenceProvider 7 | edu.javacourse.jpa.entity.City 8 | edu.javacourse.jpa.entity.Region 9 | edu.javacourse.jpa.entity.Author 10 | edu.javacourse.jpa.entity.Book 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /GS_JPA_7_NativeQuery/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | jpa-native 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 5.1.18 20 | 21 | 22 | 23 | org.eclipse.persistence 24 | org.eclipse.persistence.jpa 25 | 2.5.0 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /GS_JPA_7_NativeQuery/src/main/java/edu/javacourse/jpa/SimpleExample.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa; 2 | 3 | import edu.javacourse.jpa.entity.Author; 4 | import edu.javacourse.jpa.entity.Book; 5 | import edu.javacourse.jpa.entity.City; 6 | import edu.javacourse.jpa.entity.Region; 7 | import edu.javacourse.jpa.manager.RegionManager; 8 | 9 | import javax.persistence.EntityManager; 10 | import javax.persistence.Query; 11 | import java.util.List; 12 | 13 | public class SimpleExample { 14 | 15 | public static void main(String[] args) { 16 | RegionManager rm = new RegionManager(); 17 | rm.init(); 18 | EntityManager em = rm.getEntityManager(); 19 | 20 | 21 | 22 | Query query = em.createNativeQuery("select * from jc_city", City.class); 23 | printCities(query.getResultList()); 24 | 25 | Query query2 = em.createNativeQuery("SELECT * FROM jc_author AS a JOIN jc_book_author AS ba ON a.author_id = ba.author_id JOIN jc_book AS b ON b.book_id = ba.book_id AND b.book_name=?1", Author.class); 26 | query2.setParameter(1, "Java for beginners"); 27 | printAuthors(query2.getResultList()); 28 | 29 | 30 | // select regions with cities size greater than one 31 | Query query3 = em.createNativeQuery("SELECT * FROM jc_region AS reg JOIN jc_city AS cit ON reg.region_id = cit.region_id GROUP BY region_name HAVING COUNT(cit.region_id) > 1", Region.class); 32 | printRegions(query3.getResultList()); 33 | 34 | 35 | 36 | 37 | } 38 | 39 | private static void printBooks(List books) { 40 | for (Book book : books) { 41 | System.out.println(book.getBookName()); 42 | } 43 | } 44 | 45 | private static void printAuthors(List authors) { 46 | for (Author author : authors) { 47 | System.out.println(author.getAuthorName()); 48 | } 49 | System.out.println("============================================"); 50 | } 51 | 52 | private static void printCities(List cities) { 53 | for (City city : cities) { 54 | System.out.println(city.getRegion().getRegionName() + " " + city.getCityName()); 55 | } 56 | System.out.println("============================================"); 57 | } 58 | 59 | private static void printRegions(List regions) { 60 | for (Region region : regions) { 61 | System.out.println(region.getRegionName()); 62 | } 63 | System.out.println("============================================"); 64 | } 65 | 66 | 67 | } 68 | -------------------------------------------------------------------------------- /GS_JPA_7_NativeQuery/src/main/java/edu/javacourse/jpa/entity/Author.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinTable; 15 | import javax.persistence.ManyToMany; 16 | import javax.persistence.OrderBy; 17 | import javax.persistence.Table; 18 | import javax.persistence.Transient; 19 | 20 | @Entity 21 | @Table(name = "jc_author") 22 | public class Author implements Serializable { 23 | 24 | @Id 25 | @GeneratedValue(strategy = GenerationType.IDENTITY) 26 | @Column(name = "author_id") 27 | private Long authorId; 28 | @Column(name = "author_name") 29 | private String authorName; 30 | 31 | @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY) 32 | @JoinTable(name = "jc_book_author", 33 | joinColumns = @JoinColumn(name = "author_id"), 34 | inverseJoinColumns = @JoinColumn(name = "book_id")) 35 | @OrderBy(value="bookName") 36 | private Set bookList; 37 | 38 | public Long getAuthorId() { 39 | return authorId; 40 | } 41 | 42 | public void setAuthorId(Long authorId) { 43 | this.authorId = authorId; 44 | } 45 | 46 | public String getAuthorName() { 47 | return authorName; 48 | } 49 | 50 | public void setAuthorName(String authorName) { 51 | this.authorName = authorName; 52 | } 53 | 54 | public Set getBookList() { 55 | return bookList; 56 | } 57 | 58 | public void setBookList(Set bookList) { 59 | this.bookList = bookList; 60 | } 61 | 62 | public void addBook(Book book) { 63 | if (bookList == null) { 64 | bookList = new HashSet(); 65 | } 66 | bookList.add(book); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "Author{" + "authorId=" + authorId + ", authorName=" + authorName + '}'; 72 | } 73 | 74 | @Override 75 | public boolean equals(Object obj) { 76 | if (obj == null) { 77 | return false; 78 | } 79 | if (getClass() != obj.getClass()) { 80 | return false; 81 | } 82 | final Author other = (Author) obj; 83 | if (this.authorId != other.authorId && (this.authorId == null || !this.authorId.equals(other.authorId))) { 84 | return false; 85 | } 86 | return true; 87 | } 88 | 89 | @Override 90 | public int hashCode() { 91 | int hash = 7; 92 | hash = 71 * hash + (this.authorId != null ? this.authorId.hashCode() : 0); 93 | return hash; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /GS_JPA_7_NativeQuery/src/main/java/edu/javacourse/jpa/entity/Book.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.JoinTable; 15 | import javax.persistence.ManyToMany; 16 | import javax.persistence.OrderBy; 17 | import javax.persistence.Table; 18 | 19 | @Entity 20 | @Table(name = "jc_book") 21 | public class Book implements Serializable { 22 | 23 | @Id 24 | @GeneratedValue(strategy = GenerationType.IDENTITY) 25 | @Column(name = "book_id") 26 | private Long bookId; 27 | @Column(name = "book_name") 28 | private String bookName; 29 | 30 | @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY) 31 | @JoinTable(name = "jc_book_author", 32 | joinColumns = @JoinColumn(name = "book_id"), 33 | inverseJoinColumns = @JoinColumn(name = "author_id")) 34 | @OrderBy(value="authorName") 35 | private Set authorList; 36 | 37 | public Set getAuthorList() { 38 | return authorList; 39 | } 40 | 41 | public void setAuthorList(Set authorList) { 42 | this.authorList = authorList; 43 | } 44 | 45 | public void addAuthor(Author author) { 46 | if (authorList == null) { 47 | authorList = new HashSet(); 48 | } 49 | authorList.add(author); 50 | } 51 | 52 | public Long getBookId() { 53 | return bookId; 54 | } 55 | 56 | public void setBookId(Long bookId) { 57 | this.bookId = bookId; 58 | } 59 | 60 | public String getBookName() { 61 | return bookName; 62 | } 63 | 64 | public void setBookName(String bookName) { 65 | this.bookName = bookName; 66 | } 67 | 68 | @Override 69 | public String toString() { 70 | return "Book{" + "bookId=" + bookId + ", bookName=" + bookName + '}'; 71 | } 72 | 73 | @Override 74 | public boolean equals(Object obj) { 75 | if (obj == null) { 76 | return false; 77 | } 78 | if (getClass() != obj.getClass()) { 79 | return false; 80 | } 81 | final Book other = (Book) obj; 82 | if (this.bookId != other.bookId && (this.bookId == null || !this.bookId.equals(other.bookId))) { 83 | return false; 84 | } 85 | return true; 86 | } 87 | 88 | @Override 89 | public int hashCode() { 90 | int hash = 7; 91 | hash = 71 * hash + (this.bookId != null ? this.bookId.hashCode() : 0); 92 | return hash; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /GS_JPA_7_NativeQuery/src/main/java/edu/javacourse/jpa/entity/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.FetchType; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.JoinColumn; 11 | import javax.persistence.ManyToOne; 12 | import javax.persistence.Table; 13 | 14 | @Entity 15 | @Table(name = "jc_city") 16 | public class City implements Serializable { 17 | 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | @Column(name = "city_id") 21 | private Integer cityId; 22 | @Column(name = "city_name") 23 | private String cityName; 24 | 25 | @JoinColumn(name = "region_id", referencedColumnName = "region_id") 26 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 27 | private Region region; 28 | 29 | public City() { 30 | } 31 | 32 | public City(Integer cityId) { 33 | this.cityId = cityId; 34 | } 35 | 36 | public City(Integer cityId, String cityName) { 37 | this.cityId = cityId; 38 | this.cityName = cityName; 39 | } 40 | 41 | public Integer getCityId() { 42 | return cityId; 43 | } 44 | 45 | public void setCityId(Integer cityId) { 46 | this.cityId = cityId; 47 | } 48 | 49 | public String getCityName() { 50 | return cityName; 51 | } 52 | 53 | public void setCityName(String cityName) { 54 | this.cityName = cityName; 55 | } 56 | 57 | public Region getRegion() { 58 | return region; 59 | } 60 | 61 | public void setRegion(Region region) { 62 | this.region = region; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (cityId != null ? cityId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof City)) { 76 | return false; 77 | } 78 | City other = (City) object; 79 | if ((this.cityId == null && other.cityId != null) || (this.cityId != null && !this.cityId.equals(other.cityId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.City[ cityId=" + cityId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_7_NativeQuery/src/main/java/edu/javacourse/jpa/entity/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.Basic; 6 | import javax.persistence.CascadeType; 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.FetchType; 10 | import javax.persistence.GeneratedValue; 11 | import javax.persistence.GenerationType; 12 | import javax.persistence.Id; 13 | import javax.persistence.OneToMany; 14 | import javax.persistence.Table; 15 | 16 | @Entity 17 | @Table(name = "jc_region") 18 | public class Region implements Serializable { 19 | 20 | @Id 21 | @GeneratedValue(strategy = GenerationType.IDENTITY) 22 | @Column(name = "region_id") 23 | private Integer regionId; 24 | @Column(name = "region_name") 25 | private String regionName; 26 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "region", fetch = FetchType.LAZY) 27 | private List cityList; 28 | 29 | public Region() { 30 | } 31 | 32 | public Region(Integer regionId) { 33 | this.regionId = regionId; 34 | } 35 | 36 | public Region(String regionName) { 37 | this.regionName = regionName; 38 | } 39 | 40 | public Region(Integer regionId, String regionName) { 41 | this.regionId = regionId; 42 | this.regionName = regionName; 43 | } 44 | 45 | public Integer getRegionId() { 46 | return regionId; 47 | } 48 | 49 | public void setRegionId(Integer regionId) { 50 | this.regionId = regionId; 51 | } 52 | 53 | public String getRegionName() { 54 | return regionName; 55 | } 56 | 57 | public void setRegionName(String regionName) { 58 | this.regionName = regionName; 59 | } 60 | 61 | public List getCityList() { 62 | return cityList; 63 | } 64 | 65 | public void setCityList(List cityList) { 66 | this.cityList = cityList; 67 | } 68 | 69 | @Override 70 | public int hashCode() { 71 | int hash = 0; 72 | hash += (regionId != null ? regionId.hashCode() : 0); 73 | return hash; 74 | } 75 | 76 | @Override 77 | public boolean equals(Object object) { 78 | // TODO: Warning - this method won't work in the case the id fields are not set 79 | if (!(object instanceof Region)) { 80 | return false; 81 | } 82 | Region other = (Region) object; 83 | if ((this.regionId == null && other.regionId != null) || (this.regionId != null && !this.regionId.equals(other.regionId))) { 84 | return false; 85 | } 86 | return true; 87 | } 88 | 89 | @Override 90 | public String toString() { 91 | return "Region{" + 92 | "regionId=" + regionId + 93 | ", regionName='" + regionName + '\'' + 94 | '}'; 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /GS_JPA_7_NativeQuery/src/main/java/edu/javacourse/jpa/manager/RegionManager.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.manager; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import java.util.List; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.EntityManagerFactory; 7 | import javax.persistence.Persistence; 8 | import javax.persistence.criteria.CriteriaBuilder; 9 | import javax.persistence.criteria.CriteriaQuery; 10 | import javax.persistence.criteria.Root; 11 | 12 | public class RegionManager { 13 | 14 | private EntityManagerFactory entityManagerFactory; 15 | private EntityManager entityManager; 16 | 17 | public void init() { 18 | entityManagerFactory = Persistence.createEntityManagerFactory("unitEclipseLink", new java.util.HashMap()); 19 | System.out.println(entityManagerFactory.getClass().getSimpleName()); 20 | entityManager = entityManagerFactory.createEntityManager(); 21 | System.out.println(entityManager.getClass().getSimpleName()); 22 | } 23 | 24 | public List getRegionList() { 25 | return entityManager.createQuery("select r from Region r").getResultList(); 26 | } 27 | 28 | public EntityManager getEntityManager() { 29 | return entityManager; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /GS_JPA_7_NativeQuery/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | org.eclipse.persistence.jpa.PersistenceProvider 7 | edu.javacourse.jpa.entity.City 8 | edu.javacourse.jpa.entity.Region 9 | edu.javacourse.jpa.entity.Author 10 | edu.javacourse.jpa.entity.Book 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /GS_JPA_8_NamedQuery/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | jpa-named 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 5.1.18 20 | 21 | 22 | 23 | org.eclipse.persistence 24 | org.eclipse.persistence.jpa 25 | 2.5.0 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /GS_JPA_8_NamedQuery/readme.txt: -------------------------------------------------------------------------------- 1 | http://www.objectdb.com/java/jpa/query/named -------------------------------------------------------------------------------- /GS_JPA_8_NamedQuery/src/main/java/edu/javacourse/jpa/SimpleExample.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa; 2 | 3 | import edu.javacourse.jpa.entity.Author; 4 | import edu.javacourse.jpa.entity.Book; 5 | import edu.javacourse.jpa.entity.City; 6 | import edu.javacourse.jpa.entity.Region; 7 | import edu.javacourse.jpa.manager.RegionManager; 8 | 9 | import javax.persistence.EntityManager; 10 | import javax.persistence.Query; 11 | import java.util.List; 12 | 13 | public class SimpleExample { 14 | 15 | public static void main(String[] args) { 16 | RegionManager rm = new RegionManager(); 17 | rm.init(); 18 | EntityManager em = rm.getEntityManager(); 19 | 20 | 21 | Query query = em.createNamedQuery("Book.ByName"); 22 | query.setParameter("name", "Java for beginners"); 23 | Book book = (Book) query.getSingleResult(); 24 | System.out.println("book = " + book); 25 | 26 | Query query2 = em.createNamedQuery("Book.ByAuthor"); 27 | query2.setParameter("Author", "Small Author"); 28 | List books = query2.getResultList(); 29 | printBooks(books); 30 | 31 | Query query3 = em.createNamedQuery("Author.ById"); 32 | query3.setParameter("id", 1); 33 | Author author = (Author) query3.getSingleResult(); 34 | System.out.println("author = " + author); 35 | 36 | 37 | Query query4 = em.createNamedQuery("Region.Count"); 38 | Long count = (Long)query4.getSingleResult(); 39 | System.out.println("count = " + count); 40 | 41 | Query query5 = em.createNamedQuery("Region.DelebeById"); 42 | em.getTransaction().begin(); 43 | query5.setParameter("id", count); 44 | query5.executeUpdate(); 45 | em.getTransaction().commit(); 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | } 54 | 55 | private static void printBooks(List books) { 56 | for (Book book : books) { 57 | System.out.println(book.getBookName()); 58 | } 59 | } 60 | 61 | private static void printAuthors(List authors) { 62 | for (Author author : authors) { 63 | System.out.println(author.getAuthorName()); 64 | } 65 | System.out.println("============================================"); 66 | } 67 | 68 | private static void printCities(List cities) { 69 | for (City city : cities) { 70 | System.out.println(city.getRegion().getRegionName() + " " + city.getCityName()); 71 | } 72 | System.out.println("============================================"); 73 | } 74 | 75 | private static void printRegions(List regions) { 76 | for (Region region : regions) { 77 | System.out.println(region.getRegionName()); 78 | } 79 | System.out.println("============================================"); 80 | } 81 | 82 | 83 | } 84 | -------------------------------------------------------------------------------- /GS_JPA_8_NamedQuery/src/main/java/edu/javacourse/jpa/entity/Author.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import javax.persistence.*; 4 | import java.io.Serializable; 5 | import java.util.HashSet; 6 | import java.util.Set; 7 | 8 | @Entity 9 | @Table(name = "jc_author") 10 | @NamedQueries({ 11 | @NamedQuery(name = "Author.ById", query = "select a from Author as a where a.authorId =:id") 12 | }) 13 | public class Author implements Serializable { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | @Column(name = "author_id") 18 | private Long authorId; 19 | @Column(name = "author_name") 20 | private String authorName; 21 | 22 | @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY) 23 | @JoinTable(name = "jc_book_author", 24 | joinColumns = @JoinColumn(name = "author_id"), 25 | inverseJoinColumns = @JoinColumn(name = "book_id")) 26 | @OrderBy(value="bookName") 27 | private Set bookList; 28 | 29 | public Long getAuthorId() { 30 | return authorId; 31 | } 32 | 33 | public void setAuthorId(Long authorId) { 34 | this.authorId = authorId; 35 | } 36 | 37 | public String getAuthorName() { 38 | return authorName; 39 | } 40 | 41 | public void setAuthorName(String authorName) { 42 | this.authorName = authorName; 43 | } 44 | 45 | public Set getBookList() { 46 | return bookList; 47 | } 48 | 49 | public void setBookList(Set bookList) { 50 | this.bookList = bookList; 51 | } 52 | 53 | public void addBook(Book book) { 54 | if (bookList == null) { 55 | bookList = new HashSet(); 56 | } 57 | bookList.add(book); 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "Author{" + "authorId=" + authorId + ", authorName=" + authorName + '}'; 63 | } 64 | 65 | @Override 66 | public boolean equals(Object obj) { 67 | if (obj == null) { 68 | return false; 69 | } 70 | if (getClass() != obj.getClass()) { 71 | return false; 72 | } 73 | final Author other = (Author) obj; 74 | if (this.authorId != other.authorId && (this.authorId == null || !this.authorId.equals(other.authorId))) { 75 | return false; 76 | } 77 | return true; 78 | } 79 | 80 | @Override 81 | public int hashCode() { 82 | int hash = 7; 83 | hash = 71 * hash + (this.authorId != null ? this.authorId.hashCode() : 0); 84 | return hash; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /GS_JPA_8_NamedQuery/src/main/java/edu/javacourse/jpa/entity/Book.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | import javax.persistence.*; 7 | 8 | @Entity 9 | @Table(name = "jc_book") 10 | @NamedQueries({ 11 | @NamedQuery(name = "Book.ByName", query = "select b from Book as b where b.bookName =:name"), 12 | @NamedQuery(name = "Book.ByAuthor", query = "select b from Book as b, Author a where b member of a.bookList and a.authorName =:Author") 13 | }) 14 | public class Book implements Serializable { 15 | 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.IDENTITY) 18 | @Column(name = "book_id") 19 | private Long bookId; 20 | @Column(name = "book_name") 21 | private String bookName; 22 | 23 | @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY) 24 | @JoinTable(name = "jc_book_author", 25 | joinColumns = @JoinColumn(name = "book_id"), 26 | inverseJoinColumns = @JoinColumn(name = "author_id")) 27 | @OrderBy(value="authorName") 28 | private Set authorList; 29 | 30 | public Set getAuthorList() { 31 | return authorList; 32 | } 33 | 34 | public void setAuthorList(Set authorList) { 35 | this.authorList = authorList; 36 | } 37 | 38 | public void addAuthor(Author author) { 39 | if (authorList == null) { 40 | authorList = new HashSet(); 41 | } 42 | authorList.add(author); 43 | } 44 | 45 | public Long getBookId() { 46 | return bookId; 47 | } 48 | 49 | public void setBookId(Long bookId) { 50 | this.bookId = bookId; 51 | } 52 | 53 | public String getBookName() { 54 | return bookName; 55 | } 56 | 57 | public void setBookName(String bookName) { 58 | this.bookName = bookName; 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | return "Book{" + "bookId=" + bookId + ", bookName=" + bookName + '}'; 64 | } 65 | 66 | @Override 67 | public boolean equals(Object obj) { 68 | if (obj == null) { 69 | return false; 70 | } 71 | if (getClass() != obj.getClass()) { 72 | return false; 73 | } 74 | final Book other = (Book) obj; 75 | if (this.bookId != other.bookId && (this.bookId == null || !this.bookId.equals(other.bookId))) { 76 | return false; 77 | } 78 | return true; 79 | } 80 | 81 | @Override 82 | public int hashCode() { 83 | int hash = 7; 84 | hash = 71 * hash + (this.bookId != null ? this.bookId.hashCode() : 0); 85 | return hash; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /GS_JPA_8_NamedQuery/src/main/java/edu/javacourse/jpa/entity/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.FetchType; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.JoinColumn; 11 | import javax.persistence.ManyToOne; 12 | import javax.persistence.Table; 13 | 14 | @Entity 15 | @Table(name = "jc_city") 16 | public class City implements Serializable { 17 | 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | @Column(name = "city_id") 21 | private Integer cityId; 22 | @Column(name = "city_name") 23 | private String cityName; 24 | 25 | @JoinColumn(name = "region_id", referencedColumnName = "region_id") 26 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 27 | private Region region; 28 | 29 | public City() { 30 | } 31 | 32 | public City(Integer cityId) { 33 | this.cityId = cityId; 34 | } 35 | 36 | public City(Integer cityId, String cityName) { 37 | this.cityId = cityId; 38 | this.cityName = cityName; 39 | } 40 | 41 | public Integer getCityId() { 42 | return cityId; 43 | } 44 | 45 | public void setCityId(Integer cityId) { 46 | this.cityId = cityId; 47 | } 48 | 49 | public String getCityName() { 50 | return cityName; 51 | } 52 | 53 | public void setCityName(String cityName) { 54 | this.cityName = cityName; 55 | } 56 | 57 | public Region getRegion() { 58 | return region; 59 | } 60 | 61 | public void setRegion(Region region) { 62 | this.region = region; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (cityId != null ? cityId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof City)) { 76 | return false; 77 | } 78 | City other = (City) object; 79 | if ((this.cityId == null && other.cityId != null) || (this.cityId != null && !this.cityId.equals(other.cityId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.City[ cityId=" + cityId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_8_NamedQuery/src/main/java/edu/javacourse/jpa/entity/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.*; 6 | 7 | @Entity 8 | @Table(name = "jc_region") 9 | @NamedQueries({ 10 | @NamedQuery(name = "Region.DelebeById", query = "delete from Region r where r.regionId =:id"), 11 | @NamedQuery(name = "Region.Count", query = "select count (r) from Region r") 12 | }) 13 | public class Region implements Serializable { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | @Column(name = "region_id") 18 | private Integer regionId; 19 | @Column(name = "region_name") 20 | private String regionName; 21 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "region", fetch = FetchType.LAZY) 22 | private List cityList; 23 | 24 | public Region() { 25 | } 26 | 27 | public Region(Integer regionId) { 28 | this.regionId = regionId; 29 | } 30 | 31 | public Region(String regionName) { 32 | this.regionName = regionName; 33 | } 34 | 35 | public Region(Integer regionId, String regionName) { 36 | this.regionId = regionId; 37 | this.regionName = regionName; 38 | } 39 | 40 | public Integer getRegionId() { 41 | return regionId; 42 | } 43 | 44 | public void setRegionId(Integer regionId) { 45 | this.regionId = regionId; 46 | } 47 | 48 | public String getRegionName() { 49 | return regionName; 50 | } 51 | 52 | public void setRegionName(String regionName) { 53 | this.regionName = regionName; 54 | } 55 | 56 | public List getCityList() { 57 | return cityList; 58 | } 59 | 60 | public void setCityList(List cityList) { 61 | this.cityList = cityList; 62 | } 63 | 64 | @Override 65 | public int hashCode() { 66 | int hash = 0; 67 | hash += (regionId != null ? regionId.hashCode() : 0); 68 | return hash; 69 | } 70 | 71 | @Override 72 | public boolean equals(Object object) { 73 | // TODO: Warning - this method won't work in the case the id fields are not set 74 | if (!(object instanceof Region)) { 75 | return false; 76 | } 77 | Region other = (Region) object; 78 | if ((this.regionId == null && other.regionId != null) || (this.regionId != null && !this.regionId.equals(other.regionId))) { 79 | return false; 80 | } 81 | return true; 82 | } 83 | 84 | @Override 85 | public String toString() { 86 | return "Region{" + 87 | "regionId=" + regionId + 88 | ", regionName='" + regionName + '\'' + 89 | '}'; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /GS_JPA_8_NamedQuery/src/main/java/edu/javacourse/jpa/manager/RegionManager.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.manager; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import java.util.List; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.EntityManagerFactory; 7 | import javax.persistence.Persistence; 8 | import javax.persistence.criteria.CriteriaBuilder; 9 | import javax.persistence.criteria.CriteriaQuery; 10 | import javax.persistence.criteria.Root; 11 | 12 | public class RegionManager { 13 | 14 | private EntityManagerFactory entityManagerFactory; 15 | private EntityManager entityManager; 16 | 17 | public void init() { 18 | entityManagerFactory = Persistence.createEntityManagerFactory("unitEclipseLink", new java.util.HashMap()); 19 | System.out.println(entityManagerFactory.getClass().getSimpleName()); 20 | entityManager = entityManagerFactory.createEntityManager(); 21 | System.out.println(entityManager.getClass().getSimpleName()); 22 | } 23 | 24 | public List getRegionList() { 25 | return entityManager.createQuery("select r from Region r").getResultList(); 26 | } 27 | 28 | public EntityManager getEntityManager() { 29 | return entityManager; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /GS_JPA_8_NamedQuery/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | org.eclipse.persistence.jpa.PersistenceProvider 7 | edu.javacourse.jpa.entity.City 8 | edu.javacourse.jpa.entity.Region 9 | edu.javacourse.jpa.entity.Author 10 | edu.javacourse.jpa.entity.Book 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /GS_JPA_9_Criteria/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | edu.javacourse 6 | jpa-criteria 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | http://maven.apache.org 11 | 12 | 13 | 14 | 15 | 16 | 17 | mysql 18 | mysql-connector-java 19 | 5.1.18 20 | 21 | 22 | 23 | org.eclipse.persistence 24 | org.eclipse.persistence.jpa 25 | 2.5.0 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /GS_JPA_9_Criteria/readme.txt: -------------------------------------------------------------------------------- 1 | http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Criteria 2 | 3 | http://www.ibm.com/developerworks/ru/library/j-typesafejpa/ -------------------------------------------------------------------------------- /GS_JPA_9_Criteria/readme.xml: -------------------------------------------------------------------------------- 1 | http://docs.oracle.com/javaee/6/tutorial/doc/gjrij.html -------------------------------------------------------------------------------- /GS_JPA_9_Criteria/src/main/java/edu/javacourse/jpa/SimpleExample.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa; 2 | 3 | import edu.javacourse.jpa.entity.Author; 4 | import edu.javacourse.jpa.entity.Book; 5 | import edu.javacourse.jpa.entity.City; 6 | import edu.javacourse.jpa.entity.Region; 7 | import edu.javacourse.jpa.manager.RegionManager; 8 | 9 | import javax.persistence.EntityManager; 10 | import javax.persistence.Query; 11 | import javax.persistence.TypedQuery; 12 | import javax.persistence.criteria.*; 13 | import java.util.Collection; 14 | import java.util.List; 15 | 16 | public class SimpleExample { 17 | 18 | public static void main(String[] args) { 19 | RegionManager rm = new RegionManager(); 20 | rm.init(); 21 | EntityManager em = rm.getEntityManager(); 22 | 23 | CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); 24 | 25 | //test1(em, criteriaBuilder); 26 | 27 | test2(em, criteriaBuilder); 28 | 29 | test3(em, criteriaBuilder); 30 | 31 | 32 | 33 | 34 | } 35 | 36 | 37 | 38 | private static void test3(EntityManager em, CriteriaBuilder cb) { 39 | CriteriaQuery criteriaQuery = cb.createQuery(Region.class); 40 | Root root = criteriaQuery.from(Region.class); 41 | criteriaQuery.select(root); 42 | 43 | 44 | Expression> cities = root.get("cityList"); 45 | 46 | criteriaQuery.where(cb.greaterThan(cb.size(cities), 1)); 47 | 48 | // create query via EM 49 | Query query = em.createQuery(criteriaQuery); 50 | List regions = query.getResultList(); 51 | printRegions(regions); 52 | } 53 | 54 | private static void test2(EntityManager em, CriteriaBuilder cb) { 55 | 56 | CriteriaQuery criteriaQuery = cb.createQuery(Book.class); 57 | Root root = criteriaQuery.from(Book.class); 58 | criteriaQuery.select(root); 59 | criteriaQuery.where(cb.equal(root.get("bookId"), 1)); 60 | TypedQuery q = em.createQuery(criteriaQuery); 61 | List books = q.getResultList(); 62 | printBooks(books); 63 | } 64 | 65 | private static void test1(EntityManager em, CriteriaBuilder cb) { 66 | CriteriaQuery criteriaQuery = cb.createQuery(Region.class); 67 | Root root = criteriaQuery.from(Region.class); 68 | criteriaQuery.select(root); 69 | TypedQuery q = em.createQuery(criteriaQuery); 70 | List allRegions = q.getResultList(); 71 | printRegions(allRegions); 72 | } 73 | 74 | private static void printBooks(List books) { 75 | for (Book book : books) { 76 | System.out.println(book.getBookName()); 77 | } 78 | } 79 | 80 | private static void printAuthors(List authors) { 81 | for (Author author : authors) { 82 | System.out.println(author.getAuthorName()); 83 | } 84 | System.out.println("============================================"); 85 | } 86 | 87 | private static void printCities(List cities) { 88 | for (City city : cities) { 89 | System.out.println(city.getRegion().getRegionName() + " " + city.getCityName()); 90 | } 91 | System.out.println("============================================"); 92 | } 93 | 94 | private static void printRegions(List regions) { 95 | for (Region region : regions) { 96 | System.out.println(region.getRegionName()); 97 | } 98 | System.out.println("============================================"); 99 | } 100 | 101 | 102 | } 103 | -------------------------------------------------------------------------------- /GS_JPA_9_Criteria/src/main/java/edu/javacourse/jpa/entity/Author.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import javax.persistence.*; 4 | import java.io.Serializable; 5 | import java.util.HashSet; 6 | import java.util.Set; 7 | 8 | @Entity 9 | @Table(name = "jc_author") 10 | @NamedQueries({ 11 | @NamedQuery(name = "Author.ById", query = "select a from Author as a where a.authorId =:id") 12 | }) 13 | public class Author implements Serializable { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | @Column(name = "author_id") 18 | private Long authorId; 19 | @Column(name = "author_name") 20 | private String authorName; 21 | 22 | @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY) 23 | @JoinTable(name = "jc_book_author", 24 | joinColumns = @JoinColumn(name = "author_id"), 25 | inverseJoinColumns = @JoinColumn(name = "book_id")) 26 | @OrderBy(value="bookName") 27 | private Set bookList; 28 | 29 | public Long getAuthorId() { 30 | return authorId; 31 | } 32 | 33 | public void setAuthorId(Long authorId) { 34 | this.authorId = authorId; 35 | } 36 | 37 | public String getAuthorName() { 38 | return authorName; 39 | } 40 | 41 | public void setAuthorName(String authorName) { 42 | this.authorName = authorName; 43 | } 44 | 45 | public Set getBookList() { 46 | return bookList; 47 | } 48 | 49 | public void setBookList(Set bookList) { 50 | this.bookList = bookList; 51 | } 52 | 53 | public void addBook(Book book) { 54 | if (bookList == null) { 55 | bookList = new HashSet(); 56 | } 57 | bookList.add(book); 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "Author{" + "authorId=" + authorId + ", authorName=" + authorName + '}'; 63 | } 64 | 65 | @Override 66 | public boolean equals(Object obj) { 67 | if (obj == null) { 68 | return false; 69 | } 70 | if (getClass() != obj.getClass()) { 71 | return false; 72 | } 73 | final Author other = (Author) obj; 74 | if (this.authorId != other.authorId && (this.authorId == null || !this.authorId.equals(other.authorId))) { 75 | return false; 76 | } 77 | return true; 78 | } 79 | 80 | @Override 81 | public int hashCode() { 82 | int hash = 7; 83 | hash = 71 * hash + (this.authorId != null ? this.authorId.hashCode() : 0); 84 | return hash; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /GS_JPA_9_Criteria/src/main/java/edu/javacourse/jpa/entity/Book.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | import javax.persistence.*; 7 | 8 | @Entity 9 | @Table(name = "jc_book") 10 | @NamedQueries({ 11 | @NamedQuery(name = "Book.ByName", query = "select b from Book as b where b.bookName =:name"), 12 | @NamedQuery(name = "Book.ByAuthor", query = "select b from Book as b, Author a where b member of a.bookList and a.authorName =:Author") 13 | }) 14 | public class Book implements Serializable { 15 | 16 | @Id 17 | @GeneratedValue(strategy = GenerationType.IDENTITY) 18 | @Column(name = "book_id") 19 | private Long bookId; 20 | @Column(name = "book_name") 21 | private String bookName; 22 | 23 | @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY) 24 | @JoinTable(name = "jc_book_author", 25 | joinColumns = @JoinColumn(name = "book_id"), 26 | inverseJoinColumns = @JoinColumn(name = "author_id")) 27 | @OrderBy(value="authorName") 28 | private Set authorList; 29 | 30 | public Set getAuthorList() { 31 | return authorList; 32 | } 33 | 34 | public void setAuthorList(Set authorList) { 35 | this.authorList = authorList; 36 | } 37 | 38 | public void addAuthor(Author author) { 39 | if (authorList == null) { 40 | authorList = new HashSet(); 41 | } 42 | authorList.add(author); 43 | } 44 | 45 | public Long getBookId() { 46 | return bookId; 47 | } 48 | 49 | public void setBookId(Long bookId) { 50 | this.bookId = bookId; 51 | } 52 | 53 | public String getBookName() { 54 | return bookName; 55 | } 56 | 57 | public void setBookName(String bookName) { 58 | this.bookName = bookName; 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | return "Book{" + "bookId=" + bookId + ", bookName=" + bookName + '}'; 64 | } 65 | 66 | @Override 67 | public boolean equals(Object obj) { 68 | if (obj == null) { 69 | return false; 70 | } 71 | if (getClass() != obj.getClass()) { 72 | return false; 73 | } 74 | final Book other = (Book) obj; 75 | if (this.bookId != other.bookId && (this.bookId == null || !this.bookId.equals(other.bookId))) { 76 | return false; 77 | } 78 | return true; 79 | } 80 | 81 | @Override 82 | public int hashCode() { 83 | int hash = 7; 84 | hash = 71 * hash + (this.bookId != null ? this.bookId.hashCode() : 0); 85 | return hash; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /GS_JPA_9_Criteria/src/main/java/edu/javacourse/jpa/entity/City.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import javax.persistence.Column; 5 | import javax.persistence.Entity; 6 | import javax.persistence.FetchType; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.JoinColumn; 11 | import javax.persistence.ManyToOne; 12 | import javax.persistence.Table; 13 | 14 | @Entity 15 | @Table(name = "jc_city") 16 | public class City implements Serializable { 17 | 18 | @Id 19 | @GeneratedValue(strategy = GenerationType.IDENTITY) 20 | @Column(name = "city_id") 21 | private Integer cityId; 22 | @Column(name = "city_name") 23 | private String cityName; 24 | 25 | @JoinColumn(name = "region_id", referencedColumnName = "region_id") 26 | @ManyToOne(optional = false, fetch = FetchType.LAZY) 27 | private Region region; 28 | 29 | public City() { 30 | } 31 | 32 | public City(Integer cityId) { 33 | this.cityId = cityId; 34 | } 35 | 36 | public City(Integer cityId, String cityName) { 37 | this.cityId = cityId; 38 | this.cityName = cityName; 39 | } 40 | 41 | public Integer getCityId() { 42 | return cityId; 43 | } 44 | 45 | public void setCityId(Integer cityId) { 46 | this.cityId = cityId; 47 | } 48 | 49 | public String getCityName() { 50 | return cityName; 51 | } 52 | 53 | public void setCityName(String cityName) { 54 | this.cityName = cityName; 55 | } 56 | 57 | public Region getRegion() { 58 | return region; 59 | } 60 | 61 | public void setRegion(Region region) { 62 | this.region = region; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | int hash = 0; 68 | hash += (cityId != null ? cityId.hashCode() : 0); 69 | return hash; 70 | } 71 | 72 | @Override 73 | public boolean equals(Object object) { 74 | // TODO: Warning - this method won't work in the case the id fields are not set 75 | if (!(object instanceof City)) { 76 | return false; 77 | } 78 | City other = (City) object; 79 | if ((this.cityId == null && other.cityId != null) || (this.cityId != null && !this.cityId.equals(other.cityId))) { 80 | return false; 81 | } 82 | return true; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "edu.javacourse.jpa.entity.City[ cityId=" + cityId + " ]"; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /GS_JPA_9_Criteria/src/main/java/edu/javacourse/jpa/entity/Region.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.entity; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import javax.persistence.*; 6 | 7 | @Entity 8 | @Table(name = "jc_region") 9 | @NamedQueries({ 10 | @NamedQuery(name = "Region.DelebeById", query = "delete from Region r where r.regionId =:id"), 11 | @NamedQuery(name = "Region.Count", query = "select count (r) from Region r") 12 | }) 13 | public class Region implements Serializable { 14 | 15 | @Id 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | @Column(name = "region_id") 18 | private Integer regionId; 19 | @Column(name = "region_name") 20 | private String regionName; 21 | @OneToMany(cascade = CascadeType.ALL, mappedBy = "region", fetch = FetchType.LAZY) 22 | private List cityList; 23 | 24 | public Region() { 25 | } 26 | 27 | public Region(Integer regionId) { 28 | this.regionId = regionId; 29 | } 30 | 31 | public Region(String regionName) { 32 | this.regionName = regionName; 33 | } 34 | 35 | public Region(Integer regionId, String regionName) { 36 | this.regionId = regionId; 37 | this.regionName = regionName; 38 | } 39 | 40 | public Integer getRegionId() { 41 | return regionId; 42 | } 43 | 44 | public void setRegionId(Integer regionId) { 45 | this.regionId = regionId; 46 | } 47 | 48 | public String getRegionName() { 49 | return regionName; 50 | } 51 | 52 | public void setRegionName(String regionName) { 53 | this.regionName = regionName; 54 | } 55 | 56 | public List getCityList() { 57 | return cityList; 58 | } 59 | 60 | public void setCityList(List cityList) { 61 | this.cityList = cityList; 62 | } 63 | 64 | @Override 65 | public int hashCode() { 66 | int hash = 0; 67 | hash += (regionId != null ? regionId.hashCode() : 0); 68 | return hash; 69 | } 70 | 71 | @Override 72 | public boolean equals(Object object) { 73 | // TODO: Warning - this method won't work in the case the id fields are not set 74 | if (!(object instanceof Region)) { 75 | return false; 76 | } 77 | Region other = (Region) object; 78 | if ((this.regionId == null && other.regionId != null) || (this.regionId != null && !this.regionId.equals(other.regionId))) { 79 | return false; 80 | } 81 | return true; 82 | } 83 | 84 | @Override 85 | public String toString() { 86 | return "Region{" + 87 | "regionId=" + regionId + 88 | ", regionName='" + regionName + '\'' + 89 | '}'; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /GS_JPA_9_Criteria/src/main/java/edu/javacourse/jpa/manager/RegionManager.java: -------------------------------------------------------------------------------- 1 | package edu.javacourse.jpa.manager; 2 | 3 | import edu.javacourse.jpa.entity.Region; 4 | import java.util.List; 5 | import javax.persistence.EntityManager; 6 | import javax.persistence.EntityManagerFactory; 7 | import javax.persistence.Persistence; 8 | import javax.persistence.criteria.CriteriaBuilder; 9 | import javax.persistence.criteria.CriteriaQuery; 10 | import javax.persistence.criteria.Root; 11 | 12 | public class RegionManager { 13 | 14 | private EntityManagerFactory entityManagerFactory; 15 | private EntityManager entityManager; 16 | 17 | public void init() { 18 | entityManagerFactory = Persistence.createEntityManagerFactory("unitEclipseLink", new java.util.HashMap()); 19 | System.out.println(entityManagerFactory.getClass().getSimpleName()); 20 | entityManager = entityManagerFactory.createEntityManager(); 21 | System.out.println(entityManager.getClass().getSimpleName()); 22 | } 23 | 24 | public List getRegionList() { 25 | return entityManager.createQuery("select r from Region r").getResultList(); 26 | } 27 | 28 | public EntityManager getEntityManager() { 29 | return entityManager; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /GS_JPA_9_Criteria/src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | org.eclipse.persistence.jpa.PersistenceProvider 7 | edu.javacourse.jpa.entity.City 8 | edu.javacourse.jpa.entity.Region 9 | edu.javacourse.jpa.entity.Author 10 | edu.javacourse.jpa.entity.Book 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | --------------------------------------------------------------------------------