How to install, git clone, build and test
TweetPosted on Thursday Feb 20, 2014 at 06:01PM in Technology
Just try to make Jenkins work, and do some typical works (git clone, build, test) with Git and Maven.
Environment
- Jenkins 1.551
- Apache Maven 3.1.1
- git version 1.8.3.4 (Apple Git-47)
- Oracle JDK7u51
- OS X 10.9.1
Download and Install
- OS X Native package is available at [1].
- The package is a pkg file.
- Install is easy. we can just open the package and follow instructions.
- When installation finishes, Jenkins will run automatically at port 8080 and browser will open and going to show localhost:8080, so we have to stop any application servers ran at port 8080 before we start installation.
- If you missed that point, Jenkins will run automatically after you have stop the application server.
- Followed this instruction, Jenkins will be installed as a service of OS X so it will be launched every time at system boot.
Configuration of platform specific matters
Change ports of Jenkins
- I have been used port 8080 for application server, so I don't want to give it to Jenkins.
- According to [3], this will make Jenkins to use other ports on OS X.
sudo defaults write /Library/Preferences/org.jenkins-ci httpPort 18080 sudo defaults write /Library/Preferences/org.jenkins-ci ajp13Port 18009
Avoid garbled characters for Multi-byte environment
- This is for Japanese OS X environment
- Add this line to “/Library/Application Support/Jenkins/jenkins-runner.sh”
export LANG=ja_JP.UTF-8
Use English as UI Language
- As default, Jenkins use browser's language for UI.
- Locale Plugin[7] allows to specify language.
- I prefer English at Jenkins so I have installed it.
- After plugin installed, go to Configure System
- Enter “ENGLISH” to Default Language
- Check “Ignore browser preference and force this language to all users”
- Click “Save” in the bottom of the page
Restart Jenkins
- To affect these changes, we have to restart Jenkins.
sudo launchctl stop org.jenkins-ci sudo launchctl start org.jenkins-ci
- Now we got to run Jenkins at 18080 port.
Configuration
Make sure that “git” and “mvn” command are installed
kyle-no-MacBook:~ kyle$ which git /usr/bin/git kyle-no-MacBook:~ kyle$ git --version git version 1.8.3.4 (Apple Git-47) kyle-no-MacBook:~ kyle$ which mvn /Users/kyle/apps/apache-maven-3.1.1/bin/mvn kyle-no-MacBook:~ kyle$ mvn --version Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8 Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-18 00:22:22+0900) Maven home: /Users/kyle/apps/apache-maven-3.1.1 Java version: 1.7.0_51, vendor: Oracle Corporation Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre Default locale: ja_JP, platform encoding: UTF-8 OS name: "mac os x", version: "10.9.1", arch: "x86_64", family: "mac" kyle-no-MacBook:~ kyle$
Install the Git Plugin of Jenkins
- Open the Jenkins page.
- Click “Jenkinsの管理” (in Japanese messages)
- Click “プラグインの管理”
- Click “利用可能”
- Enter “git plugin” to filter field.
- Check “Git Plugin”
- Click “ダウンロードして再起動後にインストール”
- Installation progress page will be shown.
- It seems to a static page at first, but it updates on the progress for dynamically so just wait for a while.
- It takes several minutes for complete.
- Confirm all processes are completed
- Restart the Jenkins.
Set JAVA_HOME
- Output of this command would be help for OS X users.
kyle-no-MacBook:~ kyle$ /usr/libexec/java_home -V Matching Java Virtual Machines (3): 1.7.0_51, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home 1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home 1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home kyle-no-MacBook:~ kyle$
- Click “Jenkinsの管理”
- Click “システムの設定”
- Find JDK section.
- Click “JDK追加”
- Uncheck “自動インストール”
- Enter “名前”
- Enter “JAVA_HOME”
- Click “保存” at the bottom of the page.
Set Path to Maven executable
- Click “Jenkinsの管理”
- Click “システムの設定”
- Find Maven section.
- Click “Maven追加”
- Uncheck “自動インストール”
- Enter MAVEN_HOME
- Click “保存” at the bottom of the page.
Make a job
- At first, we assume that a example maven project is committed to a local repository. it is better if the project contains some JUnit test classes. I have created a project which contains a servlet, a CDI managed bean and a test class for it.
- This job will do:
- Retrieve the source tree of a maven project from the Git repository in local filesystem
- Build a WAR file
- Run JUnit test
Procedures:
- Click “新規ジョブ作成”
- Enter “MyJob001” to “ジョブ名”
- Check “Maven2/3プロジェクトのビルド”
- Click “OK”
- Find “ソースコード管理” section.
- Check “Git”
- Enter “Repository URL”
- Find “ビルド” Section
- Enter “ルートPOM'
- Enter “ゴールとオプション”
- Find “ビルド後の処理” Section
- Click “ビルド後の処理の追加”
- Click “成果物を保存”
- Enter “保存するファイル”
- Click “保存”
- Now I got a job “MyJob001”
Build
- Open the job “MyJob001”
- Click “ビルド実行”
- Wait for a while.
- It takes really long time for the first time because there are many new dependencies. maybe we should check the console output of Jenkins.
- Some minutes later, we got to see fine mark in the dashboard.
- Detail page is interesting, we can see that all tests are passed and deliverables at once.
- Check the WAR file which have built by Jenkins.
kyle-no-MacBook:hellojenkins kyle$ unzip -l /Users/Shared/Jenkins/Home/jobs/MyJob001/lastSuccessful/org.nailedtothex\$hellojenkins/archive/org.nailedtothex/hellojenkins/0.0.1-SNAPSHOT/hellojenkins-0.0.1-SNAPSHOT.war Archive: /Users/Shared/Jenkins/Home/jobs/MyJob001/lastSuccessful/org.nailedtothex$hellojenkins/archive/org.nailedtothex/hellojenkins/0.0.1-SNAPSHOT/hellojenkins-0.0.1-SNAPSHOT.war Length Date Time Name -------- ---- ---- ---- 0 02-21-14 11:24 META-INF/ 132 02-21-14 11:24 META-INF/MANIFEST.MF 0 02-21-14 11:24 WEB-INF/ 0 02-21-14 11:24 WEB-INF/classes/ 0 02-21-14 11:24 WEB-INF/classes/hellojenkins/ 477 02-21-14 11:24 WEB-INF/classes/hellojenkins/HelloBean.class 1169 02-21-14 11:24 WEB-INF/classes/hellojenkins/HelloServlet.class 0 02-21-14 11:24 META-INF/maven/ 0 02-21-14 11:24 META-INF/maven/org.nailedtothex/ 0 02-21-14 11:24 META-INF/maven/org.nailedtothex/hellojenkins/ 1053 02-21-14 11:21 META-INF/maven/org.nailedtothex/hellojenkins/pom.xml 122 02-21-14 11:24 META-INF/maven/org.nailedtothex/hellojenkins/pom.properties -------- ------- 2953 12 files kyle-no-MacBook:hellojenkins kyle$
Remarks
- I would also try that other operations such as automated execution or deploy later.
References
- Welcome to Jenkins CI! | Jenkins CI
- Jenkins を Mac で使う | Hazi.tech
- Jenkins Mac OS X InstallerでJenkinsを入れる - IwazerReport
- Jenkins の git plugin と git-client plugin の相性に注意 - diary.sorah
- Git Plugin の導入に躓いた - おこらない日記
- githubとAndroidとJenkinsの素敵な関係 | GeNERACE labo
- Locale Plugin - Jenkins - Jenkins Wiki
- The Locale plugin - Howto
Tags: jenkins
Just try to use 2nd level cache with WildFly and Hibernate
TweetPosted on Wednesday Feb 19, 2014 at 05:30PM in Technology
Just playing with 2nd level cache that mentioned at [1]. it is a good tutorial so may we only need to refer [1], and this is my notes and logs only.
Environment
- WildFly 8.0.0.Final (Hibernate 4.3.1)
- Maven 3.1.1
- Oracle JDK7u51
- OS X 10.9.1
Run Maven to get the sample project
kyle-no-MacBook:l2cache kyle$ mvn archetype:generate \ > -DarchetypeArtifactId=jboss-javaee6-webapp-archetype \ > -DarchetypeGroupId=org.jboss.spec.archetypes \ > -DarchetypeVersion=7.1.2.Final \ > -DgroupId=de.consol.research \ > -DartifactId=infinispan-jpa-example \ > -Dversion=1.0-SNAPSHOT \ > -Dname="Infinispan JPA Example" \ > -DarchetypeCatalog=http://search.maven.org/remotecontent?filepath=archetype-catalog.xml \ > -DinteractiveMode=false Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8 [INFO] Scanning for projects... Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom (7 KB at 5.4 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-deploy-plugin/2.7/maven-deploy-plugin-2.7.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-deploy-plugin/2.7/maven-deploy-plugin-2.7.pom (6 KB at 20.5 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-site-plugin/3.3/maven-site-plugin-3.3.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-site-plugin/3.3/maven-site-plugin-3.3.pom (21 KB at 51.1 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-site-plugin/3.3/maven-site-plugin-3.3.jar Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-site-plugin/3.3/maven-site-plugin-3.3.jar (122 KB at 180.7 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.pom (5 KB at 17.0 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.jar Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.jar (24 KB at 84.3 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.pom (15 KB at 55.4 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/16/maven-plugins-16.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/16/maven-plugins-16.pom (13 KB at 47.0 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.jar Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-assembly-plugin/2.2-beta-5/maven-assembly-plugin-2.2-beta-5.jar (204 KB at 357.8 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.pom Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.pom (12 KB at 41.7 KB/sec) Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.jar Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-dependency-plugin/2.8/maven-dependency-plugin-2.8.jar (150 KB at 274.5 KB/sec) [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<< [INFO] [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --- Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/2.0.8/maven-reporting-api-2.0.8.jar Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/doxia/doxia-sink-api/1.0-alpha-9/doxia-sink-api-1.0-alpha-9.jar Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-5/plexus-interactivity-api-1.0-alpha-5.jar Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/2.0.8/maven-reporting-api-2.0.8.jar (10 KB at 28.5 KB/sec) Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/doxia/doxia-sink-api/1.0-alpha-9/doxia-sink-api-1.0-alpha-9.jar (10 KB at 19.8 KB/sec) Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-5/plexus-interactivity-api-1.0-alpha-5.jar (14 KB at 26.0 KB/sec) [INFO] Generating project in Batch mode [INFO] Archetype repository missing. Using the one from [org.jboss.spec.archetypes:jboss-javaee6-webapp-archetype:7.1.3.Final] found in catalog http://search.maven.org/remotecontent?filepath=archetype-catalog.xml Downloading: http://search.maven.org/org/jboss/spec/archetypes/jboss-javaee6-webapp-archetype/7.1.2.Final/jboss-javaee6-webapp-archetype-7.1.2.Final.jar Downloading: http://repo.maven.apache.org/maven2/org/jboss/spec/archetypes/jboss-javaee6-webapp-archetype/7.1.2.Final/jboss-javaee6-webapp-archetype-7.1.2.Final.jar Downloaded: http://repo.maven.apache.org/maven2/org/jboss/spec/archetypes/jboss-javaee6-webapp-archetype/7.1.2.Final/jboss-javaee6-webapp-archetype-7.1.2.Final.jar (96 KB at 88.0 KB/sec) Downloading: http://search.maven.org/org/jboss/spec/archetypes/jboss-javaee6-webapp-archetype/7.1.2.Final/jboss-javaee6-webapp-archetype-7.1.2.Final.pom Downloading: http://repo.maven.apache.org/maven2/org/jboss/spec/archetypes/jboss-javaee6-webapp-archetype/7.1.2.Final/jboss-javaee6-webapp-archetype-7.1.2.Final.pom Downloaded: http://repo.maven.apache.org/maven2/org/jboss/spec/archetypes/jboss-javaee6-webapp-archetype/7.1.2.Final/jboss-javaee6-webapp-archetype-7.1.2.Final.pom (3 KB at 5.5 KB/sec) [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: jboss-javaee6-webapp-archetype:7.1.2.Final [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: de.consol.research [INFO] Parameter: artifactId, Value: infinispan-jpa-example [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: de.consol.research [INFO] Parameter: packageInPathFormat, Value: de/consol/research [INFO] Parameter: package, Value: de.consol.research [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: name, Value: Infinispan JPA Example [INFO] Parameter: groupId, Value: de.consol.research [INFO] Parameter: jboss-bom-enterprise-version, Value: 1.0.4.Final-redhat-1 [INFO] Parameter: artifactId, Value: infinispan-jpa-example [INFO] Parameter: enterprise, Value: false [INFO] project created from Archetype in dir: /Users/kyle/src/l2cache/infinispan-jpa-example [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 18.514s [INFO] Finished at: Wed Feb 19 17:26:26 JST 2014 [INFO] Final Memory: 14M/245M [INFO] ------------------------------------------------------------------------
Import the project to Eclipse
- File - Import - Existing Maven Projects
- It comes with no errors.
Make the application to use PostgreSQL
This is not necessary for everyone. It comes with internal H2 datasource definition in the application so we can just skip this work.
Create a database
jbatch=# create database infinispan owner kyle encoding 'UTF8' TEMPLATE template0; CREATE DATABASE jbatch=#
Define a data source
data-source add \ --name=InfinispanExampleDS \ --driver-name=postgresql \ --connection-url=jdbc:postgresql://localhost:5432/infinispan \ --jndi-name=java:jboss/datasources/infinispan-jpa-exampleDS \ --user-name=postgres \ --password=***
Delete datasource definition in WEB-INF
delete Deployed Resources/webapp/WEB-INF/infinispan-jpa-example-ds.xml
Deploy
I did it with Eclipse.
- Open Servers Window
- Right-click WildFly
- Add and remove
- Add infinispan-jpa-example
- Finish
Browse JSF page
- Browse here
- When we reload the page, every time it executes SELECT clause. like this:
17:46:37,631 DEBUG [org.hibernate.SQL] (default task-11) select member0_.id as id1_0_, member0_.email as email2_0_, member0_.name as name3_0_, member0_.phone_number as phone_nu4_0_ from Member member0_ order by member0_.name asc 17:46:39,055 DEBUG [org.hibernate.SQL] (default task-12) select member0_.id as id1_0_, member0_.email as email2_0_, member0_.name as name3_0_, member0_.phone_number as phone_nu4_0_ from Member member0_ order by member0_.name asc 17:49:13,527 DEBUG [org.hibernate.SQL] (default task-10) select member0_.id as id1_0_, member0_.email as email2_0_, member0_.name as name3_0_, member0_.phone_number as phone_nu4_0_ from Member member0_ order by member0_.name asc
- One test data is already registered when we browse the page.
Send GET request
kyle-no-MacBook:infinispan-jpa-example kyle$ curl http://localhost:8080/infinispan-jpa-example/rest/members/0 <?xml version="1.0" encoding="UTF-8" standalone="yes"?><member><email>john.smith@mailinator.com</email><id>0</id><name>John Smith</name><phoneNumber>2125551212</phoneNumber></member>kyle-no-MacBook:infinispan-jpa-example kyle$
It executes SQL for every request.
17:55:28,664 DEBUG [org.hibernate.SQL] (default task-6) select member0_.id as id1_0_0_, member0_.email as email2_0_0_, member0_.name as name3_0_0_, member0_.phone_number as phone_nu4_0_0_ from Member member0_ where member0_.id=? 17:55:28,665 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [0]
Modify persistence.xml to use l2-cache
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="primary"> <!-- If you are running in a production environment, add a managed data source, the example data source is just for development and testing! --> <jta-data-source>java:jboss/datasources/infinispan-jpa-exampleDS</jta-data-source> <shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode> <properties> <!-- Properties for Hibernate --> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.cache.use_second_level_cache" value="true"/> </properties> </persistence-unit> </persistence>
Send GET request again
- Now SQL is executed only once.
Browse JSF page again
- It still executes SQL every time I reload. why?
Make the application to allow to use l2-cache for queries
In reference to [2], we still have to modify some codes.
Modify persistence.xml
Add this property:
<property name="hibernate.cache.use_query_cache" value="true" />
Now it is:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="primary"> <!-- If you are running in a production environment, add a managed data source, the example data source is just for development and testing! --> <jta-data-source>java:jboss/datasources/infinispan-jpa-exampleDS</jta-data-source> <shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode> <properties> <!-- Properties for Hibernate --> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> <property name="hibernate.show_sql" value="false" /> <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true" /> </properties> </persistence-unit> </persistence>
Modify the code of JSF page
The query is executed in MemberListProducer#retrieveAllMembersOrderedByName():
@PostConstruct public void retrieveAllMembersOrderedByName() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Member> criteria = cb.createQuery(Member.class); Root<Member> member = criteria.from(Member.class); // Swap criteria statements if you would like to try out type-safe criteria queries, a new // feature in JPA 2.0 // criteria.select(member).orderBy(cb.asc(member.get(Member_.name))); criteria.select(member).orderBy(cb.asc(member.get("name"))); members = em.createQuery(criteria).getResultList(); }
Add the code that invokes Query#setHint():
setHint("org.hibernate.cacheable", true)
Now the code is:
@PostConstruct public void retrieveAllMembersOrderedByName() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Member> criteria = cb.createQuery(Member.class); Root<Member> member = criteria.from(Member.class); // Swap criteria statements if you would like to try out type-safe criteria queries, a new // feature in JPA 2.0 // criteria.select(member).orderBy(cb.asc(member.get(Member_.name))); criteria.select(member).orderBy(cb.asc(member.get("name"))); members = em.createQuery(criteria).setHint("org.hibernate.cacheable", true).getResultList(); }
Browse JSF page once again
- Now SQL is executed only once even I reloaded many times.
Remarks
- As default, 2nd level cache is disabled for queries.
- It seems enabled for operations like EntityManager#find() only.
- Every time queries need to set hint “org.hibernate.cacheable=true”.
- According to [3], it seems to there is a way to avoid to invoke annoying setHint() method for every queries.
- Seems to Query Hints are can define at when we declare Named Query.
- May I would try it later.
References
Tags: jpa
Just try to use ElementCollection with List
TweetPosted on Tuesday Feb 18, 2014 at 02:45PM in Technology
As I tried in Just try to use ElementCollection with Set, this is for Lists.
Environment
- WildFly 8.0.0.Final
- Hibernate 4.3.1
- PostgreSQL 9.2.4
- postgresql-9.3-1100.jdbc41.jar
Example project
Whole project resources are available in GitHub.
Ordering of elements
- According to the spec, ordering of elements will not be kept by JPA as default.
- It can be achieved easily with @OrderBy or @OrderColumn.
- Now It works for ElementCollection with basic type at recent versions of Hibernate[1].
List<Basic> with @OrderBy
Schema
jpatest=# \d listbasicorderby Table "public.listbasicorderby" Column | Type | Modifiers --------+--------+----------- id | bigint | not null Indexes: "listbasicorderby_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "listbasicorderby_list" CONSTRAINT "fk_5fs67i9n98xsp58x8vp4myxq8" FOREIGN KEY (listbasicorderby_id) REFERENCES listbasicorderby(id) jpatest=# \d listbasicorderby_list Table "public.listbasicorderby_list" Column | Type | Modifiers ---------------------+------------------------+----------- listbasicorderby_id | bigint | not null list | character varying(255) | Foreign-key constraints: "fk_5fs67i9n98xsp58x8vp4myxq8" FOREIGN KEY (listbasicorderby_id) REFERENCES listbasicorderby(id) jpatest=#
Entry data
log
15:42:35,481 DEBUG [org.hibernate.SQL] (default task-6) insert into ListBasicOrderBy (id) values (?) 15:42:35,481 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [1] 15:42:35,482 DEBUG [org.hibernate.SQL] (default task-6) insert into ListBasicOrderBy_list (ListBasicOrderBy_id, list) values (?, ?) 15:42:35,482 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [1] 15:42:35,482 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [VARCHAR] - [hoge] 15:42:35,484 DEBUG [org.hibernate.SQL] (default task-6) insert into ListBasicOrderBy_list (ListBasicOrderBy_id, list) values (?, ?) 15:42:35,484 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [1] 15:42:35,484 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [VARCHAR] - [hoge] 15:42:35,484 DEBUG [org.hibernate.SQL] (default task-6) insert into ListBasicOrderBy_list (ListBasicOrderBy_id, list) values (?, ?) 15:42:35,484 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [1] 15:42:35,484 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [VARCHAR] - [hige]
table
jpatest=# select * from listbasicorderby; id ---- 1 (1 row) jpatest=# select * from listbasicorderby_list order by list; listbasicorderby_id | list ---------------------+------ 1 | hige 1 | hoge 1 | hoge (3 rows)
Find
15:58:19,719 DEBUG [org.hibernate.SQL] (default task-15) select listbasico0_.id as id1_0_0_ from ListBasicOrderBy listbasico0_ where listbasico0_.id=? 15:58:19,720 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [1] as [BIGINT] - [1] 15:58:19,721 DEBUG [org.hibernate.SQL] (default task-15) select list0_.ListBasicOrderBy_id as ListBasi1_0_0_, list0_.list as list2_1_0_ from ListBasicOrderBy_list list0_ where list0_.ListBasicOrderBy_id=? order by list0_.list asc 15:58:19,721 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [1] as [BIGINT] - [1] 15:58:19,721 FINE [org.nailedtothex.jpatest.list.ListBasicTestDataManipulator] (default task-15) find(): [hige, hoge, hoge]
Add
log
16:05:46,474 DEBUG [org.hibernate.SQL] (default task-14) select listbasico0_.id as id1_0_0_ from ListBasicOrderBy listbasico0_ where listbasico0_.id=? 16:05:46,474 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 16:05:46,475 DEBUG [org.hibernate.SQL] (default task-14) select list0_.ListBasicOrderBy_id as ListBasi1_0_0_, list0_.list as list2_1_0_ from ListBasicOrderBy_list list0_ where list0_.ListBasicOrderBy_id=? order by list0_.list asc 16:05:46,476 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 16:05:46,480 DEBUG [org.hibernate.SQL] (default task-14) delete from ListBasicOrderBy_list where ListBasicOrderBy_id=? 16:05:46,480 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 16:05:46,481 DEBUG [org.hibernate.SQL] (default task-14) insert into ListBasicOrderBy_list (ListBasicOrderBy_id, list) values (?, ?) 16:05:46,481 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 16:05:46,481 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [2] as [VARCHAR] - [hige] 16:05:46,481 DEBUG [org.hibernate.SQL] (default task-14) insert into ListBasicOrderBy_list (ListBasicOrderBy_id, list) values (?, ?) 16:05:46,482 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 16:05:46,482 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [2] as [VARCHAR] - [hoge] 16:05:46,482 DEBUG [org.hibernate.SQL] (default task-14) insert into ListBasicOrderBy_list (ListBasicOrderBy_id, list) values (?, ?) 16:05:46,482 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 16:05:46,482 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [2] as [VARCHAR] - [hoge] 16:05:46,483 DEBUG [org.hibernate.SQL] (default task-14) insert into ListBasicOrderBy_list (ListBasicOrderBy_id, list) values (?, ?) 16:05:46,483 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 16:05:46,483 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [2] as [VARCHAR] - [hige]
table
jpatest=# select * from listbasicorderby_list order by list; listbasicorderby_id | list ---------------------+------ 1 | hige 1 | hige 1 | hoge 1 | hoge (4 rows) jpatest=#
- Surprisingly, Hibernate deletes all of elements, and insert them again.
Remove
log
16:14:09,474 DEBUG [org.hibernate.SQL] (default task-3) select listbasico0_.id as id1_0_0_ from ListBasicOrderBy listbasico0_ where listbasico0_.id=? 16:14:09,474 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [1] as [BIGINT] - [1] 16:14:09,475 DEBUG [org.hibernate.SQL] (default task-3) select list0_.ListBasicOrderBy_id as ListBasi1_0_0_, list0_.list as list2_1_0_ from ListBasicOrderBy_list list0_ where list0_.ListBasicOrderBy_id=? order by list0_.list asc 16:14:09,475 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [1] as [BIGINT] - [1] 16:14:09,480 DEBUG [org.hibernate.SQL] (default task-3) delete from ListBasicOrderBy_list where ListBasicOrderBy_id=? 16:14:09,480 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [1] as [BIGINT] - [1] 16:14:09,480 DEBUG [org.hibernate.SQL] (default task-3) insert into ListBasicOrderBy_list (ListBasicOrderBy_id, list) values (?, ?) 16:14:09,480 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [1] as [BIGINT] - [1] 16:14:09,480 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [2] as [VARCHAR] - [hige] 16:14:09,481 DEBUG [org.hibernate.SQL] (default task-3) insert into ListBasicOrderBy_list (ListBasicOrderBy_id, list) values (?, ?) 16:14:09,481 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [1] as [BIGINT] - [1] 16:14:09,481 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [2] as [VARCHAR] - [hoge]
table
jpatest=# select * from listbasicorderby_list order by list; listbasicorderby_id | list ---------------------+------ 1 | hige 1 | hoge (2 rows) jpatest=#
- Hibernate deletes all of elements again, same as preceding try.
List<Basic> with @OrderColumn
Schema
jpatest=# \d listbasicordercolumn Table "public.listbasicordercolumn" Column | Type | Modifiers --------+--------+----------- id | bigint | not null Indexes: "listbasicordercolumn_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "listbasicordercolumn_list" CONSTRAINT "fk_rp520yoe4qk3x4joe6114js" FOREIGN KEY (listbasicordercolumn_id) REFERENCES listbasicordercolumn(id) jpatest=# \d listbasicordercolumn_list Table "public.listbasicordercolumn_list" Column | Type | Modifiers -------------------------+------------------------+----------- listbasicordercolumn_id | bigint | not null list | character varying(255) | list_order | integer | not null Indexes: "listbasicordercolumn_list_pkey" PRIMARY KEY, btree (listbasicordercolumn_id, list_order) Foreign-key constraints: "fk_rp520yoe4qk3x4joe6114js" FOREIGN KEY (listbasicordercolumn_id) REFERENCES listbasicordercolumn(id)
Entry data
log
16:29:32,747 DEBUG [org.hibernate.SQL] (default task-2) insert into ListBasicOrderColumn (id) values (?) 16:29:32,748 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [BIGINT] - [1] 16:29:32,749 DEBUG [org.hibernate.SQL] (default task-2) insert into ListBasicOrderColumn_list (ListBasicOrderColumn_id, list_ORDER, list) values (?, ?, ?) 16:29:32,749 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [BIGINT] - [1] 16:29:32,749 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [2] as [INTEGER] - [0] 16:29:32,749 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [3] as [VARCHAR] - [hoge] 16:29:32,750 DEBUG [org.hibernate.SQL] (default task-2) insert into ListBasicOrderColumn_list (ListBasicOrderColumn_id, list_ORDER, list) values (?, ?, ?) 16:29:32,750 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [BIGINT] - [1] 16:29:32,751 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [2] as [INTEGER] - [1] 16:29:32,751 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [3] as [VARCHAR] - [hoge] 16:29:32,751 DEBUG [org.hibernate.SQL] (default task-2) insert into ListBasicOrderColumn_list (ListBasicOrderColumn_id, list_ORDER, list) values (?, ?, ?) 16:29:32,751 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [BIGINT] - [1] 16:29:32,751 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [2] as [INTEGER] - [2] 16:29:32,752 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [3] as [VARCHAR] - [hige]
table
jpatest=# select * from listbasicordercolumn; id ---- 1 (1 row) jpatest=# select * from listbasicordercolumn_list order by list_order; listbasicordercolumn_id | list | list_order -------------------------+------+------------ 1 | hoge | 0 1 | hoge | 1 1 | hige | 2 (3 rows) jpatest=#
Find
16:39:29,863 DEBUG [org.hibernate.SQL] (default task-2) select listbasico0_.id as id1_2_0_ from ListBasicOrderColumn listbasico0_ where listbasico0_.id=? 16:39:29,864 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [BIGINT] - [1] 16:39:29,865 DEBUG [org.hibernate.SQL] (default task-2) select list0_.ListBasicOrderColumn_id as ListBasi1_2_0_, list0_.list as list2_3_0_, list0_.list_ORDER as list_ORD3_0_ from ListBasicOrderColumn_list list0_ where list0_.ListBasicOrderColumn_id=? 16:39:29,865 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-2) binding parameter [1] as [BIGINT] - [1] 16:39:29,865 FINE [org.nailedtothex.jpatest.list.ListBasicOrderColumnTestDataManipulator] (default task-2) find(): [hoge, hoge, hige]
- List returned with correct order while there is no ORDER BY in the select clause.
- I guess that every rows have its order number, so they doesn't need to got sorted at database side.
Add
log
16:46:43,469 DEBUG [org.hibernate.SQL] (default task-6) select listbasico0_.id as id1_2_0_ from ListBasicOrderColumn listbasico0_ where listbasico0_.id=? 16:46:43,469 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [1] 16:46:43,470 DEBUG [org.hibernate.SQL] (default task-6) select list0_.ListBasicOrderColumn_id as ListBasi1_2_0_, list0_.list as list2_3_0_, list0_.list_ORDER as list_ORD3_0_ from ListBasicOrderColumn_list list0_ where list0_.ListBasicOrderColumn_id=? 16:46:43,470 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [1] 16:46:43,474 DEBUG [org.hibernate.SQL] (default task-6) update ListBasicOrderColumn_list set list=? where ListBasicOrderColumn_id=? and list_ORDER=? 16:46:43,475 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [VARCHAR] - [hige] 16:46:43,475 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [BIGINT] - [1] 16:46:43,475 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [3] as [INTEGER] - [1] 16:46:43,475 DEBUG [org.hibernate.SQL] (default task-6) update ListBasicOrderColumn_list set list=? where ListBasicOrderColumn_id=? and list_ORDER=? 16:46:43,475 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [VARCHAR] - [hoge] 16:46:43,476 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [BIGINT] - [1] 16:46:43,476 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [3] as [INTEGER] - [2] 16:46:43,476 DEBUG [org.hibernate.SQL] (default task-6) insert into ListBasicOrderColumn_list (ListBasicOrderColumn_id, list_ORDER, list) values (?, ?, ?) 16:46:43,476 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [1] 16:46:43,476 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [2] as [INTEGER] - [3] 16:46:43,476 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [3] as [VARCHAR] - [hige]
table
jpatest=# select * from listbasicordercolumn_list order by list_order; listbasicordercolumn_id | list | list_order -------------------------+------+------------ 1 | hoge | 0 1 | hige | 1 1 | hoge | 2 1 | hige | 3 (4 rows) jpatest=#
- Tried to add a element with index 1, it works correctly.
Remove
log
16:52:52,834 DEBUG [org.hibernate.SQL] (default task-4) select listbasico0_.id as id1_2_0_ from ListBasicOrderColumn listbasico0_ where listbasico0_.id=? 16:52:52,834 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-4) binding parameter [1] as [BIGINT] - [1] 16:52:52,835 DEBUG [org.hibernate.SQL] (default task-4) select list0_.ListBasicOrderColumn_id as ListBasi1_2_0_, list0_.list as list2_3_0_, list0_.list_ORDER as list_ORD3_0_ from ListBasicOrderColumn_list list0_ where list0_.ListBasicOrderColumn_id=? 16:52:52,835 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-4) binding parameter [1] as [BIGINT] - [1] 16:52:52,841 DEBUG [org.hibernate.SQL] (default task-4) delete from ListBasicOrderColumn_list where ListBasicOrderColumn_id=? and list_ORDER=? 16:52:52,841 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-4) binding parameter [1] as [BIGINT] - [1] 16:52:52,841 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-4) binding parameter [2] as [INTEGER] - [2] 16:52:52,842 DEBUG [org.hibernate.SQL] (default task-4) update ListBasicOrderColumn_list set list=? where ListBasicOrderColumn_id=? and list_ORDER=? 16:52:52,842 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-4) binding parameter [1] as [VARCHAR] - [hige] 16:52:52,842 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-4) binding parameter [2] as [BIGINT] - [1] 16:52:52,842 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-4) binding parameter [3] as [INTEGER] - [1]
table
jpatest=# select * from listbasicordercolumn_list order by list_order; listbasicordercolumn_id | list | list_order -------------------------+------+------------ 1 | hoge | 0 1 | hige | 1 (2 rows)
Swap
log
16:58:05,240 DEBUG [org.hibernate.SQL] (default task-11) select listbasico0_.id as id1_2_0_ from ListBasicOrderColumn listbasico0_ where listbasico0_.id=? 16:58:05,241 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-11) binding parameter [1] as [BIGINT] - [1] 16:58:05,242 DEBUG [org.hibernate.SQL] (default task-11) select list0_.ListBasicOrderColumn_id as ListBasi1_2_0_, list0_.list as list2_3_0_, list0_.list_ORDER as list_ORD3_0_ from ListBasicOrderColumn_list list0_ where list0_.ListBasicOrderColumn_id=? 16:58:05,243 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-11) binding parameter [1] as [BIGINT] - [1] 16:58:05,245 FINE [org.nailedtothex.jpatest.list.ListBasicOrderColumnTestDataManipulator] (default task-11) swap(): [hige, hoge, hoge] 16:58:05,248 DEBUG [org.hibernate.SQL] (default task-11) update ListBasicOrderColumn_list set list=? where ListBasicOrderColumn_id=? and list_ORDER=? 16:58:05,248 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-11) binding parameter [1] as [VARCHAR] - [hige] 16:58:05,248 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-11) binding parameter [2] as [BIGINT] - [1] 16:58:05,248 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-11) binding parameter [3] as [INTEGER] - [0] 16:58:05,249 DEBUG [org.hibernate.SQL] (default task-11) update ListBasicOrderColumn_list set list=? where ListBasicOrderColumn_id=? and list_ORDER=? 16:58:05,249 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-11) binding parameter [1] as [VARCHAR] - [hoge] 16:58:05,249 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-11) binding parameter [2] as [BIGINT] - [1] 16:58:05,249 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-11) binding parameter [3] as [INTEGER] - [2]
table
jpatest=# select * from listbasicordercolumn_list order by list_order; listbasicordercolumn_id | list | list_order -------------------------+------+------------ 1 | hige | 0 1 | hoge | 1 1 | hoge | 2 (3 rows)
List<Embeddable>
Schema
jpatest=# \d listembeddableparent Table "public.listembeddableparent" Column | Type | Modifiers --------+--------+----------- id | bigint | not null Indexes: "listembeddableparent_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "listembeddableparent_listembeddablechilds" CONSTRAINT "fk_owld92ex87wavc8ncap74eqk2" FOREIGN KEY (listembeddableparent_id) REFERENCES listembeddableparent(id) jpatest=# \d listembeddableparent_listembeddablechilds Table "public.listembeddableparent_listembeddablechilds" Column | Type | Modifiers ----------------------------+------------------------+----------- listembeddableparent_id | bigint | not null embfield1 | character varying(255) | embfield2 | character varying(255) | listembeddablechilds_order | integer | not null Indexes: "listembeddableparent_listembeddablechilds_pkey" PRIMARY KEY, btree (listembeddableparent_id, listembeddablechilds_order) Foreign-key constraints: "fk_owld92ex87wavc8ncap74eqk2" FOREIGN KEY (listembeddableparent_id) REFERENCES listembeddableparent(id) jpatest=#
Entry data
log
17:10:16,833 DEBUG [org.hibernate.SQL] (default task-13) insert into ListEmbeddableParent (id) values (?) 17:10:16,834 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [1] as [BIGINT] - [1] 17:10:16,836 DEBUG [org.hibernate.SQL] (default task-13) insert into ListEmbeddableParent_listEmbeddableChilds (ListEmbeddableParent_id, listEmbeddableChilds_ORDER, embField1, embField2) values (?, ?, ?, ?) 17:10:16,836 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [1] as [BIGINT] - [1] 17:10:16,836 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [2] as [INTEGER] - [0] 17:10:16,836 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [3] as [VARCHAR] - [child1field1] 17:10:16,836 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [4] as [VARCHAR] - [child1field2] 17:10:16,840 DEBUG [org.hibernate.SQL] (default task-13) insert into ListEmbeddableParent_listEmbeddableChilds (ListEmbeddableParent_id, listEmbeddableChilds_ORDER, embField1, embField2) values (?, ?, ?, ?) 17:10:16,840 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [1] as [BIGINT] - [1] 17:10:16,840 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [2] as [INTEGER] - [1] 17:10:16,840 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [3] as [VARCHAR] - [child2field1] 17:10:16,840 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [4] as [VARCHAR] - [child2field2] 17:10:16,841 DEBUG [org.hibernate.SQL] (default task-13) insert into ListEmbeddableParent_listEmbeddableChilds (ListEmbeddableParent_id, listEmbeddableChilds_ORDER, embField1, embField2) values (?, ?, ?, ?) 17:10:16,841 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [1] as [BIGINT] - [1] 17:10:16,841 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [2] as [INTEGER] - [2] 17:10:16,842 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [3] as [VARCHAR] - [child3field1] 17:10:16,842 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [4] as [VARCHAR] - [child3field2]
table
jpatest=# select * from listembeddableparent; id ---- 1 (1 row) jpatest=# select * from listembeddableparent_listembeddablechilds order by listembeddablechilds_order; listembeddableparent_id | embfield1 | embfield2 | listembeddablechilds_order -------------------------+--------------+--------------+---------------------------- 1 | child1field1 | child1field2 | 0 1 | child2field1 | child2field2 | 1 1 | child3field1 | child3field2 | 2 (3 rows)
Find
17:32:03,442 DEBUG [org.hibernate.SQL] (default task-7) select listembedd0_.id as id1_4_0_ from ListEmbeddableParent listembedd0_ where listembedd0_.id=? 17:32:03,443 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [1] as [BIGINT] - [1] 17:32:03,444 DEBUG [org.hibernate.SQL] (default task-7) select listembedd0_.ListEmbeddableParent_id as ListEmbe1_4_0_, listembedd0_.embField1 as embField2_5_0_, listembedd0_.embField2 as embField3_5_0_, listembedd0_.listEmbeddableChilds_ORDER as listEmbe4_0_ from ListEmbeddableParent_listEmbeddableChilds listembedd0_ where listembedd0_.ListEmbeddableParent_id=? 17:32:03,444 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [1] as [BIGINT] - [1] 17:32:03,443 FINE [org.nailedtothex.jpatest.list.ListEmbeddableTestDataManipulator] (default task-7) find(): [ListEmbeddableChild [embField1=child1field1, embField2=child1field2], ListEmbeddableChild [embField1=child2field1, embField2=child2field2], ListEmbeddableChild [embField1=child3field1, embField2=child3field2]]
Add
log
17:42:47,823 DEBUG [org.hibernate.SQL] (default task-3) select listembedd0_.id as id1_4_0_ from ListEmbeddableParent listembedd0_ where listembedd0_.id=? 17:42:47,824 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [1] as [BIGINT] - [1] 17:42:47,825 DEBUG [org.hibernate.SQL] (default task-3) select listembedd0_.ListEmbeddableParent_id as ListEmbe1_4_0_, listembedd0_.embField1 as embField2_5_0_, listembedd0_.embField2 as embField3_5_0_, listembedd0_.listEmbeddableChilds_ORDER as listEmbe4_0_ from ListEmbeddableParent_listEmbeddableChilds listembedd0_ where listembedd0_.ListEmbeddableParent_id=? 17:42:47,826 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [1] as [BIGINT] - [1] 17:42:47,830 DEBUG [org.hibernate.SQL] (default task-3) update ListEmbeddableParent_listEmbeddableChilds set embField1=?, embField2=? where ListEmbeddableParent_id=? and listEmbeddableChilds_ORDER=? 17:42:47,830 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [1] as [VARCHAR] - [child4field1] 17:42:47,830 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [2] as [VARCHAR] - [child4field2] 17:42:47,830 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [3] as [BIGINT] - [1] 17:42:47,830 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [4] as [INTEGER] - [2] 17:42:47,831 DEBUG [org.hibernate.SQL] (default task-3) insert into ListEmbeddableParent_listEmbeddableChilds (ListEmbeddableParent_id, listEmbeddableChilds_ORDER, embField1, embField2) values (?, ?, ?, ?) 17:42:47,831 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [1] as [BIGINT] - [1] 17:42:47,831 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [2] as [INTEGER] - [3] 17:42:47,831 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [3] as [VARCHAR] - [child3field1] 17:42:47,831 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-3) binding parameter [4] as [VARCHAR] - [child3field2]
table
jpatest=# select * from listembeddableparent_listembeddablechilds order by listembeddablechilds_order; listembeddableparent_id | embfield1 | embfield2 | listembeddablechilds_order -------------------------+--------------+--------------+---------------------------- 1 | child1field1 | child1field2 | 0 1 | child2field1 | child2field2 | 1 1 | child4field1 | child4field2 | 2 1 | child3field1 | child3field2 | 3 (4 rows)
Remove
log
17:47:20,736 DEBUG [org.hibernate.SQL] (default task-14) select listembedd0_.id as id1_4_0_ from ListEmbeddableParent listembedd0_ where listembedd0_.id=? 17:47:20,737 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 17:47:20,738 DEBUG [org.hibernate.SQL] (default task-14) select listembedd0_.ListEmbeddableParent_id as ListEmbe1_4_0_, listembedd0_.embField1 as embField2_5_0_, listembedd0_.embField2 as embField3_5_0_, listembedd0_.listEmbeddableChilds_ORDER as listEmbe4_0_ from ListEmbeddableParent_listEmbeddableChilds listembedd0_ where listembedd0_.ListEmbeddableParent_id=? 17:47:20,738 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 17:47:20,744 DEBUG [org.hibernate.SQL] (default task-14) delete from ListEmbeddableParent_listEmbeddableChilds where ListEmbeddableParent_id=? and listEmbeddableChilds_ORDER=? 17:47:20,744 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 17:47:20,744 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [2] as [INTEGER] - [2] 17:47:20,745 DEBUG [org.hibernate.SQL] (default task-14) update ListEmbeddableParent_listEmbeddableChilds set embField1=?, embField2=? where ListEmbeddableParent_id=? and listEmbeddableChilds_ORDER=? 17:47:20,745 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [VARCHAR] - [child3field1] 17:47:20,745 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [2] as [VARCHAR] - [child3field2] 17:47:20,745 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [3] as [BIGINT] - [1] 17:47:20,745 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [4] as [INTEGER] - [1]
table
jpatest=# select * from listembeddableparent_listembeddablechilds order by listembeddablechilds_order; listembeddableparent_id | embfield1 | embfield2 | listembeddablechilds_order -------------------------+--------------+--------------+---------------------------- 1 | child1field1 | child1field2 | 0 1 | child3field1 | child3field2 | 1 (2 rows)
Update
log
17:51:19,995 DEBUG [org.hibernate.SQL] (default task-7) select listembedd0_.id as id1_4_0_ from ListEmbeddableParent listembedd0_ where listembedd0_.id=? 17:51:19,996 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [1] as [BIGINT] - [1] 17:51:19,997 DEBUG [org.hibernate.SQL] (default task-7) select listembedd0_.ListEmbeddableParent_id as ListEmbe1_4_0_, listembedd0_.embField1 as embField2_5_0_, listembedd0_.embField2 as embField3_5_0_, listembedd0_.listEmbeddableChilds_ORDER as listEmbe4_0_ from ListEmbeddableParent_listEmbeddableChilds listembedd0_ where listembedd0_.ListEmbeddableParent_id=? 17:51:19,997 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [1] as [BIGINT] - [1] 17:51:20,001 DEBUG [org.hibernate.SQL] (default task-7) update ListEmbeddableParent_listEmbeddableChilds set embField1=?, embField2=? where ListEmbeddableParent_id=? and listEmbeddableChilds_ORDER=? 17:51:20,001 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [1] as [VARCHAR] - [child2field1-updated] 17:51:20,002 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [2] as [VARCHAR] - [child2field2-updated] 17:51:20,002 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [3] as [BIGINT] - [1] 17:51:20,002 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [4] as [INTEGER] - [1]
table
jpatest=# select * from listembeddableparent_listembeddablechilds order by listembeddablechilds_order; listembeddableparent_id | embfield1 | embfield2 | listembeddablechilds_order -------------------------+----------------------+----------------------+---------------------------- 1 | child1field1 | child1field2 | 0 1 | child2field1-updated | child2field2-updated | 1 1 | child3field1 | child3field2 | 2 (3 rows)
Remarks
- It's so convenient for prototypes.
- Maybe also good for small data sets.
References
Tags: jpa
Just try to use ElementCollection with Set
TweetPosted on Monday Feb 17, 2014 at 04:25PM in Technology
Just some notes about how it works, how create schemas, and how DMLs executes, and try to find its pros and cons.
Environment
- WildFly 8.0.0.Final
- Hibernate 4.3.1
- PostgreSQL 9.2.4
- postgresql-9.3-1100.jdbc41.jar
Example project
Whole project resources are available in GitHub.
Set<Basic>
Schema
jpatest=# \d setbasic Table "public.setbasic" Column | Type | Modifiers --------+--------+----------- id | bigint | not null Indexes: "setbasic_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "setbasic_set" CONSTRAINT "fk_qov1oy3cmf1wmttooahy264eh" FOREIGN KEY (setbasic_id) REFERENCES setbasic(id) jpatest=# \d setbasic_set Table "public.setbasic_set" Column | Type | Modifiers -------------+------------------------+----------- setbasic_id | bigint | not null set | character varying(255) | Foreign-key constraints: "fk_qov1oy3cmf1wmttooahy264eh" FOREIGN KEY (setbasic_id) REFERENCES setbasic(id) jpatest=#
Entry data
log
13:59:11,377 DEBUG [org.hibernate.SQL] (default task-15) insert into SetBasic (id) values (?) 13:59:11,378 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [1] as [BIGINT] - [1] 13:59:11,379 DEBUG [org.hibernate.SQL] (default task-15) insert into SetBasic_set (SetBasic_id, set) values (?, ?) 13:59:11,379 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [1] as [BIGINT] - [1] 13:59:11,379 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [2] as [VARCHAR] - [hige] 13:59:11,380 DEBUG [org.hibernate.SQL] (default task-15) insert into SetBasic_set (SetBasic_id, set) values (?, ?) 13:59:11,380 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [1] as [BIGINT] - [1] 13:59:11,380 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [2] as [VARCHAR] - [hoge] 13:59:11,381 DEBUG [org.hibernate.SQL] (default task-15) insert into SetBasic_set (SetBasic_id, set) values (?, ?) 13:59:11,381 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [1] as [BIGINT] - [1] 13:59:11,381 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [2] as [VARCHAR] - [fuge]
table
jpatest=# select * from setbasic; id ---- 1 (1 row) jpatest=# select * from setbasic_set; setbasic_id | set -------------+------ 1 | hige 1 | hoge 1 | fuge (3 rows) jpatest=#
Find
14:01:27,838 DEBUG [org.hibernate.SQL] (default task-8) select setbasic0_.id as id1_2_0_ from SetBasic setbasic0_ where setbasic0_.id=? 14:01:27,838 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-8) binding parameter [1] as [BIGINT] - [1] 14:01:27,839 DEBUG [org.hibernate.SQL] (default task-8) select set0_.SetBasic_id as SetBasic1_2_0_, set0_.set as set2_3_0_ from SetBasic_set set0_ where set0_.SetBasic_id=? 14:01:27,840 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-8) binding parameter [1] as [BIGINT] - [1] 14:01:27,839 FINE [org.nailedtothex.jpatest.set.SetBasicTestDataManipulator] (default task-8) find(): [hige, hoge, fuge]
Add
log
14:03:45,339 DEBUG [org.hibernate.SQL] (default task-15) select setbasic0_.id as id1_2_0_ from SetBasic setbasic0_ where setbasic0_.id=? 14:03:45,340 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [1] as [BIGINT] - [1] 14:03:45,341 DEBUG [org.hibernate.SQL] (default task-15) select set0_.SetBasic_id as SetBasic1_2_0_, set0_.set as set2_3_0_ from SetBasic_set set0_ where set0_.SetBasic_id=? 14:03:45,341 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [1] as [BIGINT] - [1] 14:03:45,345 DEBUG [org.hibernate.SQL] (default task-15) insert into SetBasic_set (SetBasic_id, set) values (?, ?) 14:03:45,345 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [1] as [BIGINT] - [1] 14:03:45,345 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-15) binding parameter [2] as [VARCHAR] - [hege]
table
jpatest=# select * from setbasic_set; setbasic_id | set -------------+------ 1 | hoge 1 | hige 1 | fuge 1 | hege (4 rows) jpatest=#
Remove
log
14:05:08,367 DEBUG [org.hibernate.SQL] (default task-5) select setbasic0_.id as id1_2_0_ from SetBasic setbasic0_ where setbasic0_.id=? 14:05:08,368 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-5) binding parameter [1] as [BIGINT] - [1] 14:05:08,369 DEBUG [org.hibernate.SQL] (default task-5) select set0_.SetBasic_id as SetBasic1_2_0_, set0_.set as set2_3_0_ from SetBasic_set set0_ where set0_.SetBasic_id=? 14:05:08,369 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-5) binding parameter [1] as [BIGINT] - [1] 14:05:08,374 DEBUG [org.hibernate.SQL] (default task-5) delete from SetBasic_set where SetBasic_id=? and set=? 14:05:08,374 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-5) binding parameter [1] as [BIGINT] - [1] 14:05:08,374 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-5) binding parameter [2] as [VARCHAR] - [hige]
table
jpatest=# select * from setbasic_set; setbasic_id | set -------------+------ 1 | hoge 1 | fuge (2 rows) jpatest=#
Set<Embeddable>
Schema
jpatest=# \d setembeddableparent Table "public.setembeddableparent" Column | Type | Modifiers --------+--------+----------- id | bigint | not null Indexes: "setembeddableparent_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "setembeddableparent_setembeddablechilds" CONSTRAINT "fk_r8ikgl74hjndujhjwuvnkt59m" FOREIGN KEY (setembeddableparent_id) REFERENCES setembeddableparent(id) jpatest=# \d setembeddableparent_setembeddablechilds Table "public.setembeddableparent_setembeddablechilds" Column | Type | Modifiers ------------------------+------------------------+----------- setembeddableparent_id | bigint | not null embfield1 | character varying(255) | embfield2 | character varying(255) | Foreign-key constraints: "fk_r8ikgl74hjndujhjwuvnkt59m" FOREIGN KEY (setembeddableparent_id) REFERENCES setembeddableparent(id) jpatest=#
Entry data
log
14:06:58,199 DEBUG [org.hibernate.SQL] (default task-12) insert into SetEmbeddableParent (id) values (?) 14:06:58,199 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-12) binding parameter [1] as [BIGINT] - [1] 14:06:58,200 DEBUG [org.hibernate.SQL] (default task-12) insert into SetEmbeddableParent_setEmbeddableChilds (SetEmbeddableParent_id, embField1, embField2) values (?, ?, ?) 14:06:58,200 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-12) binding parameter [1] as [BIGINT] - [1] 14:06:58,200 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-12) binding parameter [2] as [VARCHAR] - [child3field1] 14:06:58,201 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-12) binding parameter [3] as [VARCHAR] - [child3field2] 14:06:58,201 DEBUG [org.hibernate.SQL] (default task-12) insert into SetEmbeddableParent_setEmbeddableChilds (SetEmbeddableParent_id, embField1, embField2) values (?, ?, ?) 14:06:58,202 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-12) binding parameter [1] as [BIGINT] - [1] 14:06:58,202 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-12) binding parameter [2] as [VARCHAR] - [child2field1] 14:06:58,202 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-12) binding parameter [3] as [VARCHAR] - [child2field2] 14:06:58,202 DEBUG [org.hibernate.SQL] (default task-12) insert into SetEmbeddableParent_setEmbeddableChilds (SetEmbeddableParent_id, embField1, embField2) values (?, ?, ?) 14:06:58,202 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-12) binding parameter [1] as [BIGINT] - [1] 14:06:58,203 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-12) binding parameter [2] as [VARCHAR] - [child1field1] 14:06:58,203 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-12) binding parameter [3] as [VARCHAR] - [child1field2]
table
jpatest=# select * from setembeddableparent; id ---- 1 (1 row) jpatest=# select * from setembeddableparent_setembeddablechilds; setembeddableparent_id | embfield1 | embfield2 ------------------------+--------------+-------------- 1 | child3field1 | child3field2 1 | child2field1 | child2field2 1 | child1field1 | child1field2 (3 rows) jpatest=#
Find
14:10:32,579 DEBUG [org.hibernate.SQL] (default task-6) select setembedda0_.id as id1_4_0_ from SetEmbeddableParent setembedda0_ where setembedda0_.id=? 14:10:32,579 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [1] 14:10:32,580 DEBUG [org.hibernate.SQL] (default task-6) select setembedda0_.SetEmbeddableParent_id as SetEmbed1_4_0_, setembedda0_.embField1 as embField2_5_0_, setembedda0_.embField2 as embField3_5_0_ from SetEmbeddableParent_setEmbeddableChilds setembedda0_ where setembedda0_.SetEmbeddableParent_id=? 14:10:32,581 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-6) binding parameter [1] as [BIGINT] - [1] 14:10:32,580 FINE [org.nailedtothex.jpatest.set.SetEmbeddableTestDataManipulator] (default task-6) [SetEmbeddableChild [embField1=child3field1, embField2=child3field2], SetEmbeddableChild [embField1=child2field1, embField2=child2field2], SetEmbeddableChild [embField1=child1field1, embField2=child1field2]]
Add
log
14:12:20,219 DEBUG [org.hibernate.SQL] (default task-13) select setembedda0_.id as id1_4_0_ from SetEmbeddableParent setembedda0_ where setembedda0_.id=? 14:12:20,220 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [1] as [BIGINT] - [1] 14:12:20,221 DEBUG [org.hibernate.SQL] (default task-13) select setembedda0_.SetEmbeddableParent_id as SetEmbed1_4_0_, setembedda0_.embField1 as embField2_5_0_, setembedda0_.embField2 as embField3_5_0_ from SetEmbeddableParent_setEmbeddableChilds setembedda0_ where setembedda0_.SetEmbeddableParent_id=? 14:12:20,221 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [1] as [BIGINT] - [1] 14:12:20,225 DEBUG [org.hibernate.SQL] (default task-13) insert into SetEmbeddableParent_setEmbeddableChilds (SetEmbeddableParent_id, embField1, embField2) values (?, ?, ?) 14:12:20,225 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [1] as [BIGINT] - [1] 14:12:20,225 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [2] as [VARCHAR] - [child4field1] 14:12:20,225 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-13) binding parameter [3] as [VARCHAR] - [child4field2]
table
jpatest=# select * from setembeddableparent_setembeddablechilds; setembeddableparent_id | embfield1 | embfield2 ------------------------+--------------+-------------- 1 | child1field1 | child1field2 1 | child2field1 | child2field2 1 | child3field1 | child3field2 1 | child4field1 | child4field2 (4 rows)
Remove
log
14:14:48,499 DEBUG [org.hibernate.SQL] (default task-9) select setembedda0_.id as id1_4_0_ from SetEmbeddableParent setembedda0_ where setembedda0_.id=? 14:14:48,499 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [1] as [BIGINT] - [1] 14:14:48,500 DEBUG [org.hibernate.SQL] (default task-9) select setembedda0_.SetEmbeddableParent_id as SetEmbed1_4_0_, setembedda0_.embField1 as embField2_5_0_, setembedda0_.embField2 as embField3_5_0_ from SetEmbeddableParent_setEmbeddableChilds setembedda0_ where setembedda0_.SetEmbeddableParent_id=? 14:14:48,500 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [1] as [BIGINT] - [1] 14:14:48,500 FINE [org.nailedtothex.jpatest.set.SetEmbeddableTestDataManipulator] (default task-9) [SetEmbeddableChild [embField1=child3field1, embField2=child3field2], SetEmbeddableChild [embField1=child2field1, embField2=child2field2], SetEmbeddableChild [embField1=child1field1, embField2=child1field2]] 14:14:48,505 DEBUG [org.hibernate.SQL] (default task-9) delete from SetEmbeddableParent_setEmbeddableChilds where SetEmbeddableParent_id=? and embField1=? and embField2=? 14:14:48,505 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [1] as [BIGINT] - [1] 14:14:48,505 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [2] as [VARCHAR] - [child1field1] 14:14:48,506 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-9) binding parameter [3] as [VARCHAR] - [child1field2]
table
jpatest=# select * from setembeddableparent_setembeddablechilds; setembeddableparent_id | embfield1 | embfield2 ------------------------+--------------+-------------- 1 | child2field1 | child2field2 1 | child3field1 | child3field2 (2 rows) jpatest=#
Update
log
15:07:27,811 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [1] as [BIGINT] - [1] 15:07:27,812 DEBUG [org.hibernate.SQL] (default task-7) select setembedda0_.SetEmbeddableParent_id as SetEmbed1_2_0_, setembedda0_.embField1 as embField2_3_0_, setembedda0_.embField2 as embField3_3_0_ from SetEmbeddableParent_setEmbeddableChilds setembedda0_ where setembedda0_.SetEmbeddableParent_id=? 15:07:27,812 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [1] as [BIGINT] - [1] 15:07:27,815 DEBUG [org.hibernate.SQL] (default task-7) delete from SetEmbeddableParent_setEmbeddableChilds where SetEmbeddableParent_id=? and embField1=? and embField2=? 15:07:27,815 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [1] as [BIGINT] - [1] 15:07:27,815 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [2] as [VARCHAR] - [child2field1] 15:07:27,815 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [3] as [VARCHAR] - [child2field2] 15:07:27,816 DEBUG [org.hibernate.SQL] (default task-7) insert into SetEmbeddableParent_setEmbeddableChilds (SetEmbeddableParent_id, embField1, embField2) values (?, ?, ?) 15:07:27,816 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [1] as [BIGINT] - [1] 15:07:27,816 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [2] as [VARCHAR] - [child2field1] 15:07:27,816 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-7) binding parameter [3] as [VARCHAR] - [child2field2-updated]
table
jpatest=# select * from setembeddableparent_setembeddablechilds order by embfield1; setembeddableparent_id | embfield1 | embfield2 ------------------------+--------------+---------------------- 1 | child1field1 | child1field2 1 | child2field1 | child2field2-updated 1 | child3field1 | child3field2 (3 rows) jpatest=#
Add a duplicate element
14:13:59,763 DEBUG [org.hibernate.SQL] (default task-14) select setembedda0_.id as id1_4_0_ from SetEmbeddableParent setembedda0_ where setembedda0_.id=? 14:13:59,764 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1] 14:13:59,765 DEBUG [org.hibernate.SQL] (default task-14) select setembedda0_.SetEmbeddableParent_id as SetEmbed1_4_0_, setembedda0_.embField1 as embField2_5_0_, setembedda0_.embField2 as embField3_5_0_ from SetEmbeddableParent_setEmbeddableChilds setembedda0_ where setembedda0_.SetEmbeddableParent_id=? 14:13:59,765 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-14) binding parameter [1] as [BIGINT] - [1]
- No changes are made to DB.
Remove a element that not exist
14:16:48,827 DEBUG [org.hibernate.SQL] (default task-10) select setembedda0_.id as id1_4_0_ from SetEmbeddableParent setembedda0_ where setembedda0_.id=? 14:16:48,828 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-10) binding parameter [1] as [BIGINT] - [1] 14:16:48,829 DEBUG [org.hibernate.SQL] (default task-10) select setembedda0_.SetEmbeddableParent_id as SetEmbed1_4_0_, setembedda0_.embField1 as embField2_5_0_, setembedda0_.embField2 as embField3_5_0_ from SetEmbeddableParent_setEmbeddableChilds setembedda0_ where setembedda0_.SetEmbeddableParent_id=? 14:16:48,829 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (default task-10) binding parameter [1] as [BIGINT] - [1]
- No changes are made to DB.
Nested collection is prohibited
- It said @ElementCollection can be used with embeddable or basic type only.
Conclusion
Pros
- It works well as expected for some operations of Set API. it prevents that add duplicated elements.
- It can reduce some annoying codes.
Cons
- All elements are need to being fetched for manipulate, so maybe it is not good for large sets.
- Child elements are not entities, so use of some convenient operations is impossible.
Remarks
- Maybe it's good for proto-typing.
- But I'm not sure about whether it is appropriate for production systems especially high-traffic concurrent systems.
- For appropriate size sets, maybe 2nd level cache would be great help for its performance.
Tags: jpa
ExitStatusでフロー制御してみる
TweetPosted on Sunday Feb 16, 2014 at 08:17AM in Technology
ExitStatusでフロー制御的なことをして遊んでみる
環境・前提条件
- Chunk方式のStepで例外発生時にスキップさせてみると同じ。ここで作ったプロジェクトが普通に動いているものとする
仕様を見てみる
[1]から引いてみる
8.6 Transition Elements
Transition elements may be specified in the containment scope of a step, flow, split, or decision to direct job execution sequence or to terminate job execution. There are four transition elements:
- next - directs execution flow to the next execution element.
- fail - causes a job to end with FAILED batch status.
- end - causes a job to end with COMPLETED batch status.
- stop - causes a job to end with STOPPED batch status.
Fail end, and stop are considered “terminating elements” because they cause a job execution to terminate.
この4つを使って遊んでみる
サンプルの仕様
- 第一レベルの要素は以下
- step1
- step2
- どちらも参照するartifactは同じExitStatusBatchlet
- パラメータで与えたExitStatusで終わるだけ
- step1のExitStatusはジョブパラメータで変えられるようにしてある
- step2のExitStatusはnull(COMPLETED)固定
- step1には先で引いたTransition Elementsを4つ指定してある
- 4つのテストメソッドでstep1のExitStatusを以下のパターンで変えてテストする
- next(): step2に遷移する
- fail(): step1で異常終了
- end(): step1で正常終了
- stop(): step1で停止
資源
資源はこのへんにまとめて全部ある
動かしてみる
next
ログ
10:29:46,990 FINE [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 6) step1: exitStatus=NEXT 10:29:47,009 FINE [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 6) step2: exitStatus=null
Repository
job_execution
jbatch=# select * from job_execution order by jobexecutionid desc limit 1; jobexecutionid | jobinstanceid | version | createtime | starttime | endtime | lastupdatedtime | batchstatus | exitstatus | jobparameters | restartposition ----------------+---------------+---------+-------------------------+-------------------------+-------------------------+-------------------------+-------------+------------+-------------------+----------------- 148 | 141 | | 2014-02-16 10:29:46.976 | 2014-02-16 10:29:46.976 | 2014-02-16 10:29:47.011 | 2014-02-16 10:29:47.011 | COMPLETED | COMPLETED | exitStatus = NEXT+| | | | | | | | | | | (1 row)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (148) order by jobexecutionid, stepexecutionid; stepexecutionid | jobexecutionid | version | stepname | starttime | endtime | batchstatus | exitstatus | executionexception | persistentuserdata | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo -----------------+----------------+---------+----------+-------------------------+-------------------------+-------------+------------+--------------------+--------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+---------------------- 171 | 148 | | step1 | 2014-02-16 10:29:46.982 | 2014-02-16 10:29:46.991 | COMPLETED | NEXT | | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | 172 | 148 | | step2 | 2014-02-16 10:29:47.008 | 2014-02-16 10:29:47.009 | COMPLETED | COMPLETED | | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | (2 rows)
- 普通にstep2に遷移している
fail
ログ
10:41:31,341 FINE [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 9) step1: exitStatus=FAIL
Repository
job_execution
jbatch=# select * from job_execution order by jobexecutionid desc limit 1; jobexecutionid | jobinstanceid | version | createtime | starttime | endtime | lastupdatedtime | batchstatus | exitstatus | jobparameters | restartposition ----------------+---------------+---------+-------------------------+-------------------------+-------------------------+-------------------------+-------------+---------------------------+-------------------+----------------- 149 | 142 | | 2014-02-16 10:41:31.338 | 2014-02-16 10:41:31.338 | 2014-02-16 10:41:31.344 | 2014-02-16 10:41:31.344 | FAILED | EARLY COMPLETION (FAILED) | exitStatus = FAIL+| | | | | | | | | | | (1 row)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (149) order by jobexecutionid, stepexecutionid; stepexecutionid | jobexecutionid | version | stepname | starttime | endtime | batchstatus | exitstatus | executionexception | persistentuserdata | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo -----------------+----------------+---------+----------+-------------------------+-------------------------+-------------+------------+--------------------+--------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+---------------------- 173 | 149 | | step1 | 2014-02-16 10:41:31.339 | 2014-02-16 10:41:31.341 | COMPLETED | FAIL | | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | (1 row)
- step1終了後に異常終了している
- step_executionのbatchstatusがCOMPLETEDなのが気になるけどjob_executionのbatchstatusはちゃんとFAILEDになっている
end
ログ
10:43:54,134 FINE [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 3) step1: exitStatus=END
Repository
job_execution
jbatch=# select * from job_execution order by jobexecutionid desc limit 1; jobexecutionid | jobinstanceid | version | createtime | starttime | endtime | lastupdatedtime | batchstatus | exitstatus | jobparameters | restartposition ----------------+---------------+---------+-------------------------+-------------------------+-------------------------+-------------------------+-------------+------------------------+------------------+----------------- 150 | 143 | | 2014-02-16 10:43:54.129 | 2014-02-16 10:43:54.129 | 2014-02-16 10:43:54.137 | 2014-02-16 10:43:54.137 | COMPLETED | EARLY COMPLETION (END) | exitStatus = END+| | | | | | | | | | | (1 row)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (150) order by jobexecutionid, stepexecutionid; stepexecutionid | jobexecutionid | version | stepname | starttime | endtime | batchstatus | exitstatus | executionexception | persistentuserdata | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo -----------------+----------------+---------+----------+------------------------+-------------------------+-------------+------------+--------------------+--------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+---------------------- 174 | 150 | | step1 | 2014-02-16 10:43:54.13 | 2014-02-16 10:43:54.134 | COMPLETED | END | | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | (1 row)
- step2には遷移せず正常終了している
stop
ログ
10:46:52,718 FINE [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 2) step1: exitStatus=STOP
Repository
job_execution
jbatch=# select * from job_execution order by jobexecutionid desc limit 1; jobexecutionid | jobinstanceid | version | createtime | starttime | endtime | lastupdatedtime | batchstatus | exitstatus | jobparameters | restartposition ----------------+---------------+---------+-------------------------+-------------------------+------------------------+------------------------+-------------+-------------------------+-------------------+----------------- 151 | 144 | | 2014-02-16 10:46:52.705 | 2014-02-16 10:46:52.705 | 2014-02-16 10:46:52.72 | 2014-02-16 10:46:52.72 | STOPPED | EARLY COMPLETION (STOP) | exitStatus = STOP+| step2 | | | | | | | | | | (1 row)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (151) order by jobexecutionid, stepexecutionid; stepexecutionid | jobexecutionid | version | stepname | starttime | endtime | batchstatus | exitstatus | executionexception | persistentuserdata | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo -----------------+----------------+---------+----------+-------------------------+-------------------------+-------------+------------+--------------------+--------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+---------------------- 175 | 151 | | step1 | 2014-02-16 10:46:52.712 | 2014-02-16 10:46:52.718 | COMPLETED | STOP | | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | (1 row)
- step1終了後に停止している
- step_executionのbatchstatusがCOMPLETEDなのが気になるけどjob_executionのbatchstatusはちゃんとSTOPPEDになっている
stop要素のrestart属性で再実行時に遷移するstepを指定してみる
仕様を引いてみる
[1]の「8.6.4 Stop Element」に、restart属性についてこう書いてある
Specifies the job-level step, flow, or split at which to restart when the job is restarted. It must be a valid XML string value. This is a required attribute.
再実行してみる
テストメソッドstopRestart()に再実行のテストを書いてあるので実行してみる
ログ
10:58:59,039 FINE [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 4) step1: exitStatus=STOP 10:59:00,053 WARN [org.jberet] (batch-batch - 6) JBERET000018: Could not find the original step execution to restart. Current step execution id: 0, step name: step2 10:59:00,054 FINE [org.nailedtothex.jbatch.example.on.ExitStatusBatchlet] (batch-batch - 6) step2: exitStatus=null
Repository
job_execution
jbatch=# select * from job_execution where jobexecutionid in (161, 162) order by jobexecutionid; jobexecutionid | jobinstanceid | version | createtime | starttime | endtime | lastupdatedtime | batchstatus | exitstatus | jobparameters | restartposition ----------------+---------------+---------+-------------------------+-------------------------+-------------------------+-------------------------+-------------+-------------------------+-------------------+----------------- 161 | 151 | | 2014-02-16 10:58:59.035 | 2014-02-16 10:58:59.035 | 2014-02-16 10:58:59.042 | 2014-02-16 10:58:59.042 | STOPPED | EARLY COMPLETION (STOP) | exitStatus = STOP+| step2 | | | | | | | | | | 162 | 151 | | 2014-02-16 10:59:00.05 | 2014-02-16 10:59:00.05 | 2014-02-16 10:59:00.056 | 2014-02-16 10:59:00.056 | COMPLETED | COMPLETED | exitStatus = STOP+| | | | | | | | | | | (2 rows)
step_execution
jbatch=# select * from step_execution where jobexecutionid in (161, 162) order by jobexecutionid, stepexecutionid; stepexecutionid | jobexecutionid | version | stepname | starttime | endtime | batchstatus | exitstatus | executionexception | persistentuserdata | readcount | writecount | commitcount | rollbackcount | readskipcount | processskipcount | filtercount | writeskipcount | readercheckpointinfo | writercheckpointinfo -----------------+----------------+---------+----------+-------------------------+-------------------------+-------------+------------+--------------------+--------------------+-----------+------------+-------------+---------------+---------------+------------------+-------------+----------------+----------------------+---------------------- 186 | 161 | | step1 | 2014-02-16 10:58:59.037 | 2014-02-16 10:58:59.04 | COMPLETED | STOP | | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | 187 | 162 | | step2 | 2014-02-16 10:59:00.051 | 2014-02-16 10:59:00.054 | COMPLETED | COMPLETED | | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | (2 rows)
- 一応予想通り動いたけど、ログにWARNで出ている文言が気になる
- 再実行時に初回実行時に実行してないStepから再開するっていうのは仕様上微妙なのかも
備考
- on属性にはワイルドカード(*と?)が使える
- next on=“*” とかよく使いそう
- endは正常系で後続処理やらなくて良い時に使う感じ。非営業日だから何もせずに終わりとか(営業日関連はジョブ内でやらずに、ジョブ呼び出すところを作り込んでジョブが動く前に止めた方がいい気がするけど)
- failは異常系の時(例外投げてもいい気がするけど)
- stopは再実行前提で止めたい時とか、再実行するstepを制御したい時に使う?どういう状況で使うのかいまいち想像できないけど
- 先行処理の状況から分岐させたいときは、StepExecutionの配列を受け取ってExitStatusを決められるDeciderを使うのが良い
参考文献
Tags: jbatch