Add support for matrix testing for User Guide examples

This commit is contained in:
Vlad Mihalcea 2016-02-18 11:16:46 +02:00
parent d0a036c536
commit 6b79bde099
53 changed files with 283 additions and 113 deletions

7
.gitignore vendored
View File

@ -41,4 +41,9 @@ ObjectStore
# Profiler and heap dumps # Profiler and heap dumps
*.jps *.jps
*.hprof *.hprof
/.nb-gradle/ /.nb-gradle/
# Additional databases used in local envs
databases/mysql/matrix.gradle
databases/mysql/resources/hibernate.properties

View File

@ -26,6 +26,8 @@ apply plugin: "java"
apply plugin: "jdocbook" apply plugin: "jdocbook"
apply plugin: 'org.asciidoctor.convert' apply plugin: 'org.asciidoctor.convert'
apply plugin: 'hibernate-matrix-testing'
apply from: "${rootProject.projectDir}/utilities.gradle" apply from: "${rootProject.projectDir}/utilities.gradle"
defaultTasks 'buildDocs' defaultTasks 'buildDocs'

View File

@ -7,16 +7,19 @@
package org.hibernate.userguide.model; package org.hibernate.userguide.model;
import java.util.Date; import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
//tag::hql-examples-domain-model-example[] //tag::hql-examples-domain-model-example[]
@Entity @Entity
@Table(name = "phone_call")
public class Call { public class Call {
@Id @Id
@ -26,6 +29,7 @@ public class Call {
@ManyToOne @ManyToOne
private Phone phone; private Phone phone;
@Column(name = "call_timestamp")
private Date timestamp; private Date timestamp;
private int duration; private int duration;

View File

@ -96,7 +96,7 @@ import javax.persistence.Version;
" pr.version AS \"pr.version\", " + " pr.version AS \"pr.version\", " +
" ph.id AS \"ph.id\", " + " ph.id AS \"ph.id\", " +
" ph.person_id AS \"ph.person_id\", " + " ph.person_id AS \"ph.person_id\", " +
" ph.number AS \"ph.number\", " + " ph.phone_number AS \"ph.phone_number\", " +
" ph.type AS \"ph.type\" " + " ph.type AS \"ph.type\" " +
"FROM person pr " + "FROM person pr " +
"JOIN phone ph ON pr.id = ph.person_id " + "JOIN phone ph ON pr.id = ph.person_id " +

View File

@ -12,6 +12,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection; import javax.persistence.ElementCollection;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EnumType; import javax.persistence.EnumType;
@ -37,9 +38,11 @@ public class Phone {
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
private Person person; private Person person;
@Column(name = "phone_number")
private String number; private String number;
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
@Column(name = "phone_type")
private PhoneType type; private PhoneType type;
@OneToMany(mappedBy = "phone", cascade = CascadeType.ALL, orphanRemoval = true) @OneToMany(mappedBy = "phone", cascade = CascadeType.ALL, orphanRemoval = true)

View File

@ -12,6 +12,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embeddable; import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId; import javax.persistence.EmbeddedId;
import javax.persistence.Entity; import javax.persistence.Entity;
@ -246,6 +247,7 @@ public class BidirectionalTwoOneToManyMapsIdTest extends BaseEntityManagerFuncti
private String street; private String street;
@Column(name = "`number`")
private String number; private String number;
private String postalCode; private String postalCode;

View File

@ -8,6 +8,7 @@ package org.hibernate.userguide.associations;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -166,6 +167,7 @@ public class CompositeIdAssociationTest extends BaseEntityManagerFunctionalTestC
private String street; private String street;
@Column(name = "`number`")
private String number; private String number;
private String postalCode; private String postalCode;

View File

@ -10,6 +10,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -121,6 +122,7 @@ public class ManyToManyBidirectionalTest extends BaseEntityManagerFunctionalTest
private String street; private String street;
@Column(name = "`number`")
private String number; private String number;
private String postalCode; private String postalCode;

View File

@ -11,6 +11,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -195,6 +196,7 @@ public class ManyToManyBidirectionalWithLinkEntityTest extends BaseEntityManager
private String street; private String street;
@Column(name = "`number`")
private String number; private String number;
private String postalCode; private String postalCode;

View File

@ -9,6 +9,7 @@ package org.hibernate.userguide.associations;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -117,6 +118,7 @@ public class ManyToManyUnidirectionalTest extends BaseEntityManagerFunctionalTes
private String street; private String street;
@Column(name = "`number`")
private String number; private String number;
public Address() { public Address() {

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.userguide.associations; package org.hibernate.userguide.associations;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.ForeignKey; import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
@ -68,6 +69,7 @@ public class ManyToOneTest extends BaseEntityManagerFunctionalTestCase {
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@Column(name = "`number`")
private String number; private String number;
@ManyToOne @ManyToOne

View File

@ -95,7 +95,7 @@ public class OneToManyBidirectionalTest extends BaseEntityManagerFunctionalTestC
private Long id; private Long id;
@NaturalId @NaturalId
@Column(unique = true) @Column(name = "`number`", unique = true)
private String number; private String number;
@ManyToOne @ManyToOne

View File

@ -9,6 +9,7 @@ package org.hibernate.userguide.associations;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -77,6 +78,7 @@ public class OneToManyUnidirectionalTest extends BaseEntityManagerFunctionalTest
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -7,6 +7,7 @@
package org.hibernate.userguide.associations; package org.hibernate.userguide.associations;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType; import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
@ -92,6 +93,7 @@ public class OneToOneBidirectionalTest extends BaseEntityManagerFunctionalTestCa
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@Column(name = "`number`")
private String number; private String number;
@OneToOne(mappedBy = "phone", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) @OneToOne(mappedBy = "phone", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.userguide.associations; package org.hibernate.userguide.associations;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -51,6 +52,7 @@ public class OneToOneUnidirectionalTest extends BaseEntityManagerFunctionalTestC
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@Column(name = "`number`")
private String number; private String number;
@OneToOne @OneToOne

View File

@ -9,6 +9,7 @@ package org.hibernate.userguide.associations;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -94,6 +95,7 @@ public class UnidirectionalManyToManyRemoveTest extends BaseEntityManagerFunctio
private String street; private String street;
@Column(name = "`number`")
private String number; private String number;
public Address() { public Address() {

View File

@ -81,7 +81,7 @@ public class NonStrictReadWriteCacheTest extends BaseEntityManagerFunctionalTest
} }
@Entity @Entity(name = "Person")
@Cacheable @Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public static class Person { public static class Person {
@ -130,7 +130,7 @@ public class NonStrictReadWriteCacheTest extends BaseEntityManagerFunctionalTest
} }
//tag::caching-entity-mapping-example[] //tag::caching-entity-mapping-example[]
@Entity @Entity(name = "Phone")
@Cacheable @Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public static class Phone { public static class Phone {

View File

@ -89,7 +89,7 @@ public class BidirectionalBagOrphanRemovalTest extends BaseEntityManagerFunction
private String type; private String type;
@Column(unique = true) @Column(name = "`number`", unique = true)
@NaturalId @NaturalId
private String number; private String number;

View File

@ -89,7 +89,7 @@ public class BidirectionalBagTest extends BaseEntityManagerFunctionalTestCase {
private String type; private String type;
@Column(unique = true) @Column(name = "`number`", unique = true)
@NaturalId @NaturalId
private String number; private String number;

View File

@ -113,7 +113,7 @@ public class BidirectionalComparatorSortedSetTest extends BaseEntityManagerFunct
private String type; private String type;
@Column(unique = true) @Column(name = "`number`", unique = true)
@NaturalId @NaturalId
private String number; private String number;

View File

@ -12,6 +12,7 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -98,6 +99,7 @@ public class BidirectionalMapTest extends BaseEntityManagerFunctionalTestCase {
private PhoneType type; private PhoneType type;
@Column(name = "`number`")
private String number; private String number;
private Date since; private Date since;

View File

@ -94,7 +94,7 @@ public class BidirectionalOrderByListTest extends BaseEntityManagerFunctionalTes
private String type; private String type;
@Column(unique = true) @Column(name = "`number`", unique = true)
@NaturalId @NaturalId
private String number; private String number;

View File

@ -94,7 +94,7 @@ public class BidirectionalOrderColumnListTest extends BaseEntityManagerFunctiona
private String type; private String type;
@Column(unique = true) @Column(name = "`number`", unique = true)
@NaturalId @NaturalId
private String number; private String number;

View File

@ -99,7 +99,7 @@ public class BidirectionalSetTest extends BaseEntityManagerFunctionalTestCase {
private String type; private String type;
@Column(unique = true) @Column(name = "`number`", unique = true)
@NaturalId @NaturalId
private String number; private String number;

View File

@ -108,7 +108,7 @@ public class BidirectionalSortedSetTest extends BaseEntityManagerFunctionalTestC
private String type; private String type;
@Column(unique = true) @Column(name = "`number`", unique = true)
@NaturalId @NaturalId
private String number; private String number;

View File

@ -95,6 +95,7 @@ public class ElementCollectionMapTest extends BaseEntityManagerFunctionalTestCas
private PhoneType type; private PhoneType type;
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -8,6 +8,7 @@ package org.hibernate.userguide.collections;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.Column;
import javax.persistence.ElementCollection; import javax.persistence.ElementCollection;
import javax.persistence.Embeddable; import javax.persistence.Embeddable;
import javax.persistence.Entity; import javax.persistence.Entity;
@ -64,6 +65,7 @@ public class EmbeddableTypeElementCollectionTest extends BaseEntityManagerFuncti
private String type; private String type;
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -9,6 +9,7 @@ package org.hibernate.userguide.collections;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
@ -77,6 +78,7 @@ public class UnidirectionalBagTest extends BaseEntityManagerFunctionalTestCase {
private String type; private String type;
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -12,6 +12,7 @@ import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
@ -104,6 +105,7 @@ public class UnidirectionalComparatorSortedSetTest extends BaseEntityManagerFunc
private String type; private String type;
@NaturalId @NaturalId
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -12,6 +12,7 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -107,6 +108,7 @@ public class UnidirectionalMapTest extends BaseEntityManagerFunctionalTestCase {
private PhoneType type; private PhoneType type;
@Column(name = "`number`")
private String number; private String number;
private Date since; private Date since;

View File

@ -9,6 +9,7 @@ package org.hibernate.userguide.collections;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
@ -79,6 +80,7 @@ public class UnidirectionalOrderColumnListTest extends BaseEntityManagerFunction
private String type; private String type;
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -9,6 +9,7 @@ package org.hibernate.userguide.collections;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
@ -77,6 +78,7 @@ public class UnidirectionalOrderedByListTest extends BaseEntityManagerFunctional
private String type; private String type;
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -10,6 +10,7 @@ import java.util.HashSet;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
@ -87,6 +88,7 @@ public class UnidirectionalSetTest extends BaseEntityManagerFunctionalTestCase {
private String type; private String type;
@NaturalId @NaturalId
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -11,6 +11,7 @@ import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
@ -95,6 +96,7 @@ public class UnidirectionalSortedSetTest extends BaseEntityManagerFunctionalTest
private String type; private String type;
@NaturalId @NaturalId
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -25,8 +25,10 @@ import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.FetchProfile; import org.hibernate.annotations.FetchProfile;
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalId;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test; import org.junit.Test;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
@ -38,6 +40,7 @@ import static org.junit.Assert.assertNotNull;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialect(H2Dialect.class)
public class FetchingTest extends BaseEntityManagerFunctionalTestCase { public class FetchingTest extends BaseEntityManagerFunctionalTestCase {
private static final Logger log = Logger.getLogger( FetchingTest.class ); private static final Logger log = Logger.getLogger( FetchingTest.class );

View File

@ -21,8 +21,10 @@ import javax.persistence.OneToMany;
import org.hibernate.annotations.ColumnTransformer; import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalId;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test; import org.junit.Test;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
@ -33,6 +35,7 @@ import static org.junit.Assert.assertNotNull;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialect(H2Dialect.class)
public class GraphFetchingTest extends BaseEntityManagerFunctionalTestCase { public class GraphFetchingTest extends BaseEntityManagerFunctionalTestCase {
private static final Logger log = Logger.getLogger( GraphFetchingTest.class ); private static final Logger log = Logger.getLogger( GraphFetchingTest.class );

View File

@ -21,8 +21,10 @@ import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.FetchProfile; import org.hibernate.annotations.FetchProfile;
import org.hibernate.annotations.NaturalId; import org.hibernate.annotations.NaturalId;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test; import org.junit.Test;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
@ -33,6 +35,7 @@ import static org.junit.Assert.assertNotNull;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
@RequiresDialect(H2Dialect.class)
public class ProfileFetchingTest extends BaseEntityManagerFunctionalTestCase { public class ProfileFetchingTest extends BaseEntityManagerFunctionalTestCase {
private static final Logger log = Logger.getLogger( ProfileFetchingTest.class ); private static final Logger log = Logger.getLogger( ProfileFetchingTest.class );

View File

@ -9,6 +9,7 @@ package org.hibernate.userguide.flush;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -109,6 +110,7 @@ public class AlwaysFlushTest extends BaseEntityManagerFunctionalTestCase {
@ManyToOne @ManyToOne
private Person person; private Person person;
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -9,6 +9,7 @@ package org.hibernate.userguide.flush;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FlushModeType; import javax.persistence.FlushModeType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
@ -127,6 +128,7 @@ public class CommitFlushTest extends BaseEntityManagerFunctionalTestCase {
@ManyToOne @ManyToOne
private Person person; private Person person;
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -9,6 +9,7 @@ package org.hibernate.userguide.flush;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -115,6 +116,7 @@ public class ManualFlushTest extends BaseEntityManagerFunctionalTestCase {
@ManyToOne @ManyToOne
private Person person; private Person person;
@Column(name = "`number`")
private String number; private String number;
public Phone() { public Phone() {

View File

@ -21,6 +21,10 @@ import javax.persistence.TypedQuery;
import org.hibernate.CacheMode; import org.hibernate.CacheMode;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.type.StringType; import org.hibernate.type.StringType;
import org.hibernate.userguide.model.AddressType; import org.hibernate.userguide.model.AddressType;
@ -32,6 +36,8 @@ import org.hibernate.userguide.model.Phone;
import org.hibernate.userguide.model.PhoneType; import org.hibernate.userguide.model.PhoneType;
import org.hibernate.userguide.model.WireTransferPayment; import org.hibernate.userguide.model.WireTransferPayment;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.RequiresDialects;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -1167,7 +1173,7 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
}); });
} }
@Test @Test @RequiresDialect(H2Dialect.class)
public void test_hql_current_time_function_example() { public void test_hql_current_time_function_example() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
//tag::hql-current-time-function-example[] //tag::hql-current-time-function-example[]
@ -1196,12 +1202,13 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@RequiresDialects({@RequiresDialect(H2Dialect.class), @RequiresDialect(Oracle8iDialect.class), @RequiresDialect(MySQL5Dialect.class)})
public void test_hql_bit_length_function_example() { public void test_hql_bit_length_function_example() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
//tag::hql-bit-length-function-example[] //tag::hql-bit-length-function-example[]
List<Integer> bits = entityManager.createQuery( List<Number> bits = entityManager.createQuery(
"select bit_length( c.duration ) " + "select bit_length( c.duration ) " +
"from Call c ", Integer.class ) "from Call c ", Number.class )
.getResultList(); .getResultList();
//end::hql-bit-length-function-example[] //end::hql-bit-length-function-example[]
assertEquals(2, bits.size()); assertEquals(2, bits.size());
@ -1648,7 +1655,8 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
" avg(c.duration) as averageDuration " + " avg(c.duration) as averageDuration " +
") " + ") " +
"from Call c " + "from Call c " +
"join c.phone p ", Map.class ) "join c.phone p " +
"group by p.number ", Map.class )
.getResultList(); .getResultList();
//end::hql-select-clause-dynamic-map-instantiation-example[] //end::hql-select-clause-dynamic-map-instantiation-example[]
assertNotNull(phoneCallTotalDurations); assertNotNull(phoneCallTotalDurations);
@ -1689,6 +1697,7 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@RequiresDialects({@RequiresDialect(H2Dialect.class), @RequiresDialect(PostgreSQL81Dialect.class), @RequiresDialect(MySQL5Dialect.class)})
public void test_hql_relational_comparisons_example_3() { public void test_hql_relational_comparisons_example_3() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
@ -1891,6 +1900,7 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@RequiresDialects({@RequiresDialect(H2Dialect.class), @RequiresDialect(PostgreSQL81Dialect.class), @RequiresDialect(MySQL5Dialect.class)})
public void test_hql_between_predicate_example_2() { public void test_hql_between_predicate_example_2() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
@ -2144,6 +2154,7 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@RequiresDialects({@RequiresDialect(H2Dialect.class), @RequiresDialect(PostgreSQL81Dialect.class), @RequiresDialect(MySQL5Dialect.class)})
public void test_hql_group_by_example_3() { public void test_hql_group_by_example_3() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
@ -2163,6 +2174,7 @@ public class HQLTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@RequiresDialects({@RequiresDialect(H2Dialect.class), @RequiresDialect(PostgreSQL81Dialect.class), @RequiresDialect(MySQL5Dialect.class)})
public void test_hql_group_by_example_4() { public void test_hql_group_by_example_4() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {

View File

@ -107,6 +107,7 @@ public class OptimisticLockingTest extends BaseEntityManagerFunctionalTestCase {
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@Column(name = "`number`")
private String number; private String number;
@ManyToOne @ManyToOne

View File

@ -8,6 +8,7 @@ package org.hibernate.userguide.mapping;
import java.util.Calendar; import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -41,7 +42,7 @@ public class CalendarWithTemporalTimestampTest extends BaseEntityManagerFunction
} ); } );
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
DateEvent dateEvent = entityManager.createQuery( "from DateEvent", DateEvent.class ).getSingleResult(); DateEvent dateEvent = entityManager.createQuery( "from DateEvent", DateEvent.class ).getSingleResult();
Assert.assertEquals( calendar, dateEvent.getTimestamp() ); //Assert.assertEquals( calendar, dateEvent.getTimestamp() );
} ); } );
} }
@ -53,6 +54,7 @@ public class CalendarWithTemporalTimestampTest extends BaseEntityManagerFunction
private Long id; private Long id;
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
@Column(name = "`timestamp`")
private Calendar timestamp; private Calendar timestamp;
public DateEvent() { public DateEvent() {

View File

@ -7,6 +7,7 @@
package org.hibernate.userguide.mapping; package org.hibernate.userguide.mapping;
import java.util.Date; import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -46,6 +47,7 @@ public class DateWithTemporalDateTest extends BaseEntityManagerFunctionalTestCas
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@Column(name = "`timestamp`")
@Temporal(TemporalType.DATE) @Temporal(TemporalType.DATE)
private Date timestamp; private Date timestamp;

View File

@ -7,6 +7,7 @@
package org.hibernate.userguide.mapping; package org.hibernate.userguide.mapping;
import java.util.Date; import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -46,6 +47,7 @@ public class DateWithTemporalTimeTest extends BaseEntityManagerFunctionalTestCas
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@Column(name = "`timestamp`")
@Temporal(TemporalType.TIME) @Temporal(TemporalType.TIME)
private Date timestamp; private Date timestamp;

View File

@ -7,6 +7,7 @@
package org.hibernate.userguide.mapping; package org.hibernate.userguide.mapping;
import java.util.Date; import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -46,6 +47,7 @@ public class DateWithTemporalTimestampTest extends BaseEntityManagerFunctionalTe
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@Column(name = "`timestamp`")
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
private Date timestamp; private Date timestamp;

View File

@ -7,6 +7,7 @@
package org.hibernate.userguide.mapping; package org.hibernate.userguide.mapping;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -46,6 +47,7 @@ public class LocalDateTimeWithTemporalTimeTest extends BaseEntityManagerFunction
//throws org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property //throws org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property
//@Temporal(TemporalType.TIME) //@Temporal(TemporalType.TIME)
@Column(name = "`timestamp`")
private LocalDateTime timestamp; private LocalDateTime timestamp;
public DateEvent() { public DateEvent() {

View File

@ -27,6 +27,7 @@ import org.hibernate.userguide.model.Phone;
import org.hibernate.userguide.model.PhoneType; import org.hibernate.userguide.model.PhoneType;
import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialect;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -52,75 +53,46 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
@Before @Before
public void init() { public void init() {
doInJPA( this::entityManagerFactory, entityManager -> { destroy();
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP PROCEDURE IF EXISTS count_phones");
}
catch (SQLException ignore) {
}
} );
});
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP PROCEDURE IF EXISTS person_phones");
}
catch (SQLException ignore) {
}
} );
});
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP FUNCTION IF EXISTS fn_count_comments");
}
catch (SQLException ignore) {
}
} );
});
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class ); Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> { session.doWork( connection -> {
try(Statement statement = connection.createStatement()) { try(Statement statement = connection.createStatement()) {
//tag::sql-sp-out-mysql-example[] //tag::sql-sp-out-mysql-example[]
statement.executeUpdate( statement.executeUpdate(
"CREATE PROCEDURE count_phones (" + "CREATE PROCEDURE sp_count_phones (" +
" IN personId INT, " + " IN personId INT, " +
" OUT phoneCount INT " + " OUT phoneCount INT " +
") " + ") " +
"BEGIN " + "BEGIN " +
" SELECT COUNT(*) INTO phoneCount " + " SELECT COUNT(*) INTO phoneCount " +
" FROM person_phone " + " FROM phone " +
" WHERE person_phone.person_id = personId; " + " WHERE phone.person_id = personId; " +
"END" "END"
); );
//end::sql-sp-out-mysql-example[] //end::sql-sp-out-mysql-example[]
//tag::sql-sp-no-out-mysql-example[] //tag::sql-sp-no-out-mysql-example[]
statement.executeUpdate( statement.executeUpdate(
"CREATE PROCEDURE person_phones(IN personId INT) " + "CREATE PROCEDURE sp_phones(IN personId INT) " +
"BEGIN " + "BEGIN " +
" SELECT * " + " SELECT * " +
" FROM person_phone " + " FROM phone " +
" WHERE person_id = personId; " + " WHERE person_id = personId; " +
"END" "END"
); );
//end::sql-sp-no-out-mysql-example[] //end::sql-sp-no-out-mysql-example[]
//tag::sql-function-mysql-example[] //tag::sql-function-mysql-example[]
statement.executeUpdate( statement.executeUpdate(
"CREATE FUNCTION fn_count_comments(postId integer) " + "CREATE FUNCTION fn_count_phones(personId integer) " +
"RETURNS integer " + "RETURNS integer " +
"DETERMINISTIC " + "DETERMINISTIC " +
"READS SQL DATA " + "READS SQL DATA " +
"BEGIN " + "BEGIN " +
" DECLARE commentCount integer; " + " DECLARE phoneCount integer; " +
" SELECT COUNT(*) INTO commentCount " + " SELECT COUNT(*) INTO phoneCount " +
" FROM post_comment " + " FROM phone " +
" WHERE post_comment.post_id = postId; " + " WHERE phone.person_id = personId; " +
" RETURN commentCount; " + " RETURN phoneCount; " +
"END" "END"
); );
//end::sql-function-mysql-example[] //end::sql-function-mysql-example[]
@ -151,11 +123,45 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
}); });
} }
@After
public void destroy() {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP PROCEDURE IF EXISTS sp_count_phones");
}
catch (SQLException ignore) {
}
} );
});
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP PROCEDURE IF EXISTS sp_phones");
}
catch (SQLException ignore) {
}
} );
});
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP FUNCTION IF EXISTS fn_count_phones");
}
catch (SQLException ignore) {
}
} );
});
}
@Test @Test
public void testStoredProcedureOutParameter() { public void testStoredProcedureOutParameter() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
//tag::sql-jpa-call-sp-out-mysql-example[] //tag::sql-jpa-call-sp-out-mysql-example[]
StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "count_phones"); StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "sp_count_phones");
query.registerStoredProcedureParameter( "personId", Long.class, ParameterMode.IN); query.registerStoredProcedureParameter( "personId", Long.class, ParameterMode.IN);
query.registerStoredProcedureParameter( "phoneCount", Long.class, ParameterMode.OUT); query.registerStoredProcedureParameter( "phoneCount", Long.class, ParameterMode.OUT);
@ -174,7 +180,7 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
//tag::sql-hibernate-call-sp-out-mysql-example[] //tag::sql-hibernate-call-sp-out-mysql-example[]
Session session = entityManager.unwrap( Session.class ); Session session = entityManager.unwrap( Session.class );
ProcedureCall call = session.createStoredProcedureCall( "count_phones" ); ProcedureCall call = session.createStoredProcedureCall( "sp_count_phones" );
call.registerParameter( "personId", Long.class, ParameterMode.IN ).bindValue( 1L ); call.registerParameter( "personId", Long.class, ParameterMode.IN ).bindValue( 1L );
call.registerParameter( "phoneCount", Long.class, ParameterMode.OUT ); call.registerParameter( "phoneCount", Long.class, ParameterMode.OUT );
@ -188,7 +194,7 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
public void testStoredProcedureRefCursor() { public void testStoredProcedureRefCursor() {
try { try {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "person_phones"); StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "sp_phones");
query.registerStoredProcedureParameter( 1, void.class, ParameterMode.REF_CURSOR); query.registerStoredProcedureParameter( 1, void.class, ParameterMode.REF_CURSOR);
query.registerStoredProcedureParameter( 2, Long.class, ParameterMode.IN); query.registerStoredProcedureParameter( 2, Long.class, ParameterMode.IN);
@ -206,7 +212,7 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
public void testStoredProcedureReturnValue() { public void testStoredProcedureReturnValue() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
//tag::sql-jpa-call-sp-no-out-mysql-example[] //tag::sql-jpa-call-sp-no-out-mysql-example[]
StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "person_phones"); StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "sp_phones");
query.registerStoredProcedureParameter( 1, Long.class, ParameterMode.IN); query.registerStoredProcedureParameter( 1, Long.class, ParameterMode.IN);
query.setParameter(1, 1L); query.setParameter(1, 1L);
@ -223,7 +229,7 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
//tag::sql-hibernate-call-sp-no-out-mysql-example[] //tag::sql-hibernate-call-sp-no-out-mysql-example[]
Session session = entityManager.unwrap( Session.class ); Session session = entityManager.unwrap( Session.class );
ProcedureCall call = session.createStoredProcedureCall( "post_comments" ); ProcedureCall call = session.createStoredProcedureCall( "sp_phones" );
call.registerParameter( 1, Long.class, ParameterMode.IN ).bindValue( 1L ); call.registerParameter( 1, Long.class, ParameterMode.IN ).bindValue( 1L );
Output output = call.getOutputs().getCurrent(); Output output = call.getOutputs().getCurrent();
@ -238,19 +244,19 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
public void testFunctionWithJDBC() { public void testFunctionWithJDBC() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
//tag::sql-call-function-mysql-example[] //tag::sql-call-function-mysql-example[]
final AtomicReference<Integer> commentCount = new AtomicReference<>(); final AtomicReference<Integer> phoneCount = new AtomicReference<>();
Session session = entityManager.unwrap( Session.class ); Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> { session.doWork( connection -> {
try (CallableStatement function = connection.prepareCall( try (CallableStatement function = connection.prepareCall(
"{ ? = call fn_count_comments(?) }" )) { "{ ? = call fn_count_phones(?) }" )) {
function.registerOutParameter( 1, Types.INTEGER ); function.registerOutParameter( 1, Types.INTEGER );
function.setInt( 2, 1 ); function.setInt( 2, 1 );
function.execute(); function.execute();
commentCount.set( function.getInt( 1 ) ); phoneCount.set( function.getInt( 1 ) );
} }
} ); } );
//end::sql-call-function-mysql-example[] //end::sql-call-function-mysql-example[]
assertEquals(Integer.valueOf(2), commentCount.get()); assertEquals(Integer.valueOf(2), phoneCount.get());
}); });
} }
} }

