11package com .jpa .example ;
22
3+ // import jakarta.json.Json;
4+ // import jakarta.json.JsonValue;
35import jakarta .persistence .*;
46import jakarta .persistence .criteria .CriteriaBuilder ;
57import jakarta .persistence .criteria .CriteriaQuery ;
1012// import java.nio.charset.StandardCharsets;
1113import java .io .InputStream ;
1214import java .time .LocalDate ;
15+ import java .util .ArrayList ;
1316import java .util .Arrays ;
1417//import java.util.List;
18+ import java .util .HashMap ;
19+ import java .util .List ;
1520
21+ //import com.fasterxml.jackson.databind.util.JSONPObject;
1622import com .jpa .example .models .Author ;
1723import com .jpa .example .models .Book ;
24+ import com .jpa .example .models .EntityA ;
25+ import com .jpa .example .models .EntityB ;
1826import com .jpa .example .models .MyObject ;
1927
2028public class Application {
@@ -51,26 +59,73 @@ public static void main(String[] args) {
5159
5260 }
5361
62+ {
63+ EntityManagerFactory emf = Persistence .createEntityManagerFactory ("persistence" );
64+ try {
65+ persistEntity (emf );
66+ nativeQueries (emf );
67+ loadEntityA (emf );
68+ loadEntityB (emf );
69+ }
70+
71+ finally {
72+ emf .close ();
73+ }
74+ System .out .println ("Press Enter to continue" );
75+
76+ try {
77+
78+ System .in .read ();
79+
80+ }
81+ catch ( Exception ex ){
82+
83+ //
84+
85+ }
86+
87+ }
88+
5489 EntityManagerFactory emf = Persistence .createEntityManagerFactory ("persistence" );
5590 EntityManager entityManager = emf .createEntityManager ();
5691 entityManager .getTransaction ().begin ();
5792
58- Author author = new Author ();
59- author .Name = "Tomas Moreno" ;
60- author .BirthYear = 1979 ;
61- entityManager .persist (author );
62-
63- //var id = author.Id;
93+ Author author01 = new Author ();
94+ author01 .Name = "Tomas Moreno" ;
95+ author01 .BirthYear = 1979 ;
96+ author01 .bookList = new ArrayList <>();
97+ author01 .ExtraData = new HashMap <>();
98+ author01 .ExtraData .put ( "Field1" , "Value1" );
99+ author01 .ExtraData .put ( "Field2" , "Value2" );
100+ //entityManager.persist(author01);
101+
102+ Book book01 = new Book ();
103+ book01 .Title = "GraalVM/Native-Image the future of Java" ;
104+ book01 .PubDate = LocalDate .now ();
105+ book01 .Author = author01 ;
106+ //book01.setAuthor( author01 );
107+ entityManager .persist (book01 );
108+
109+ Book book02 = new Book ();
110+ book02 .Title = "Java 2017" ;
111+ book02 .PubDate = LocalDate .now ();
112+ book02 .Author = author01 ;
113+ //book02.setAuthor( author01 );
114+ entityManager .persist (book02 );
115+
116+ author01 .bookList = Arrays .asList ( book01 , book02 );
117+ //author01.setBookList( Arrays.asList( book01, book02 ) );
118+ entityManager .persist (author01 );
64119
65- findAuthorById ( entityManager , author .Id );
120+ findAuthorById ( entityManager , author01 .Id );
121+ findBookById ( entityManager , book01 .Id );
66122
67- Book book = new Book ();
68- book .Title = "GraalVM/Native-Image the future of Java" ;
69- book .PubDate = LocalDate .now ();
70- book .Author = author ;
71- entityManager .persist (book );
123+ Author author02 = new Author ();
124+ author02 .Name = "Loly Gomez" ;
125+ author02 .BirthYear = 1980 ;
126+ entityManager .persist (author02 );
72127
73- findBookById ( entityManager , book .Id ) ;
128+ //var id = author .Id;
74129
75130 MyObject myObject = new MyObject ();
76131 myObject .Data = "one" ;
@@ -139,12 +194,14 @@ private static void findAuthorById(EntityManager entityManager, String id ) {
139194 System .out .println ("----\n finding author by id" );
140195 var o = entityManager .find (Author .class , id ); //2L);
141196 System .out .println (o );
197+ System .out .println ( "book list =>" + o .bookList .toString () );
142198 }
143199
144200 private static void findBookById (EntityManager entityManager , String id ) {
145201 System .out .println ("----\n finding book by id" );
146202 var o = entityManager .find (Book .class , id ); //2L);
147203 System .out .println (o );
204+ System .out .println ( "author =>" + o .Author .toString () );
148205 }
149206
150207 private static void findObjectById (EntityManager entityManager , String id ) {
@@ -193,4 +250,74 @@ private static void criteriaQuery(EntityManager entityManager) {
193250 System .out .println (typedQuery .getResultList ());
194251
195252 }
253+
254+ public static void nativeQuery (EntityManager em , String s ) {
255+ System .out .printf ("-----------------------------%n'%s'%n" , s );
256+ Query query = em .createNativeQuery (s );
257+ List <?> list = query .getResultList ();
258+ for (Object o : list ) {
259+ if (o instanceof Object []) {
260+ System .out .println (Arrays .toString ((Object []) o ));
261+ }else {
262+ System .out .println (o );
263+ }
264+ }
265+ }
266+
267+ private static void nativeQueries (EntityManagerFactory emf ) {
268+ System .out .println ("-- nativeQueries --" );
269+ EntityManager em = emf .createEntityManager ();
270+ Application .nativeQuery (em , "Select * from EntityA" );
271+ Application .nativeQuery (em , "Select * from EntityB" );
272+ }
273+
274+ private static void persistEntity (EntityManagerFactory emf ) {
275+ System .out .println ("-- persistEntity --" );
276+ EntityManager em = emf .createEntityManager ();
277+
278+ EntityB entityB1 = new EntityB ();
279+ entityB1 .setStrB ("testStringB" );
280+
281+ EntityB entityB2 = new EntityB ();
282+ entityB2 .setStrB ("testStringB2" );
283+
284+ EntityA entityA = new EntityA ();
285+ entityA .setStrA ("testStringA" );
286+ entityA .setEntityBList (Arrays .asList (entityB1 , entityB2 ));
287+
288+ entityB1 .setRefEntityA (entityA );
289+ entityB2 .setRefEntityA (entityA );
290+
291+ System .out .println ("-- persisting entities --" );
292+ System .out .printf (" %s%n entityA#entityBList: %s%n" , entityA , entityA .getEntityBList ());
293+ System .out .printf (" %s%n entityB1#refEntityA: %s%n" , entityB1 , entityB1 .getRefEntityA ());
294+ System .out .printf (" %s%n entityB2#refEntityA: %s%n" , entityB2 , entityB2 .getRefEntityA ());
295+
296+ em .getTransaction ().begin ();
297+ em .persist (entityA );
298+ em .persist (entityB1 );
299+ em .persist (entityB2 );
300+ em .getTransaction ().commit ();
301+
302+ em .close ();
303+ }
304+
305+ @ SuppressWarnings ("unchecked" )
306+ private static void loadEntityA (EntityManagerFactory emf ) {
307+ System .out .println ("-- loadEntityA --" );
308+ EntityManager em = emf .createEntityManager ();
309+ List <EntityA > entityAList = em .createQuery ("Select t from EntityA t" ).getResultList ();
310+ entityAList .forEach (e -> System .out .printf (" %s%n entityA#entityBList: %s%n" , e , e .getEntityBList ()));
311+ em .close ();
312+ }
313+
314+ @ SuppressWarnings ("unchecked" )
315+ private static void loadEntityB (EntityManagerFactory emf ) {
316+ System .out .println ("-- loadEntityB --" );
317+ EntityManager em = emf .createEntityManager ();
318+ List <EntityB > entityBList = em .createQuery ("Select t from EntityB t" ).getResultList ();
319+ entityBList .forEach (e -> System .out .printf (" %s%n entityB#refEntityA: %s%n" , e , e .getRefEntityA ()));
320+ em .close ();
321+ }
322+
196323}
0 commit comments