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

5
.gitignore vendored
View File

@ -42,3 +42,8 @@ ObjectStore
*.jps
*.hprof
/.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: 'org.asciidoctor.convert'
apply plugin: 'hibernate-matrix-testing'
apply from: "${rootProject.projectDir}/utilities.gradle"
defaultTasks 'buildDocs'

View File

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

View File

@ -96,7 +96,7 @@ import javax.persistence.Version;
" pr.version AS \"pr.version\", " +
" ph.id AS \"ph.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\" " +
"FROM person pr " +
"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.Map;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
@ -37,9 +38,11 @@ public class Phone {
@ManyToOne(fetch = FetchType.LAZY)
private Person person;
@Column(name = "phone_number")
private String number;
@Enumerated(EnumType.STRING)
@Column(name = "phone_type")
private PhoneType type;
@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.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@ -246,6 +247,7 @@ public class BidirectionalTwoOneToManyMapsIdTest extends BaseEntityManagerFuncti
private String street;
@Column(name = "`number`")
private String number;
private String postalCode;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -25,8 +25,10 @@ import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.FetchProfile;
import org.hibernate.annotations.NaturalId;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test;
import org.jboss.logging.Logger;
@ -38,6 +40,7 @@ import static org.junit.Assert.assertNotNull;
/**
* @author Vlad Mihalcea
*/
@RequiresDialect(H2Dialect.class)
public class FetchingTest extends BaseEntityManagerFunctionalTestCase {
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.NaturalId;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test;
import org.jboss.logging.Logger;
@ -33,6 +35,7 @@ import static org.junit.Assert.assertNotNull;
/**
* @author Vlad Mihalcea
*/
@RequiresDialect(H2Dialect.class)
public class GraphFetchingTest extends BaseEntityManagerFunctionalTestCase {
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.FetchProfile;
import org.hibernate.annotations.NaturalId;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.RequiresDialect;
import org.junit.Test;
import org.jboss.logging.Logger;
@ -33,6 +35,7 @@ import static org.junit.Assert.assertNotNull;
/**
* @author Vlad Mihalcea
*/
@RequiresDialect(H2Dialect.class)
public class ProfileFetchingTest extends BaseEntityManagerFunctionalTestCase {
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.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@ -109,6 +110,7 @@ public class AlwaysFlushTest extends BaseEntityManagerFunctionalTestCase {
@ManyToOne
private Person person;
@Column(name = "`number`")
private String number;
public Phone() {

View File

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

View File

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

View File

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

View File

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

View File

@ -8,6 +8,7 @@ package org.hibernate.userguide.mapping;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@ -41,7 +42,7 @@ public class CalendarWithTemporalTimestampTest extends BaseEntityManagerFunction
} );
doInJPA( this::entityManagerFactory, entityManager -> {
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;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "`timestamp`")
private Calendar timestamp;
public DateEvent() {

View File

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

View File

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

View File

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

View File

@ -7,6 +7,7 @@
package org.hibernate.userguide.mapping;
import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
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
//@Temporal(TemporalType.TIME)
@Column(name = "`timestamp`")
private LocalDateTime timestamp;
public DateEvent() {

View File

@ -27,6 +27,7 @@ import org.hibernate.userguide.model.Phone;
import org.hibernate.userguide.model.PhoneType;
import org.hibernate.testing.RequiresDialect;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -52,75 +53,46 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
@Before
public void init() {
doInJPA( this::entityManagerFactory, entityManager -> {
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) {
}
} );
});
destroy();
doInJPA( this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap( Session.class );
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
//tag::sql-sp-out-mysql-example[]
statement.executeUpdate(
"CREATE PROCEDURE count_phones (" +
"CREATE PROCEDURE sp_count_phones (" +
" IN personId INT, " +
" OUT phoneCount INT " +
") " +
"BEGIN " +
" SELECT COUNT(*) INTO phoneCount " +
" FROM person_phone " +
" WHERE person_phone.person_id = personId; " +
" FROM phone " +
" WHERE phone.person_id = personId; " +
"END"
);
//end::sql-sp-out-mysql-example[]
//tag::sql-sp-no-out-mysql-example[]
statement.executeUpdate(
"CREATE PROCEDURE person_phones(IN personId INT) " +
"CREATE PROCEDURE sp_phones(IN personId INT) " +
"BEGIN " +
" SELECT * " +
" FROM person_phone " +
" FROM phone " +
" WHERE person_id = personId; " +
"END"
);
//end::sql-sp-no-out-mysql-example[]
//tag::sql-function-mysql-example[]
statement.executeUpdate(
"CREATE FUNCTION fn_count_comments(postId integer) " +
"CREATE FUNCTION fn_count_phones(personId integer) " +
"RETURNS integer " +
"DETERMINISTIC " +
"READS SQL DATA " +
"BEGIN " +
" DECLARE commentCount integer; " +
" SELECT COUNT(*) INTO commentCount " +
" FROM post_comment " +
" WHERE post_comment.post_id = postId; " +
" RETURN commentCount; " +
" DECLARE phoneCount integer; " +
" SELECT COUNT(*) INTO phoneCount " +
" FROM phone " +
" WHERE phone.person_id = personId; " +
" RETURN phoneCount; " +
"END"
);
//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
public void testStoredProcedureOutParameter() {
doInJPA( this::entityManagerFactory, entityManager -> {
//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( "phoneCount", Long.class, ParameterMode.OUT);
@ -174,7 +180,7 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
//tag::sql-hibernate-call-sp-out-mysql-example[]
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( "phoneCount", Long.class, ParameterMode.OUT );
@ -188,7 +194,7 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
public void testStoredProcedureRefCursor() {
try {
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( 2, Long.class, ParameterMode.IN);
@ -206,7 +212,7 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
public void testStoredProcedureReturnValue() {
doInJPA( this::entityManagerFactory, entityManager -> {
//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.setParameter(1, 1L);
@ -223,7 +229,7 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
//tag::sql-hibernate-call-sp-no-out-mysql-example[]
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 );
Output output = call.getOutputs().getCurrent();
@ -238,19 +244,19 @@ public class MySQLStoredProcedureTest extends BaseEntityManagerFunctionalTestCas
public void testFunctionWithJDBC() {
doInJPA( this::entityManagerFactory, entityManager -> {
//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.doWork( connection -> {
try (CallableStatement function = connection.prepareCall(
"{ ? = call fn_count_comments(?) }" )) {
"{ ? = call fn_count_phones(?) }" )) {
function.registerOutParameter( 1, Types.INTEGER );
function.setInt( 2, 1 );
function.execute();
commentCount.set( function.getInt( 1 ) );
phoneCount.set( function.getInt( 1 ) );
}
} );
//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;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.time.LocalDateTime;
@ -23,6 +24,7 @@ import org.hibernate.userguide.model.Phone;
import org.hibernate.userguide.model.PhoneType;
import org.hibernate.testing.RequiresDialect;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -53,26 +55,26 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
session.doWork( connection -> {
try(Statement statement = connection.createStatement()) {
statement.executeUpdate(
"CREATE OR REPLACE PROCEDURE count_phones ( " +
"CREATE OR REPLACE PROCEDURE sp_count_phones ( " +
" personId IN NUMBER, " +
" phoneCount OUT NUMBER ) " +
"AS " +
"BEGIN " +
" SELECT COUNT(*) INTO phoneCount " +
" FROM person_phone " +
" FROM phone " +
" WHERE person_id = personId; " +
"END;"
);
//tag::sql-sp-ref-cursor-oracle-example[]
statement.executeUpdate(
"CREATE OR REPLACE PROCEDURE person_phones ( " +
"CREATE OR REPLACE PROCEDURE sp_person_phones ( " +
" personId IN NUMBER, " +
" personPhones OUT SYS_REFCURSOR ) " +
"AS " +
"BEGIN " +
" OPEN personPhones FOR " +
" SELECT *" +
" FROM person_phone " +
" FROM phone " +
" WHERE person_id = personId; " +
"END;"
);
@ -85,7 +87,7 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
" phoneCount NUMBER; " +
"BEGIN " +
" SELECT COUNT(*) INTO phoneCount " +
" FROM person_phone " +
" FROM phone " +
" WHERE person_id = personId; " +
" RETURN( phoneCount ); " +
"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
public void testStoredProcedureOutParameter() {
doInJPA( this::entityManagerFactory, entityManager -> {
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("count_phones");
query.registerStoredProcedureParameter("personId", Long.class, ParameterMode.IN);
query.registerStoredProcedureParameter("phoneCount", Long.class, ParameterMode.OUT);
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_count_phones");
query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
query.registerStoredProcedureParameter(2, Long.class, ParameterMode.OUT);
query.setParameter("person_id", 1L);
query.setParameter(1, 1L);
query.execute();
Long phoneCount = (Long) query.getOutputParameterValue("phoneCount");
Long phoneCount = (Long) query.getOutputParameterValue(2);
assertEquals(Long.valueOf(2), phoneCount);
});
}
@ -136,7 +172,7 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
public void testStoredProcedureRefCursor() {
doInJPA( this::entityManagerFactory, entityManager -> {
//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( 2, Class.class, ParameterMode.REF_CURSOR );
query.setParameter( 1, 1L );
@ -154,7 +190,7 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
//tag::sql-hibernate-call-sp-ref-cursor-oracle-example[]
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(2, Class.class, ParameterMode.REF_CURSOR);
@ -167,12 +203,17 @@ public class OracleStoredProcedureTest extends BaseEntityManagerFunctionalTestCa
@Test
public void testStoredProcedureReturnValue() {
try {
doInJPA( this::entityManagerFactory, entityManager -> {
BigDecimal phoneCount = (BigDecimal) entityManager
.createNativeQuery("SELECT fn_count_phones(:personId) FROM DUAL")
.setParameter("personId", 1L)
.setParameter("personId", 1)
.getSingleResult();
assertEquals(BigDecimal.valueOf(2), phoneCount);
});
}
catch (Exception e) {
e.printStackTrace();
}
}
}

View File

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

View File

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

View File

@ -6,16 +6,16 @@
*/
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.Function;
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
@ -122,7 +122,7 @@ public class TransactionUtil {
txn.begin();
result = function.apply(entityManager);
txn.commit();
} catch (RuntimeException e) {
} catch (Throwable e) {
if ( txn != null && txn.isActive()) txn.rollback();
throw e;
} finally {
@ -149,7 +149,7 @@ public class TransactionUtil {
txn.begin();
function.accept(entityManager);
txn.commit();
} catch (RuntimeException e) {
} catch (Throwable e) {
if ( txn != null && txn.isActive()) txn.rollback();
throw e;
} finally {
@ -178,7 +178,7 @@ public class TransactionUtil {
result = function.apply(session);
txn.commit();
} catch (RuntimeException e) {
} catch (Throwable e) {
if ( txn != null ) txn.rollback();
throw e;
} finally {
@ -205,7 +205,7 @@ public class TransactionUtil {
function.accept(session);
txn.commit();
} catch (RuntimeException e) {
} catch (Throwable e) {
if ( txn != null ) txn.rollback();
throw e;
} 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