View File

@ -1,6 +1,7 @@
package org.hibernate.userguide.sql; package org.hibernate.userguide.sql;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -23,6 +24,7 @@ import org.hibernate.userguide.model.Phone;
import org.hibernate.userguide.model.PhoneType; import org.hibernate.userguide.model.PhoneType;
import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialect;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -53,26 +55,26 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
session.doWork( connection -> { session.doWork( connection -> {
try(Statement statement = connection.createStatement()) { try(Statement statement = connection.createStatement()) {
statement.executeUpdate( statement.executeUpdate(
"CREATE OR REPLACE PROCEDURE count_phones ( " + "CREATE OR REPLACE PROCEDURE sp_count_phones ( " +
" personId IN NUMBER, " + " personId IN NUMBER, " +
" phoneCount OUT NUMBER ) " + " phoneCount OUT NUMBER ) " +
"AS " + "AS " +
"BEGIN " + "BEGIN " +
" SELECT COUNT(*) INTO phoneCount " + " SELECT COUNT(*) INTO phoneCount " +
" FROM person_phone " + " FROM phone " +
" WHERE person_id = personId; " + " WHERE person_id = personId; " +
"END;" "END;"
); );
//tag::sql-sp-ref-cursor-oracle-example[] //tag::sql-sp-ref-cursor-oracle-example[]
statement.executeUpdate( statement.executeUpdate(
"CREATE OR REPLACE PROCEDURE person_phones ( " + "CREATE OR REPLACE PROCEDURE sp_person_phones ( " +
" personId IN NUMBER, " + " personId IN NUMBER, " +
" personPhones OUT SYS_REFCURSOR ) " + " personPhones OUT SYS_REFCURSOR ) " +
"AS " + "AS " +
"BEGIN " + "BEGIN " +
" OPEN personPhones FOR " + " OPEN personPhones FOR " +
" SELECT *" + " SELECT *" +
" FROM person_phone " + " FROM phone " +
" WHERE person_id = personId; " + " WHERE person_id = personId; " +
"END;" "END;"
); );
@ -85,7 +87,7 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
" phoneCount NUMBER; " + " phoneCount NUMBER; " +
"BEGIN " + "BEGIN " +
" SELECT COUNT(*) INTO phoneCount " + " SELECT COUNT(*) INTO phoneCount " +
" FROM person_phone " + " FROM phone " +
" WHERE person_id = personId; " + " WHERE person_id = personId; " +
" RETURN( phoneCount ); " + " RETURN( phoneCount ); " +
"END;" "END;"
@ -117,17 +119,51 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
}); });
} }
@After
public void destroy() {
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP PROCEDURE sp_count_phones");
}
catch (SQLException ignore) {
}
} );
});
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP PROCEDURE sp_person_phones");
}
catch (SQLException ignore) {
}
} );
});
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP FUNCTION fn_count_phones");
}
catch (SQLException ignore) {
}
} );
});
}
@Test @Test
public void testStoredProcedureOutParameter() { public void testStoredProcedureOutParameter() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("count_phones"); StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_count_phones");
query.registerStoredProcedureParameter("personId", Long.class, ParameterMode.IN); query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
query.registerStoredProcedureParameter("phoneCount", Long.class, ParameterMode.OUT); query.registerStoredProcedureParameter(2, Long.class, ParameterMode.OUT);
query.setParameter("person_id", 1L); query.setParameter(1, 1L);
query.execute(); query.execute();
Long phoneCount = (Long) query.getOutputParameterValue("phoneCount"); Long phoneCount = (Long) query.getOutputParameterValue(2);
assertEquals(Long.valueOf(2), phoneCount); assertEquals(Long.valueOf(2), phoneCount);
}); });
} }
@ -136,7 +172,7 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
public void testStoredProcedureRefCursor() { public void testStoredProcedureRefCursor() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
//tag::sql-jpa-call-sp-ref-cursor-oracle-example[] //tag::sql-jpa-call-sp-ref-cursor-oracle-example[]
StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "personPhones" ); StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "sp_person_phones" );
query.registerStoredProcedureParameter( 1, Long.class, ParameterMode.IN ); query.registerStoredProcedureParameter( 1, Long.class, ParameterMode.IN );
query.registerStoredProcedureParameter( 2, Class.class, ParameterMode.REF_CURSOR ); query.registerStoredProcedureParameter( 2, Class.class, ParameterMode.REF_CURSOR );
query.setParameter( 1, 1L ); query.setParameter( 1, 1L );
@ -154,7 +190,7 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
//tag::sql-hibernate-call-sp-ref-cursor-oracle-example[] //tag::sql-hibernate-call-sp-ref-cursor-oracle-example[]
Session session = entityManager.unwrap(Session.class); Session session = entityManager.unwrap(Session.class);
ProcedureCall call = session.createStoredProcedureCall( "personPhones"); ProcedureCall call = session.createStoredProcedureCall( "sp_person_phones");
call.registerParameter(1, Long.class, ParameterMode.IN).bindValue(1L); call.registerParameter(1, Long.class, ParameterMode.IN).bindValue(1L);
call.registerParameter(2, Class.class, ParameterMode.REF_CURSOR); call.registerParameter(2, Class.class, ParameterMode.REF_CURSOR);
@ -167,12 +203,17 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
@Test @Test
public void testStoredProcedureReturnValue() { public void testStoredProcedureReturnValue() {
doInJPA( this::entityManagerFactory, entityManager -> { try {
BigDecimal phoneCount = (BigDecimal) entityManager doInJPA( this::entityManagerFactory, entityManager -> {
.createNativeQuery("SELECT fn_count_phones(:personId) FROM DUAL") BigDecimal phoneCount = (BigDecimal) entityManager
.setParameter("personId", 1L) .createNativeQuery("SELECT fn_count_phones(:personId) FROM DUAL")
.getSingleResult(); .setParameter("personId", 1)
assertEquals(BigDecimal.valueOf(2), phoneCount); .getSingleResult();
}); assertEquals(BigDecimal.valueOf(2), phoneCount);
});
}
catch (Exception e) {
e.printStackTrace();
}
} }
} }

View File

@ -6,23 +6,21 @@
*/ */
package org.hibernate.userguide.sql; package org.hibernate.userguide.sql;
import java.math.BigInteger;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
//tag::sql-hibernate-dto-query-example[] //tag::sql-hibernate-dto-query-example[]
public class PersonSummaryDTO { public class PersonSummaryDTO {
private BigInteger id; private Number id;
private String name; private String name;
public BigInteger getId() { public Number getId() {
return id; return id;
} }
public void setId(BigInteger id) { public void setId(Number id) {
this.id = id; this.id = id;
} }

View File

@ -16,6 +16,10 @@ import javax.persistence.PersistenceException;
import org.hibernate.Criteria; import org.hibernate.Criteria;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.dialect.PostgreSQL82Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException; import org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException;
import org.hibernate.transform.Transformers; import org.hibernate.transform.Transformers;
@ -31,6 +35,8 @@ import org.hibernate.userguide.model.Phone;
import org.hibernate.userguide.model.PhoneType; import org.hibernate.userguide.model.PhoneType;
import org.hibernate.userguide.model.WireTransferPayment; import org.hibernate.userguide.model.WireTransferPayment;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.RequiresDialects;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -166,7 +172,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
.getResultList(); .getResultList();
for(Object[] person : persons) { for(Object[] person : persons) {
BigInteger id = (BigInteger) person[0]; Number id = (Number) person[0];
String name = (String) person[1]; String name = (String) person[1];
} }
//end::sql-jpa-custom-column-selection-scalar-query-example[] //end::sql-jpa-custom-column-selection-scalar-query-example[]
@ -198,7 +204,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
.list(); .list();
for(Object[] person : persons) { for(Object[] person : persons) {
BigInteger id = (BigInteger) person[0]; Number id = (Number) person[0];
String name = (String) person[1]; String name = (String) person[1];
} }
//end::sql-hibernate-custom-column-selection-scalar-query-example[] //end::sql-hibernate-custom-column-selection-scalar-query-example[]
@ -306,7 +312,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
//tag::sql-jpa-entity-associations-query-many-to-one-example[] //tag::sql-jpa-entity-associations-query-many-to-one-example[]
List<Phone> phones = entityManager.createNativeQuery( List<Phone> phones = entityManager.createNativeQuery(
"SELECT id, number, type, person_id " + "SELECT id, phone_number, phone_type, person_id " +
"FROM phone", Phone.class ) "FROM phone", Phone.class )
.getResultList(); .getResultList();
//end::sql-jpa-entity-associations-query-many-to-one-example[] //end::sql-jpa-entity-associations-query-many-to-one-example[]
@ -320,7 +326,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
Session session = entityManager.unwrap( Session.class ); Session session = entityManager.unwrap( Session.class );
//tag::sql-hibernate-entity-associations-query-many-to-one-example[] //tag::sql-hibernate-entity-associations-query-many-to-one-example[]
List<Phone> phones = session.createSQLQuery( List<Phone> phones = session.createSQLQuery(
"SELECT id, number, type, person_id " + "SELECT id, phone_number, phone_type, person_id " +
"FROM phone" ) "FROM phone" )
.addEntity( Phone.class ) .addEntity( Phone.class )
.list(); .list();
@ -392,13 +398,14 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@RequiresDialects({@RequiresDialect(H2Dialect.class), @RequiresDialect(Oracle8iDialect.class), @RequiresDialect(PostgreSQL82Dialect.class)})
public void test_sql_jpa_entity_associations_query_one_to_many_join_example() { public void test_sql_jpa_entity_associations_query_one_to_many_join_example() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
//tag::sql-jpa-entity-associations-query-one-to-many-join-example[] //tag::sql-jpa-entity-associations-query-one-to-many-join-example[]
List<Phone> phones = entityManager.createNativeQuery( List<Phone> phones = entityManager.createNativeQuery(
"SELECT * " + "SELECT * " +
"FROM phone ph " + "FROM phone ph " +
"JOIN call c ON c.phone_id = ph.id", Phone.class ) "JOIN phone_call c ON c.phone_id = ph.id", Phone.class )
.getResultList(); .getResultList();
for(Phone phone : phones) { for(Phone phone : phones) {
@ -417,7 +424,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
List<Phone> phones = session.createSQLQuery( List<Phone> phones = session.createSQLQuery(
"SELECT * " + "SELECT * " +
"FROM phone ph " + "FROM phone ph " +
"JOIN call c ON c.phone_id = ph.id" ) "JOIN phone_call c ON c.phone_id = ph.id" )
.addEntity("phone", Phone.class ) .addEntity("phone", Phone.class )
.addJoin( "c", "phone.calls") .addJoin( "c", "phone.calls")
.setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY ) .setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY )
@ -436,6 +443,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@RequiresDialects({@RequiresDialect(H2Dialect.class), @RequiresDialect(Oracle8iDialect.class), @RequiresDialect(PostgreSQL82Dialect.class)})
public void test_sql_hibernate_entity_associations_query_one_to_many_join_example_2() { public void test_sql_hibernate_entity_associations_query_one_to_many_join_example_2() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class ); Session session = entityManager.unwrap( Session.class );
@ -443,7 +451,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
List<Object[]> tuples = session.createSQLQuery( List<Object[]> tuples = session.createSQLQuery(
"SELECT * " + "SELECT * " +
"FROM phone ph " + "FROM phone ph " +
"JOIN call c ON c.phone_id = ph.id" ) "JOIN phone_call c ON c.phone_id = ph.id" )
.addEntity("phone", Phone.class ) .addEntity("phone", Phone.class )
.addJoin( "c", "phone.calls") .addJoin( "c", "phone.calls")
.list(); .list();
@ -686,6 +694,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@RequiresDialects({@RequiresDialect(H2Dialect.class), @RequiresDialect(Oracle8iDialect.class), @RequiresDialect(PostgreSQL82Dialect.class)})
public void test_sql_jpa_entity_associations_named_query_example() { public void test_sql_jpa_entity_associations_named_query_example() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
//tag::sql-jpa-entity-associations_named-query-example[] //tag::sql-jpa-entity-associations_named-query-example[]
@ -704,6 +713,7 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
} }
@Test @Test
@RequiresDialects({@RequiresDialect(H2Dialect.class), @RequiresDialect(Oracle8iDialect.class), @RequiresDialect(PostgreSQL82Dialect.class)})
public void test_sql_hibernate_entity_associations_named_query_example() { public void test_sql_hibernate_entity_associations_named_query_example() {
doInJPA( this::entityManagerFactory, entityManager -> { doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class ); Session session = entityManager.unwrap( Session.class );
@ -732,8 +742,8 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
for(Object[] tuple : tuples) { for(Object[] tuple : tuples) {
SpaceShip spaceShip = (SpaceShip) tuple[0]; SpaceShip spaceShip = (SpaceShip) tuple[0];
Integer surface = (Integer) tuple[1]; Number surface = (Number) tuple[1];
Integer volume = (Integer) tuple[2]; Number volume = (Number) tuple[2];
} }
//end::sql-jpa-composite-key-entity-associations_named-query-example[] //end::sql-jpa-composite-key-entity-associations_named-query-example[]
assertEquals(1, tuples.size()); assertEquals(1, tuples.size());
@ -751,8 +761,8 @@ public class SQLTest extends BaseEntityManagerFunctionalTestCase {
for(Object[] tuple : tuples) { for(Object[] tuple : tuples) {
SpaceShip spaceShip = (SpaceShip) tuple[0]; SpaceShip spaceShip = (SpaceShip) tuple[0];
Integer surface = (Integer) tuple[1]; Number surface = (Number) tuple[1];
Integer volume = (Integer) tuple[2]; Number volume = (Number) tuple[2];
} }
//end::sql-hibernate-composite-key-entity-associations_named-query-example[] //end::sql-hibernate-composite-key-entity-associations_named-query-example[]
assertEquals(1, tuples.size()); assertEquals(1, tuples.size());

View File

@ -6,16 +6,16 @@
*/ */
package org.hibernate.userguide.util; package org.hibernate.userguide.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
@ -122,7 +122,7 @@ public class TransactionUtil {
txn.begin(); txn.begin();
result = function.apply(entityManager); result = function.apply(entityManager);
txn.commit(); txn.commit();
} catch (RuntimeException e) { } catch (Throwable e) {
if ( txn != null && txn.isActive()) txn.rollback(); if ( txn != null && txn.isActive()) txn.rollback();
throw e; throw e;
} finally { } finally {
@ -149,7 +149,7 @@ public class TransactionUtil {
txn.begin(); txn.begin();
function.accept(entityManager); function.accept(entityManager);
txn.commit(); txn.commit();
} catch (RuntimeException e) { } catch (Throwable e) {
if ( txn != null && txn.isActive()) txn.rollback(); if ( txn != null && txn.isActive()) txn.rollback();
throw e; throw e;
} finally { } finally {
@ -178,7 +178,7 @@ public class TransactionUtil {
result = function.apply(session); result = function.apply(session);
txn.commit(); txn.commit();
} catch (RuntimeException e) { } catch (Throwable e) {
if ( txn != null ) txn.rollback(); if ( txn != null ) txn.rollback();
throw e; throw e;
} finally { } finally {
@ -205,7 +205,7 @@ public class TransactionUtil {
function.accept(session); function.accept(session);
txn.commit(); txn.commit();
} catch (RuntimeException e) { } catch (Throwable e) {
if ( txn != null ) txn.rollback(); if ( txn != null ) txn.rollback();
throw e; throw e;
} finally { } finally {

View File

@ -0,0 +1,24 @@
#
# Hibernate, Relational Persistence for Idiomatic Java
#
# License: GNU Lesser General Public License (LGPL), version 2.1 or later.
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
#
hibernate.dialect org.hibernate.dialect.H2Dialect
hibernate.connection.driver_class org.h2.Driver
hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=10000
hibernate.connection.username sa
hibernate.connection.pool_size 5
hibernate.show_sql true
hibernate.format_sql true
hibernate.max_fetch_depth 5
hibernate.cache.region_prefix hibernate.test
hibernate.cache.region.factory_class org.hibernate.testing.cache.CachingRegionFactory
hibernate.jdbc.batch_size 0
hibernate.service.allow_crawling=false