use more standard whitespace in example code in docs
This commit is contained in:
parent
0f9faf85e5
commit
0e55930c75
|
@ -45,28 +45,28 @@ public class BidirectionalTwoOneToManyMapsIdTest extends BaseEntityManagerFuncti
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person1 = new Person( "ABC-123" );
|
||||
Person person2 = new Person( "DEF-456" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person1 = new Person("ABC-123");
|
||||
Person person2 = new Person("DEF-456");
|
||||
|
||||
Address address1 = new Address( "12th Avenue", "12A", "4005A" );
|
||||
Address address2 = new Address( "18th Avenue", "18B", "4007B" );
|
||||
Address address1 = new Address("12th Avenue", "12A", "4005A");
|
||||
Address address2 = new Address("18th Avenue", "18B", "4007B");
|
||||
|
||||
entityManager.persist( person1 );
|
||||
entityManager.persist( person2 );
|
||||
entityManager.persist(person1);
|
||||
entityManager.persist(person2);
|
||||
|
||||
entityManager.persist( address1 );
|
||||
entityManager.persist( address2 );
|
||||
entityManager.persist(address1);
|
||||
entityManager.persist(address2);
|
||||
|
||||
person1.addAddress( address1 );
|
||||
person1.addAddress( address2 );
|
||||
person1.addAddress(address1);
|
||||
person1.addAddress(address2);
|
||||
|
||||
person2.addAddress( address1 );
|
||||
person2.addAddress(address1);
|
||||
|
||||
entityManager.flush();
|
||||
|
||||
person1.removeAddress( address1 );
|
||||
} );
|
||||
person1.removeAddress(address1);
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
@ -98,13 +98,13 @@ public class BidirectionalTwoOneToManyMapsIdTest extends BaseEntityManagerFuncti
|
|||
}
|
||||
|
||||
public void addAddress(Address address) {
|
||||
PersonAddress personAddress = new PersonAddress( this, address );
|
||||
addresses.add( personAddress );
|
||||
address.getOwners().add( personAddress );
|
||||
PersonAddress personAddress = new PersonAddress(this, address);
|
||||
addresses.add(personAddress);
|
||||
address.getOwners().add(personAddress);
|
||||
}
|
||||
|
||||
public void removeAddress(Address address) {
|
||||
for ( Iterator<PersonAddress> iterator = addresses.iterator(); iterator.hasNext(); ) {
|
||||
for (Iterator<PersonAddress> iterator = addresses.iterator(); iterator.hasNext();) {
|
||||
PersonAddress personAddress = iterator.next();
|
||||
if(personAddress.getPerson().equals(this) &&
|
||||
personAddress.getAddress().equals(address)) {
|
||||
|
@ -118,19 +118,19 @@ public class BidirectionalTwoOneToManyMapsIdTest extends BaseEntityManagerFuncti
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Person person = (Person) o;
|
||||
return Objects.equals( registrationNumber, person.registrationNumber );
|
||||
return Objects.equals(registrationNumber, person.registrationNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( registrationNumber );
|
||||
return Objects.hash(registrationNumber);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -159,20 +159,20 @@ public class BidirectionalTwoOneToManyMapsIdTest extends BaseEntityManagerFuncti
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
PersonAddressId that = (PersonAddressId) o;
|
||||
return Objects.equals( personId, that.personId ) &&
|
||||
Objects.equals( addressId, that.addressId );
|
||||
return Objects.equals(personId, that.personId) &&
|
||||
Objects.equals(addressId, that.addressId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( personId, addressId );
|
||||
return Objects.hash(personId, addressId);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -196,7 +196,7 @@ public class BidirectionalTwoOneToManyMapsIdTest extends BaseEntityManagerFuncti
|
|||
public PersonAddress(Person person, Address address) {
|
||||
this.person = person;
|
||||
this.address = address;
|
||||
this.id = new PersonAddressId( person.getId(), address.getId() );
|
||||
this.id = new PersonAddressId(person.getId(), address.getId());
|
||||
}
|
||||
|
||||
public PersonAddressId getId() {
|
||||
|
@ -221,20 +221,20 @@ public class BidirectionalTwoOneToManyMapsIdTest extends BaseEntityManagerFuncti
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
PersonAddress that = (PersonAddress) o;
|
||||
return Objects.equals( person, that.person ) &&
|
||||
Objects.equals( address, that.address );
|
||||
return Objects.equals(person, that.person) &&
|
||||
Objects.equals(address, that.address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( person, address );
|
||||
return Objects.hash(person, address);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -286,21 +286,21 @@ public class BidirectionalTwoOneToManyMapsIdTest extends BaseEntityManagerFuncti
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Address address = (Address) o;
|
||||
return Objects.equals( street, address.street ) &&
|
||||
Objects.equals( number, address.number ) &&
|
||||
Objects.equals( postalCode, address.postalCode );
|
||||
return Objects.equals(street, address.street) &&
|
||||
Objects.equals(number, address.number) &&
|
||||
Objects.equals(postalCode, address.postalCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( street, number, postalCode );
|
||||
return Objects.hash(street, number, postalCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -37,32 +37,32 @@ public class CompositeIdAssociationTest extends BaseEntityManagerFunctionalTestC
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
PersonAddress _personAddress = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person1 = new Person( "ABC-123" );
|
||||
Person person2 = new Person( "DEF-456" );
|
||||
PersonAddress _personAddress = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person1 = new Person("ABC-123");
|
||||
Person person2 = new Person("DEF-456");
|
||||
|
||||
Address address1 = new Address( "12th Avenue", "12A", "4005A" );
|
||||
Address address2 = new Address( "18th Avenue", "18B", "4007B" );
|
||||
Address address1 = new Address("12th Avenue", "12A", "4005A");
|
||||
Address address2 = new Address("18th Avenue", "18B", "4007B");
|
||||
|
||||
entityManager.persist( person1 );
|
||||
entityManager.persist( person2 );
|
||||
entityManager.persist(person1);
|
||||
entityManager.persist(person2);
|
||||
|
||||
entityManager.persist( address1 );
|
||||
entityManager.persist( address2 );
|
||||
entityManager.persist(address1);
|
||||
entityManager.persist(address2);
|
||||
|
||||
PersonAddress personAddress = new PersonAddress( person1, address1 );
|
||||
entityManager.persist( personAddress );
|
||||
PersonAddress personAddress = new PersonAddress(person1, address1);
|
||||
entityManager.persist(personAddress);
|
||||
return personAddress;
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Address address = entityManager.createQuery( "from Address", Address.class ).getResultList().get( 0 );
|
||||
Person person = entityManager.createQuery( "from Person", Person.class ).getResultList().get( 0 );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Address address = entityManager.createQuery("from Address", Address.class).getResultList().get(0);
|
||||
Person person = entityManager.createQuery("from Person", Person.class).getResultList().get(0);
|
||||
PersonAddress personAddress = entityManager.find(
|
||||
PersonAddress.class,
|
||||
new PersonAddress( person, address )
|
||||
new PersonAddress(person, address)
|
||||
);
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "PersonAddress")
|
||||
|
@ -102,20 +102,20 @@ public class CompositeIdAssociationTest extends BaseEntityManagerFunctionalTestC
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
PersonAddress that = (PersonAddress) o;
|
||||
return Objects.equals( person, that.person ) &&
|
||||
Objects.equals( address, that.address );
|
||||
return Objects.equals(person, that.person) &&
|
||||
Objects.equals(address, that.address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( person, address );
|
||||
return Objects.hash(person, address);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -142,19 +142,19 @@ public class CompositeIdAssociationTest extends BaseEntityManagerFunctionalTestC
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Person person = (Person) o;
|
||||
return Objects.equals( registrationNumber, person.registrationNumber );
|
||||
return Objects.equals(registrationNumber, person.registrationNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( registrationNumber );
|
||||
return Objects.hash(registrationNumber);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -199,21 +199,21 @@ public class CompositeIdAssociationTest extends BaseEntityManagerFunctionalTestC
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Address address = (Address) o;
|
||||
return Objects.equals( street, address.street ) &&
|
||||
Objects.equals( number, address.number ) &&
|
||||
Objects.equals( postalCode, address.postalCode );
|
||||
return Objects.equals(street, address.street) &&
|
||||
Objects.equals(number, address.number) &&
|
||||
Objects.equals(postalCode, address.postalCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( street, number, postalCode );
|
||||
return Objects.hash(street, number, postalCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,21 +35,21 @@ public class DerivedIdentifierPrimaryKeyJoinColumnTest extends BaseEntityManager
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
Long personId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( "ABC-123" );
|
||||
entityManager.persist( person );
|
||||
Long personId = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person("ABC-123");
|
||||
entityManager.persist(person);
|
||||
|
||||
PersonDetails details = new PersonDetails();
|
||||
details.setPerson( person );
|
||||
details.setPerson(person);
|
||||
|
||||
entityManager.persist( details );
|
||||
entityManager.persist(details);
|
||||
return person.getId();
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
PersonDetails details = entityManager.find( PersonDetails.class, personId );
|
||||
Assert.assertNotNull( details );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
PersonDetails details = entityManager.find(PersonDetails.class, personId);
|
||||
Assert.assertNotNull(details);
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
|
|
@ -35,22 +35,22 @@ public class DerivedIdentifierTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
Long personId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( "ABC-123" );
|
||||
Long personId = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person("ABC-123");
|
||||
|
||||
PersonDetails details = new PersonDetails();
|
||||
details.setPerson( person );
|
||||
details.setPerson(person);
|
||||
|
||||
entityManager.persist( person );
|
||||
entityManager.persist( details );
|
||||
entityManager.persist(person);
|
||||
entityManager.persist(details);
|
||||
|
||||
return person.getId();
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
PersonDetails details = entityManager.find( PersonDetails.class, personId );
|
||||
Assert.assertNotNull( details );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
PersonDetails details = entityManager.find(PersonDetails.class, personId);
|
||||
Assert.assertNotNull(details);
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
|
|
@ -41,50 +41,50 @@ public class JoinColumnOrFormulaTest extends BaseEntityManagerFunctionalTestCase
|
|||
public void testLifecycle() {
|
||||
//tag::associations-JoinColumnOrFormula-persistence-example[]
|
||||
Country US = new Country();
|
||||
US.setId( 1 );
|
||||
US.setDefault( true );
|
||||
US.setPrimaryLanguage( "English" );
|
||||
US.setName( "United States" );
|
||||
US.setId(1);
|
||||
US.setDefault(true);
|
||||
US.setPrimaryLanguage("English");
|
||||
US.setName("United States");
|
||||
|
||||
Country Romania = new Country();
|
||||
Romania.setId( 40 );
|
||||
Romania.setDefault( true );
|
||||
Romania.setName( "Romania" );
|
||||
Romania.setPrimaryLanguage( "Romanian" );
|
||||
Romania.setId(40);
|
||||
Romania.setDefault(true);
|
||||
Romania.setName("Romania");
|
||||
Romania.setPrimaryLanguage("Romanian");
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist( US );
|
||||
entityManager.persist( Romania );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist(US);
|
||||
entityManager.persist(Romania);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
User user1 = new User( );
|
||||
user1.setId( 1L );
|
||||
user1.setFirstName( "John" );
|
||||
user1.setLastName( "Doe" );
|
||||
user1.setLanguage( "English" );
|
||||
entityManager.persist( user1 );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
User user1 = new User();
|
||||
user1.setId(1L);
|
||||
user1.setFirstName("John");
|
||||
user1.setLastName("Doe");
|
||||
user1.setLanguage("English");
|
||||
entityManager.persist(user1);
|
||||
|
||||
User user2 = new User( );
|
||||
user2.setId( 2L );
|
||||
user2.setFirstName( "Vlad" );
|
||||
user2.setLastName( "Mihalcea" );
|
||||
user2.setLanguage( "Romanian" );
|
||||
entityManager.persist( user2 );
|
||||
User user2 = new User();
|
||||
user2.setId(2L);
|
||||
user2.setFirstName("Vlad");
|
||||
user2.setLastName("Mihalcea");
|
||||
user2.setLanguage("Romanian");
|
||||
entityManager.persist(user2);
|
||||
|
||||
} );
|
||||
});
|
||||
//end::associations-JoinColumnOrFormula-persistence-example[]
|
||||
|
||||
//tag::associations-JoinColumnOrFormula-fetching-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Fetch User entities" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Fetch User entities");
|
||||
|
||||
User john = entityManager.find( User.class, 1L );
|
||||
assertEquals( US, john.getCountry());
|
||||
User john = entityManager.find(User.class, 1L);
|
||||
assertEquals(US, john.getCountry());
|
||||
|
||||
User vlad = entityManager.find( User.class, 2L );
|
||||
assertEquals( Romania, vlad.getCountry());
|
||||
} );
|
||||
User vlad = entityManager.find(User.class, 2L);
|
||||
assertEquals(Romania, vlad.getCountry());
|
||||
});
|
||||
//end::associations-JoinColumnOrFormula-fetching-example[]
|
||||
}
|
||||
|
||||
|
@ -103,7 +103,7 @@ public class JoinColumnOrFormulaTest extends BaseEntityManagerFunctionalTestCase
|
|||
private String language;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumnOrFormula( column =
|
||||
@JoinColumnOrFormula(column =
|
||||
@JoinColumn(
|
||||
name = "language",
|
||||
referencedColumnName = "primaryLanguage",
|
||||
|
@ -111,7 +111,7 @@ public class JoinColumnOrFormulaTest extends BaseEntityManagerFunctionalTestCase
|
|||
updatable = false
|
||||
)
|
||||
)
|
||||
@JoinColumnOrFormula( formula =
|
||||
@JoinColumnOrFormula(formula =
|
||||
@JoinFormula(
|
||||
value = "true",
|
||||
referencedColumnName = "is_default"
|
||||
|
@ -220,19 +220,19 @@ public class JoinColumnOrFormulaTest extends BaseEntityManagerFunctionalTestCase
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( !( o instanceof Country ) ) {
|
||||
if (!(o instanceof Country)) {
|
||||
return false;
|
||||
}
|
||||
Country country = (Country) o;
|
||||
return Objects.equals( getId(), country.getId() );
|
||||
return Objects.equals(getId(), country.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( getId() );
|
||||
return Objects.hash(getId());
|
||||
}
|
||||
//tag::associations-JoinColumnOrFormula-example[]
|
||||
}
|
||||
|
|
|
@ -40,45 +40,45 @@ public class JoinFormulaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
public void testLifecycle() {
|
||||
//tag::associations-JoinFormula-persistence-example[]
|
||||
Country US = new Country();
|
||||
US.setId( 1 );
|
||||
US.setName( "United States" );
|
||||
US.setId(1);
|
||||
US.setName("United States");
|
||||
|
||||
Country Romania = new Country();
|
||||
Romania.setId( 40 );
|
||||
Romania.setName( "Romania" );
|
||||
Romania.setId(40);
|
||||
Romania.setName("Romania");
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist( US );
|
||||
entityManager.persist( Romania );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist(US);
|
||||
entityManager.persist(Romania);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
User user1 = new User( );
|
||||
user1.setId( 1L );
|
||||
user1.setFirstName( "John" );
|
||||
user1.setLastName( "Doe" );
|
||||
user1.setPhoneNumber( "+1-234-5678" );
|
||||
entityManager.persist( user1 );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
User user1 = new User();
|
||||
user1.setId(1L);
|
||||
user1.setFirstName("John");
|
||||
user1.setLastName("Doe");
|
||||
user1.setPhoneNumber("+1-234-5678");
|
||||
entityManager.persist(user1);
|
||||
|
||||
User user2 = new User( );
|
||||
user2.setId( 2L );
|
||||
user2.setFirstName( "Vlad" );
|
||||
user2.setLastName( "Mihalcea" );
|
||||
user2.setPhoneNumber( "+40-123-4567" );
|
||||
entityManager.persist( user2 );
|
||||
} );
|
||||
User user2 = new User();
|
||||
user2.setId(2L);
|
||||
user2.setFirstName("Vlad");
|
||||
user2.setLastName("Mihalcea");
|
||||
user2.setPhoneNumber("+40-123-4567");
|
||||
entityManager.persist(user2);
|
||||
});
|
||||
//end::associations-JoinFormula-persistence-example[]
|
||||
|
||||
//tag::associations-JoinFormula-fetching-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Fetch User entities" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Fetch User entities");
|
||||
|
||||
User john = entityManager.find( User.class, 1L );
|
||||
assertEquals( US, john.getCountry());
|
||||
User john = entityManager.find(User.class, 1L);
|
||||
assertEquals(US, john.getCountry());
|
||||
|
||||
User vlad = entityManager.find( User.class, 2L );
|
||||
assertEquals( Romania, vlad.getCountry());
|
||||
} );
|
||||
User vlad = entityManager.find(User.class, 2L);
|
||||
assertEquals(Romania, vlad.getCountry());
|
||||
});
|
||||
//end::associations-JoinFormula-fetching-example[]
|
||||
}
|
||||
|
||||
|
@ -97,7 +97,7 @@ public class JoinFormulaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
private String phoneNumber;
|
||||
|
||||
@ManyToOne
|
||||
@JoinFormula( "REGEXP_REPLACE(phoneNumber, '\\+(\\d+)-.*', '\\1')::int" )
|
||||
@JoinFormula("REGEXP_REPLACE(phoneNumber, '\\+(\\d+)-.*', '\\1')::int")
|
||||
private Country country;
|
||||
|
||||
//Getters and setters omitted for brevity
|
||||
|
@ -176,19 +176,19 @@ public class JoinFormulaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( !( o instanceof Country ) ) {
|
||||
if (!(o instanceof Country)) {
|
||||
return false;
|
||||
}
|
||||
Country country = (Country) o;
|
||||
return Objects.equals( getId(), country.getId() );
|
||||
return Objects.equals(getId(), country.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( getId() );
|
||||
return Objects.hash(getId());
|
||||
}
|
||||
//tag::associations-JoinFormula-example[]
|
||||
}
|
||||
|
|
|
@ -38,27 +38,27 @@ public class ManyToManyBidirectionalTest extends BaseEntityManagerFunctionalTest
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::associations-many-to-many-bidirectional-lifecycle-example[]
|
||||
Person person1 = new Person( "ABC-123" );
|
||||
Person person2 = new Person( "DEF-456" );
|
||||
Person person1 = new Person("ABC-123");
|
||||
Person person2 = new Person("DEF-456");
|
||||
|
||||
Address address1 = new Address( "12th Avenue", "12A", "4005A" );
|
||||
Address address2 = new Address( "18th Avenue", "18B", "4007B" );
|
||||
Address address1 = new Address("12th Avenue", "12A", "4005A");
|
||||
Address address2 = new Address("18th Avenue", "18B", "4007B");
|
||||
|
||||
person1.addAddress( address1 );
|
||||
person1.addAddress( address2 );
|
||||
person1.addAddress(address1);
|
||||
person1.addAddress(address2);
|
||||
|
||||
person2.addAddress( address1 );
|
||||
person2.addAddress(address1);
|
||||
|
||||
entityManager.persist( person1 );
|
||||
entityManager.persist( person2 );
|
||||
entityManager.persist(person1);
|
||||
entityManager.persist(person2);
|
||||
|
||||
entityManager.flush();
|
||||
|
||||
person1.removeAddress( address1 );
|
||||
person1.removeAddress(address1);
|
||||
//end::associations-many-to-many-bidirectional-lifecycle-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::associations-many-to-many-bidirectional-example[]
|
||||
|
@ -92,30 +92,30 @@ public class ManyToManyBidirectionalTest extends BaseEntityManagerFunctionalTest
|
|||
|
||||
//tag::associations-many-to-many-bidirectional-example[]
|
||||
public void addAddress(Address address) {
|
||||
addresses.add( address );
|
||||
address.getOwners().add( this );
|
||||
addresses.add(address);
|
||||
address.getOwners().add(this);
|
||||
}
|
||||
|
||||
public void removeAddress(Address address) {
|
||||
addresses.remove( address );
|
||||
address.getOwners().remove( this );
|
||||
addresses.remove(address);
|
||||
address.getOwners().remove(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Person person = (Person) o;
|
||||
return Objects.equals( registrationNumber, person.registrationNumber );
|
||||
return Objects.equals(registrationNumber, person.registrationNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( registrationNumber );
|
||||
return Objects.hash(registrationNumber);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -172,21 +172,21 @@ public class ManyToManyBidirectionalTest extends BaseEntityManagerFunctionalTest
|
|||
//tag::associations-many-to-many-bidirectional-example[]
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Address address = (Address) o;
|
||||
return Objects.equals( street, address.street ) &&
|
||||
Objects.equals( number, address.number ) &&
|
||||
Objects.equals( postalCode, address.postalCode );
|
||||
return Objects.equals(street, address.street) &&
|
||||
Objects.equals(number, address.number) &&
|
||||
Objects.equals(postalCode, address.postalCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( street, number, postalCode );
|
||||
return Objects.hash(street, number, postalCode);
|
||||
}
|
||||
}
|
||||
//end::associations-many-to-many-bidirectional-example[]
|
||||
|
|
|
@ -41,31 +41,31 @@ public class ManyToManyBidirectionalWithLinkEntityTest extends BaseEntityManager
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::associations-many-to-many-bidirectional-with-link-entity-lifecycle-example[]
|
||||
Person person1 = new Person( "ABC-123" );
|
||||
Person person2 = new Person( "DEF-456" );
|
||||
Person person1 = new Person("ABC-123");
|
||||
Person person2 = new Person("DEF-456");
|
||||
|
||||
Address address1 = new Address( "12th Avenue", "12A", "4005A" );
|
||||
Address address2 = new Address( "18th Avenue", "18B", "4007B" );
|
||||
Address address1 = new Address("12th Avenue", "12A", "4005A");
|
||||
Address address2 = new Address("18th Avenue", "18B", "4007B");
|
||||
|
||||
entityManager.persist( person1 );
|
||||
entityManager.persist( person2 );
|
||||
entityManager.persist(person1);
|
||||
entityManager.persist(person2);
|
||||
|
||||
entityManager.persist( address1 );
|
||||
entityManager.persist( address2 );
|
||||
entityManager.persist(address1);
|
||||
entityManager.persist(address2);
|
||||
|
||||
person1.addAddress( address1 );
|
||||
person1.addAddress( address2 );
|
||||
person1.addAddress(address1);
|
||||
person1.addAddress(address2);
|
||||
|
||||
person2.addAddress( address1 );
|
||||
person2.addAddress(address1);
|
||||
|
||||
entityManager.flush();
|
||||
|
||||
log.info( "Removing address" );
|
||||
person1.removeAddress( address1 );
|
||||
log.info("Removing address");
|
||||
person1.removeAddress(address1);
|
||||
//end::associations-many-to-many-bidirectional-with-link-entity-lifecycle-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::associations-many-to-many-bidirectional-with-link-entity-example[]
|
||||
|
@ -107,34 +107,34 @@ public class ManyToManyBidirectionalWithLinkEntityTest extends BaseEntityManager
|
|||
|
||||
//tag::associations-many-to-many-bidirectional-with-link-entity-example[]
|
||||
public void addAddress(Address address) {
|
||||
PersonAddress personAddress = new PersonAddress( this, address );
|
||||
addresses.add( personAddress );
|
||||
address.getOwners().add( personAddress );
|
||||
PersonAddress personAddress = new PersonAddress(this, address);
|
||||
addresses.add(personAddress);
|
||||
address.getOwners().add(personAddress);
|
||||
}
|
||||
|
||||
public void removeAddress(Address address) {
|
||||
PersonAddress personAddress = new PersonAddress( this, address );
|
||||
address.getOwners().remove( personAddress );
|
||||
addresses.remove( personAddress );
|
||||
personAddress.setPerson( null );
|
||||
personAddress.setAddress( null );
|
||||
PersonAddress personAddress = new PersonAddress(this, address);
|
||||
address.getOwners().remove(personAddress);
|
||||
addresses.remove(personAddress);
|
||||
personAddress.setPerson(null);
|
||||
personAddress.setAddress(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Person person = (Person) o;
|
||||
return Objects.equals( registrationNumber, person.registrationNumber );
|
||||
return Objects.equals(registrationNumber, person.registrationNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( registrationNumber );
|
||||
return Objects.hash(registrationNumber);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -180,20 +180,20 @@ public class ManyToManyBidirectionalWithLinkEntityTest extends BaseEntityManager
|
|||
//tag::associations-many-to-many-bidirectional-with-link-entity-example[]
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
PersonAddress that = (PersonAddress) o;
|
||||
return Objects.equals( person, that.person ) &&
|
||||
Objects.equals( address, that.address );
|
||||
return Objects.equals(person, that.person) &&
|
||||
Objects.equals(address, that.address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( person, address );
|
||||
return Objects.hash(person, address);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -254,21 +254,21 @@ public class ManyToManyBidirectionalWithLinkEntityTest extends BaseEntityManager
|
|||
//tag::associations-many-to-many-bidirectional-with-link-entity-example[]
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Address address = (Address) o;
|
||||
return Objects.equals( street, address.street ) &&
|
||||
Objects.equals( number, address.number ) &&
|
||||
Objects.equals( postalCode, address.postalCode );
|
||||
return Objects.equals(street, address.street) &&
|
||||
Objects.equals(number, address.number) &&
|
||||
Objects.equals(postalCode, address.postalCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( street, number, postalCode );
|
||||
return Objects.hash(street, number, postalCode);
|
||||
}
|
||||
}
|
||||
//end::associations-many-to-many-bidirectional-with-link-entity-example[]
|
||||
|
|
|
@ -36,55 +36,55 @@ public class ManyToManyUnidirectionalTest extends BaseEntityManagerFunctionalTes
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::associations-many-to-many-unidirectional-lifecycle-example[]
|
||||
Person person1 = new Person();
|
||||
Person person2 = new Person();
|
||||
|
||||
Address address1 = new Address( "12th Avenue", "12A" );
|
||||
Address address2 = new Address( "18th Avenue", "18B" );
|
||||
Address address1 = new Address("12th Avenue", "12A");
|
||||
Address address2 = new Address("18th Avenue", "18B");
|
||||
|
||||
person1.getAddresses().add( address1 );
|
||||
person1.getAddresses().add( address2 );
|
||||
person1.getAddresses().add(address1);
|
||||
person1.getAddresses().add(address2);
|
||||
|
||||
person2.getAddresses().add( address1 );
|
||||
person2.getAddresses().add(address1);
|
||||
|
||||
entityManager.persist( person1 );
|
||||
entityManager.persist( person2 );
|
||||
entityManager.persist(person1);
|
||||
entityManager.persist(person2);
|
||||
|
||||
entityManager.flush();
|
||||
|
||||
person1.getAddresses().remove( address1 );
|
||||
person1.getAddresses().remove(address1);
|
||||
//end::associations-many-to-many-unidirectional-lifecycle-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
final Long personId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
final Long personId = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person1 = new Person();
|
||||
Person person2 = new Person();
|
||||
|
||||
Address address1 = new Address( "12th Avenue", "12A" );
|
||||
Address address2 = new Address( "18th Avenue", "18B" );
|
||||
Address address1 = new Address("12th Avenue", "12A");
|
||||
Address address2 = new Address("18th Avenue", "18B");
|
||||
|
||||
person1.getAddresses().add( address1 );
|
||||
person1.getAddresses().add( address2 );
|
||||
person1.getAddresses().add(address1);
|
||||
person1.getAddresses().add(address2);
|
||||
|
||||
person2.getAddresses().add( address1 );
|
||||
person2.getAddresses().add(address1);
|
||||
|
||||
entityManager.persist( person1 );
|
||||
entityManager.persist( person2 );
|
||||
entityManager.persist(person1);
|
||||
entityManager.persist(person2);
|
||||
|
||||
return person1.id;
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Remove" );
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Remove");
|
||||
//tag::associations-many-to-many-unidirectional-remove-example[]
|
||||
Person person1 = entityManager.find( Person.class, personId );
|
||||
entityManager.remove( person1 );
|
||||
Person person1 = entityManager.find(Person.class, personId);
|
||||
entityManager.remove(person1);
|
||||
//end::associations-many-to-many-unidirectional-remove-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::associations-many-to-many-unidirectional-example[]
|
||||
|
|
|
@ -35,19 +35,19 @@ public class ManyToOneTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::associations-many-to-one-lifecycle-example[]
|
||||
Person person = new Person();
|
||||
entityManager.persist( person );
|
||||
entityManager.persist(person);
|
||||
|
||||
Phone phone = new Phone( "123-456-7890" );
|
||||
phone.setPerson( person );
|
||||
entityManager.persist( phone );
|
||||
Phone phone = new Phone("123-456-7890");
|
||||
phone.setPerson(person);
|
||||
entityManager.persist(phone);
|
||||
|
||||
entityManager.flush();
|
||||
phone.setPerson( null );
|
||||
phone.setPerson(null);
|
||||
//end::associations-many-to-one-lifecycle-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::associations-many-to-one-example[]
|
||||
|
|
|
@ -34,45 +34,45 @@ public class NotFoundTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::associations-not-found-persist-example[]
|
||||
City _NewYork = new City();
|
||||
_NewYork.setName( "New York" );
|
||||
entityManager.persist( _NewYork );
|
||||
_NewYork.setName("New York");
|
||||
entityManager.persist(_NewYork);
|
||||
|
||||
Person person = new Person();
|
||||
person.setId( 1L );
|
||||
person.setName( "John Doe" );
|
||||
person.setCityName( "New York" );
|
||||
entityManager.persist( person );
|
||||
person.setId(1L);
|
||||
person.setName("John Doe");
|
||||
person.setCityName("New York");
|
||||
entityManager.persist(person);
|
||||
//end::associations-not-found-persist-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::associations-not-found-find-example[]
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
assertEquals( "New York", person.getCity().getName() );
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
assertEquals("New York", person.getCity().getName());
|
||||
//end::associations-not-found-find-example[]
|
||||
|
||||
//tag::associations-not-found-non-existing-persist-example[]
|
||||
person.setCityName( "Atlantis" );
|
||||
person.setCityName("Atlantis");
|
||||
//end::associations-not-found-non-existing-persist-example[]
|
||||
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::associations-not-found-non-existing-find-example[]
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
|
||||
assertEquals( "Atlantis", person.getCityName() );
|
||||
assertNull( null, person.getCity() );
|
||||
assertEquals("Atlantis", person.getCityName());
|
||||
assertNull(null, person.getCity());
|
||||
//end::associations-not-found-non-existing-find-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::associations-not-found-domain-model-example[]
|
||||
@Entity
|
||||
@Table( name = "Person" )
|
||||
@Table(name = "Person")
|
||||
public static class Person {
|
||||
|
||||
@Id
|
||||
|
@ -83,7 +83,7 @@ public class NotFoundTest extends BaseEntityManagerFunctionalTestCase {
|
|||
private String cityName;
|
||||
|
||||
@ManyToOne
|
||||
@NotFound ( action = NotFoundAction.IGNORE )
|
||||
@NotFound (action = NotFoundAction.IGNORE)
|
||||
@JoinColumn(
|
||||
name = "cityName",
|
||||
referencedColumnName = "name",
|
||||
|
@ -128,7 +128,7 @@ public class NotFoundTest extends BaseEntityManagerFunctionalTestCase {
|
|||
}
|
||||
|
||||
@Entity
|
||||
@Table( name = "City" )
|
||||
@Table(name = "City")
|
||||
public static class City implements Serializable {
|
||||
|
||||
@Id
|
||||
|
|
|
@ -39,20 +39,20 @@ public class OneToManyBidirectionalTest extends BaseEntityManagerFunctionalTestC
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::associations-one-to-many-bidirectional-lifecycle-example[]
|
||||
Person person = new Person();
|
||||
Phone phone1 = new Phone( "123-456-7890" );
|
||||
Phone phone2 = new Phone( "321-654-0987" );
|
||||
Phone phone1 = new Phone("123-456-7890");
|
||||
Phone phone2 = new Phone("321-654-0987");
|
||||
|
||||
person.addPhone( phone1 );
|
||||
person.addPhone( phone2 );
|
||||
entityManager.persist( person );
|
||||
person.addPhone(phone1);
|
||||
person.addPhone(phone2);
|
||||
entityManager.persist(person);
|
||||
entityManager.flush();
|
||||
|
||||
person.removePhone( phone1 );
|
||||
person.removePhone(phone1);
|
||||
//end::associations-one-to-many-bidirectional-lifecycle-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::associations-one-to-many-bidirectional-example[]
|
||||
|
@ -83,13 +83,13 @@ public class OneToManyBidirectionalTest extends BaseEntityManagerFunctionalTestC
|
|||
|
||||
//tag::associations-one-to-many-bidirectional-example[]
|
||||
public void addPhone(Phone phone) {
|
||||
phones.add( phone );
|
||||
phone.setPerson( this );
|
||||
phones.add(phone);
|
||||
phone.setPerson(this);
|
||||
}
|
||||
|
||||
public void removePhone(Phone phone) {
|
||||
phones.remove( phone );
|
||||
phone.setPerson( null );
|
||||
phones.remove(phone);
|
||||
phone.setPerson(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -137,19 +137,19 @@ public class OneToManyBidirectionalTest extends BaseEntityManagerFunctionalTestC
|
|||
//tag::associations-one-to-many-bidirectional-example[]
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
//end::associations-one-to-many-bidirectional-example[]
|
||||
|
|
|
@ -36,20 +36,20 @@ public class OneToManyUnidirectionalTest extends BaseEntityManagerFunctionalTest
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::associations-one-to-many-unidirectional-lifecycle-example[]
|
||||
Person person = new Person();
|
||||
Phone phone1 = new Phone( "123-456-7890" );
|
||||
Phone phone2 = new Phone( "321-654-0987" );
|
||||
Phone phone1 = new Phone("123-456-7890");
|
||||
Phone phone2 = new Phone("321-654-0987");
|
||||
|
||||
person.getPhones().add( phone1 );
|
||||
person.getPhones().add( phone2 );
|
||||
entityManager.persist( person );
|
||||
person.getPhones().add(phone1);
|
||||
person.getPhones().add(phone2);
|
||||
entityManager.persist(person);
|
||||
entityManager.flush();
|
||||
|
||||
person.getPhones().remove( phone1 );
|
||||
person.getPhones().remove(phone1);
|
||||
//end::associations-one-to-many-unidirectional-lifecycle-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::associations-one-to-many-unidirectional-example[]
|
||||
|
|
|
@ -58,7 +58,7 @@ public class OneToOneBidirectionalLazyTest extends BaseEntityManagerFunctionalTe
|
|||
orphanRemoval = true,
|
||||
fetch = FetchType.LAZY
|
||||
)
|
||||
@LazyToOne( LazyToOneOption.NO_PROXY )
|
||||
@LazyToOne(LazyToOneOption.NO_PROXY)
|
||||
private PhoneDetails details;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
@ -86,13 +86,13 @@ public class OneToOneBidirectionalLazyTest extends BaseEntityManagerFunctionalTe
|
|||
|
||||
//tag::associations-one-to-one-bidirectional-lazy-example[]
|
||||
public void addDetails(PhoneDetails details) {
|
||||
details.setPhone( this );
|
||||
details.setPhone(this);
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
public void removeDetails() {
|
||||
if ( details != null ) {
|
||||
details.setPhone( null );
|
||||
if (details != null) {
|
||||
details.setPhone(null);
|
||||
this.details = null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,47 +37,47 @@ public class OneToOneBidirectionalTest extends BaseEntityManagerFunctionalTestCa
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Phone phone = new Phone( "123-456-7890" );
|
||||
PhoneDetails details = new PhoneDetails( "T-Mobile", "GSM" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Phone phone = new Phone("123-456-7890");
|
||||
PhoneDetails details = new PhoneDetails("T-Mobile", "GSM");
|
||||
|
||||
phone.addDetails( details );
|
||||
entityManager.persist( phone );
|
||||
} );
|
||||
phone.addDetails(details);
|
||||
entityManager.persist(phone);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstraint() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::associations-one-to-one-bidirectional-lifecycle-example[]
|
||||
Phone phone = new Phone( "123-456-7890" );
|
||||
PhoneDetails details = new PhoneDetails( "T-Mobile", "GSM" );
|
||||
Phone phone = new Phone("123-456-7890");
|
||||
PhoneDetails details = new PhoneDetails("T-Mobile", "GSM");
|
||||
|
||||
phone.addDetails( details );
|
||||
entityManager.persist( phone );
|
||||
phone.addDetails(details);
|
||||
entityManager.persist(phone);
|
||||
//end::associations-one-to-one-bidirectional-lifecycle-example[]
|
||||
} );
|
||||
});
|
||||
try {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
|
||||
Phone phone = entityManager.find( Phone.class, 1L );
|
||||
Phone phone = entityManager.find(Phone.class, 1L);
|
||||
|
||||
//tag::associations-one-to-one-bidirectional-constraint-example[]
|
||||
PhoneDetails otherDetails = new PhoneDetails( "T-Mobile", "CDMA" );
|
||||
otherDetails.setPhone( phone );
|
||||
entityManager.persist( otherDetails );
|
||||
PhoneDetails otherDetails = new PhoneDetails("T-Mobile", "CDMA");
|
||||
otherDetails.setPhone(phone);
|
||||
entityManager.persist(otherDetails);
|
||||
entityManager.flush();
|
||||
entityManager.clear();
|
||||
|
||||
//throws jakarta.persistence.PersistenceException: org.hibernate.HibernateException: More than one row with the given identifier was found: 1
|
||||
phone = entityManager.find( Phone.class, phone.getId() );
|
||||
phone = entityManager.find(Phone.class, phone.getId());
|
||||
//end::associations-one-to-one-bidirectional-constraint-example[]
|
||||
phone.getDetails().getProvider();
|
||||
} );
|
||||
Assert.fail( "Expected: HHH000327: Error performing load command : org.hibernate.HibernateException: More than one row with the given identifier was found: 1" );
|
||||
});
|
||||
Assert.fail("Expected: HHH000327: Error performing load command : org.hibernate.HibernateException: More than one row with the given identifier was found: 1");
|
||||
}
|
||||
catch (Exception expected) {
|
||||
log.error( "Expected", expected );
|
||||
log.error("Expected", expected);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -125,13 +125,13 @@ public class OneToOneBidirectionalTest extends BaseEntityManagerFunctionalTestCa
|
|||
|
||||
//tag::associations-one-to-one-bidirectional-example[]
|
||||
public void addDetails(PhoneDetails details) {
|
||||
details.setPhone( this );
|
||||
details.setPhone(this);
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
public void removeDetails() {
|
||||
if ( details != null ) {
|
||||
details.setPhone( null );
|
||||
if (details != null) {
|
||||
details.setPhone(null);
|
||||
this.details = null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,23 +36,23 @@ public class OneToOneMapsIdTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void testLifecycle() {
|
||||
//tag::identifiers-derived-mapsid-persist-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( "ABC-123" );
|
||||
person.setId( 1L );
|
||||
entityManager.persist( person );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person("ABC-123");
|
||||
person.setId(1L);
|
||||
entityManager.persist(person);
|
||||
|
||||
PersonDetails personDetails = new PersonDetails();
|
||||
personDetails.setNickName( "John Doe" );
|
||||
personDetails.setPerson( person );
|
||||
personDetails.setNickName("John Doe");
|
||||
personDetails.setPerson(person);
|
||||
|
||||
entityManager.persist( personDetails );
|
||||
} );
|
||||
entityManager.persist(personDetails);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
PersonDetails personDetails = entityManager.find( PersonDetails.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
PersonDetails personDetails = entityManager.find(PersonDetails.class, 1L);
|
||||
|
||||
assertEquals("John Doe", personDetails.getNickName());
|
||||
} );
|
||||
});
|
||||
//end::identifiers-derived-mapsid-persist-example[]
|
||||
}
|
||||
|
||||
|
|
|
@ -36,23 +36,23 @@ public class OneToOnePrimaryKeyJoinColumnTest extends BaseEntityManagerFunctiona
|
|||
@Test
|
||||
public void testLifecycle() {
|
||||
//tag::identifiers-derived-primarykeyjoincolumn-persist-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( "ABC-123" );
|
||||
person.setId( 1L );
|
||||
entityManager.persist( person );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person("ABC-123");
|
||||
person.setId(1L);
|
||||
entityManager.persist(person);
|
||||
|
||||
PersonDetails personDetails = new PersonDetails();
|
||||
personDetails.setNickName( "John Doe" );
|
||||
personDetails.setPerson( person );
|
||||
personDetails.setNickName("John Doe");
|
||||
personDetails.setPerson(person);
|
||||
|
||||
entityManager.persist( personDetails );
|
||||
} );
|
||||
entityManager.persist(personDetails);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
PersonDetails personDetails = entityManager.find( PersonDetails.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
PersonDetails personDetails = entityManager.find(PersonDetails.class, 1L);
|
||||
|
||||
assertEquals("John Doe", personDetails.getNickName());
|
||||
} );
|
||||
});
|
||||
//end::identifiers-derived-primarykeyjoincolumn-persist-example[]
|
||||
}
|
||||
|
||||
|
|
|
@ -34,14 +34,14 @@ public class OneToOneUnidirectionalTest extends BaseEntityManagerFunctionalTestC
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Phone phone = new Phone( "123-456-7890" );
|
||||
PhoneDetails details = new PhoneDetails( "T-Mobile", "GSM" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Phone phone = new Phone("123-456-7890");
|
||||
PhoneDetails details = new PhoneDetails("T-Mobile", "GSM");
|
||||
|
||||
phone.setDetails( details );
|
||||
entityManager.persist( phone );
|
||||
entityManager.persist( details );
|
||||
} );
|
||||
phone.setDetails(details);
|
||||
entityManager.persist(phone);
|
||||
entityManager.persist(details);
|
||||
});
|
||||
}
|
||||
|
||||
//tag::associations-one-to-one-unidirectional-example[]
|
||||
|
|
|
@ -37,31 +37,31 @@ public class UnidirectionalManyToManyRemoveTest extends BaseEntityManagerFunctio
|
|||
@Test
|
||||
public void testRemove() {
|
||||
try {
|
||||
final Long personId = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
final Long personId = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person1 = new Person();
|
||||
Person person2 = new Person();
|
||||
|
||||
Address address1 = new Address( "12th Avenue", "12A" );
|
||||
Address address2 = new Address( "18th Avenue", "18B" );
|
||||
Address address1 = new Address("12th Avenue", "12A");
|
||||
Address address2 = new Address("18th Avenue", "18B");
|
||||
|
||||
person1.getAddresses().add( address1 );
|
||||
person1.getAddresses().add( address2 );
|
||||
person1.getAddresses().add(address1);
|
||||
person1.getAddresses().add(address2);
|
||||
|
||||
person2.getAddresses().add( address1 );
|
||||
person2.getAddresses().add(address1);
|
||||
|
||||
entityManager.persist( person1 );
|
||||
entityManager.persist( person2 );
|
||||
entityManager.persist(person1);
|
||||
entityManager.persist(person2);
|
||||
|
||||
return person1.id;
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
|
||||
Person person1 = entityManager.find( Person.class, personId );
|
||||
entityManager.remove( person1 );
|
||||
} );
|
||||
Person person1 = entityManager.find(Person.class, personId);
|
||||
entityManager.remove(person1);
|
||||
});
|
||||
}
|
||||
catch (Exception expected) {
|
||||
log.error( "Expected", expected );
|
||||
log.error("Expected", expected);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -36,38 +36,38 @@ public class AnyTest extends BaseCoreFunctionalTestCase {
|
|||
@Test
|
||||
public void test() {
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
doInHibernate(this::sessionFactory, session -> {
|
||||
//tag::associations-any-persist-example[]
|
||||
IntegerProperty ageProperty = new IntegerProperty();
|
||||
ageProperty.setId( 1L );
|
||||
ageProperty.setName( "age" );
|
||||
ageProperty.setValue( 23 );
|
||||
ageProperty.setId(1L);
|
||||
ageProperty.setName("age");
|
||||
ageProperty.setValue(23);
|
||||
|
||||
session.persist( ageProperty );
|
||||
session.persist(ageProperty);
|
||||
|
||||
StringProperty nameProperty = new StringProperty();
|
||||
nameProperty.setId( 1L );
|
||||
nameProperty.setName( "name" );
|
||||
nameProperty.setValue( "John Doe" );
|
||||
nameProperty.setId(1L);
|
||||
nameProperty.setName("name");
|
||||
nameProperty.setValue("John Doe");
|
||||
|
||||
session.persist( nameProperty );
|
||||
session.persist(nameProperty);
|
||||
|
||||
PropertyHolder namePropertyHolder = new PropertyHolder();
|
||||
namePropertyHolder.setId( 1L );
|
||||
namePropertyHolder.setProperty( nameProperty );
|
||||
namePropertyHolder.setId(1L);
|
||||
namePropertyHolder.setProperty(nameProperty);
|
||||
|
||||
session.persist( namePropertyHolder );
|
||||
session.persist(namePropertyHolder);
|
||||
//end::associations-any-persist-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
doInHibernate(this::sessionFactory, session -> {
|
||||
//tag::associations-any-query-example[]
|
||||
PropertyHolder propertyHolder = session.get( PropertyHolder.class, 1L );
|
||||
PropertyHolder propertyHolder = session.get(PropertyHolder.class, 1L);
|
||||
|
||||
assertEquals("name", propertyHolder.getProperty().getName());
|
||||
assertEquals("John Doe", propertyHolder.getProperty().getValue());
|
||||
//end::associations-any-query-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -37,43 +37,43 @@ public class ManyToAnyTest extends BaseCoreFunctionalTestCase {
|
|||
@Test
|
||||
public void test() {
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
doInHibernate(this::sessionFactory, session -> {
|
||||
//tag::associations-many-to-any-persist-example[]
|
||||
IntegerProperty ageProperty = new IntegerProperty();
|
||||
ageProperty.setId( 1L );
|
||||
ageProperty.setName( "age" );
|
||||
ageProperty.setValue( 23 );
|
||||
ageProperty.setId(1L);
|
||||
ageProperty.setName("age");
|
||||
ageProperty.setValue(23);
|
||||
|
||||
session.persist( ageProperty );
|
||||
session.persist(ageProperty);
|
||||
|
||||
StringProperty nameProperty = new StringProperty();
|
||||
nameProperty.setId( 1L );
|
||||
nameProperty.setName( "name" );
|
||||
nameProperty.setValue( "John Doe" );
|
||||
nameProperty.setId(1L);
|
||||
nameProperty.setName("name");
|
||||
nameProperty.setValue("John Doe");
|
||||
|
||||
session.persist( nameProperty );
|
||||
session.persist(nameProperty);
|
||||
|
||||
PropertyRepository propertyRepository = new PropertyRepository();
|
||||
propertyRepository.setId( 1L );
|
||||
propertyRepository.setId(1L);
|
||||
|
||||
propertyRepository.getProperties().add( ageProperty );
|
||||
propertyRepository.getProperties().add( nameProperty );
|
||||
propertyRepository.getProperties().add(ageProperty);
|
||||
propertyRepository.getProperties().add(nameProperty);
|
||||
|
||||
session.persist( propertyRepository );
|
||||
session.persist(propertyRepository);
|
||||
//end::associations-many-to-any-persist-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
doInHibernate(this::sessionFactory, session -> {
|
||||
//tag::associations-many-to-any-query-example[]
|
||||
PropertyRepository propertyRepository = session.get( PropertyRepository.class, 1L );
|
||||
PropertyRepository propertyRepository = session.get(PropertyRepository.class, 1L);
|
||||
|
||||
assertEquals(2, propertyRepository.getProperties().size());
|
||||
|
||||
for(Property property : propertyRepository.getProperties()) {
|
||||
assertNotNull( property.getValue() );
|
||||
assertNotNull(property.getValue());
|
||||
}
|
||||
//end::associations-many-to-any-query-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -21,10 +21,10 @@ import jakarta.persistence.DiscriminatorType;
|
|||
@Target({ElementType.TYPE, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
@AnyDiscriminator( DiscriminatorType.STRING )
|
||||
@AnyKeyJavaClass( Long.class )
|
||||
@AnyDiscriminator(DiscriminatorType.STRING)
|
||||
@AnyKeyJavaClass(Long.class)
|
||||
|
||||
@AnyDiscriminatorValue( discriminator = "S", entity = StringProperty.class )
|
||||
@AnyDiscriminatorValue( discriminator = "I", entity = IntegerProperty.class )
|
||||
@AnyDiscriminatorValue(discriminator = "S", entity = StringProperty.class)
|
||||
@AnyDiscriminatorValue(discriminator = "I", entity = IntegerProperty.class)
|
||||
public @interface PropertyDiscriminationDef {
|
||||
}
|
||||
|
|
|
@ -20,19 +20,19 @@ import jakarta.persistence.Table;
|
|||
|
||||
//tag::associations-any-example[]
|
||||
@Entity
|
||||
@Table( name = "property_holder" )
|
||||
@Table(name = "property_holder")
|
||||
public class PropertyHolder {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@Any
|
||||
@AnyDiscriminator( DiscriminatorType.STRING )
|
||||
@AnyDiscriminatorValue( discriminator = "S", entity = StringProperty.class )
|
||||
@AnyDiscriminatorValue( discriminator = "I", entity = IntegerProperty.class )
|
||||
@AnyKeyJavaClass( Long.class )
|
||||
@Column( name = "property_type" )
|
||||
@JoinColumn( name = "property_id" )
|
||||
@AnyDiscriminator(DiscriminatorType.STRING)
|
||||
@AnyDiscriminatorValue(discriminator = "S", entity = StringProperty.class)
|
||||
@AnyDiscriminatorValue(discriminator = "I", entity = IntegerProperty.class)
|
||||
@AnyKeyJavaClass(Long.class)
|
||||
@Column(name = "property_type")
|
||||
@JoinColumn(name = "property_id")
|
||||
private Property property;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
|
|
@ -16,7 +16,7 @@ import jakarta.persistence.Table;
|
|||
|
||||
//tag::associations-any-def-example[]
|
||||
@Entity
|
||||
@Table( name = "property_holder" )
|
||||
@Table(name = "property_holder")
|
||||
public class PropertyHolder2 {
|
||||
|
||||
@Id
|
||||
|
@ -24,8 +24,8 @@ public class PropertyHolder2 {
|
|||
|
||||
@Any
|
||||
@PropertyDiscriminationDef
|
||||
@Column( name = "property_type" )
|
||||
@JoinColumn( name = "property_id" )
|
||||
@Column(name = "property_type")
|
||||
@JoinColumn(name = "property_id")
|
||||
private Property property;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
|
|
@ -24,24 +24,24 @@ import org.hibernate.annotations.ManyToAny;
|
|||
|
||||
//tag::associations-many-to-any-example[]
|
||||
@Entity
|
||||
@Table( name = "property_repository" )
|
||||
@Table(name = "property_repository")
|
||||
public class PropertyRepository {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@ManyToAny
|
||||
@AnyDiscriminator( DiscriminatorType.STRING )
|
||||
@Column( name = "property_type" )
|
||||
@AnyKeyJavaClass( Long.class )
|
||||
@AnyDiscriminatorValue( discriminator = "S", entity = StringProperty.class )
|
||||
@AnyDiscriminatorValue( discriminator = "I", entity = IntegerProperty.class )
|
||||
@Cascade( { org.hibernate.annotations.CascadeType.ALL })
|
||||
@AnyDiscriminator(DiscriminatorType.STRING)
|
||||
@Column(name = "property_type")
|
||||
@AnyKeyJavaClass(Long.class)
|
||||
@AnyDiscriminatorValue(discriminator = "S", entity = StringProperty.class)
|
||||
@AnyDiscriminatorValue(discriminator = "I", entity = IntegerProperty.class)
|
||||
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
|
||||
@JoinTable(name = "repository_properties",
|
||||
joinColumns = @JoinColumn(name = "repository_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "property_id")
|
||||
)
|
||||
private List<Property<?>> properties = new ArrayList<>( );
|
||||
)
|
||||
private List<Property<?>> properties = new ArrayList<>();
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
|
|
|
@ -59,84 +59,84 @@ public class BatchTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testBulk() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist( new Person( "Vlad" ) );
|
||||
entityManager.persist( new Person( "Mihalcea" ) );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist(new Person("Vlad"));
|
||||
entityManager.persist(new Person("Mihalcea"));
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String oldName = "Vlad";
|
||||
String newName = "Alexandru";
|
||||
//tag::batch-session-jdbc-batch-size-example[]
|
||||
entityManager
|
||||
.unwrap( Session.class )
|
||||
.setJdbcBatchSize( 10 );
|
||||
.unwrap(Session.class)
|
||||
.setJdbcBatchSize(10);
|
||||
//end::batch-session-jdbc-batch-size-example[]
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String oldName = "Vlad";
|
||||
String newName = "Alexandru";
|
||||
//tag::batch-bulk-jpql-update-example[]
|
||||
int updatedEntities = entityManager.createQuery(
|
||||
"update Person p " +
|
||||
"set p.name = :newName " +
|
||||
"where p.name = :oldName" )
|
||||
.setParameter( "oldName", oldName )
|
||||
.setParameter( "newName", newName )
|
||||
"where p.name = :oldName")
|
||||
.setParameter("oldName", oldName)
|
||||
.setParameter("newName", newName)
|
||||
.executeUpdate();
|
||||
//end::batch-bulk-jpql-update-example[]
|
||||
assertEquals(1, updatedEntities);
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String oldName = "Alexandru";
|
||||
String newName = "Vlad";
|
||||
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::batch-bulk-hql-update-example[]
|
||||
int updatedEntities = session.createQuery(
|
||||
"update Person " +
|
||||
"set name = :newName " +
|
||||
"where name = :oldName" )
|
||||
.setParameter( "oldName", oldName )
|
||||
.setParameter( "newName", newName )
|
||||
"where name = :oldName")
|
||||
.setParameter("oldName", oldName)
|
||||
.setParameter("newName", newName)
|
||||
.executeUpdate();
|
||||
//end::batch-bulk-hql-update-example[]
|
||||
assertEquals(1, updatedEntities);
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String oldName = "Vlad";
|
||||
String newName = "Alexandru";
|
||||
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::batch-bulk-hql-update-version-example[]
|
||||
int updatedEntities = session.createQuery(
|
||||
"update versioned Person " +
|
||||
"set name = :newName " +
|
||||
"where name = :oldName" )
|
||||
.setParameter( "oldName", oldName )
|
||||
.setParameter( "newName", newName )
|
||||
"where name = :oldName")
|
||||
.setParameter("oldName", oldName)
|
||||
.setParameter("newName", newName)
|
||||
.executeUpdate();
|
||||
//end::batch-bulk-hql-update-version-example[]
|
||||
assertEquals(1, updatedEntities);
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String name = "Alexandru";
|
||||
|
||||
//tag::batch-bulk-jpql-delete-example[]
|
||||
int deletedEntities = entityManager.createQuery(
|
||||
"delete Person p " +
|
||||
"where p.name = :name" )
|
||||
.setParameter( "name", name )
|
||||
"where p.name = :name")
|
||||
.setParameter("name", name)
|
||||
.executeUpdate();
|
||||
//end::batch-bulk-jpql-delete-example[]
|
||||
assertEquals(1, deletedEntities);
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::batch-bulk-hql-insert-example[]
|
||||
int insertedEntities = session.createQuery(
|
||||
"insert into Partner (id, name) " +
|
||||
|
@ -145,21 +145,21 @@ public class BatchTest extends BaseEntityManagerFunctionalTestCase {
|
|||
.executeUpdate();
|
||||
//end::batch-bulk-hql-insert-example[]
|
||||
assertEquals(1, insertedEntities);
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String name = "Mihalcea";
|
||||
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::batch-bulk-hql-delete-example[]
|
||||
int deletedEntities = session.createQuery(
|
||||
"delete Person " +
|
||||
"where name = :name" )
|
||||
.setParameter( "name", name )
|
||||
"where name = :name")
|
||||
.setParameter("name", name)
|
||||
.executeUpdate();
|
||||
//end::batch-bulk-hql-delete-example[]
|
||||
assertEquals(1, deletedEntities);
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
private void withoutBatch() {
|
||||
|
@ -172,14 +172,14 @@ public class BatchTest extends BaseEntityManagerFunctionalTestCase {
|
|||
txn = entityManager.getTransaction();
|
||||
txn.begin();
|
||||
|
||||
for ( int i = 0; i < 100_000; i++ ) {
|
||||
Person Person = new Person( String.format( "Person %d", i ) );
|
||||
entityManager.persist( Person );
|
||||
for (int i = 0; i < 100_000; i++) {
|
||||
Person Person = new Person(String.format("Person %d", i));
|
||||
entityManager.persist(Person);
|
||||
}
|
||||
|
||||
txn.commit();
|
||||
} catch (RuntimeException e) {
|
||||
if ( txn != null && txn.isActive()) txn.rollback();
|
||||
if (txn != null && txn.isActive()) txn.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
if (entityManager != null) {
|
||||
|
@ -202,20 +202,20 @@ public class BatchTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
int batchSize = 25;
|
||||
|
||||
for ( int i = 0; i < entityCount; i++ ) {
|
||||
if ( i > 0 && i % batchSize == 0 ) {
|
||||
for (int i = 0; i < entityCount; i++) {
|
||||
if (i > 0 && i % batchSize == 0) {
|
||||
//flush a batch of inserts and release memory
|
||||
entityManager.flush();
|
||||
entityManager.clear();
|
||||
}
|
||||
|
||||
Person Person = new Person( String.format( "Person %d", i ) );
|
||||
entityManager.persist( Person );
|
||||
Person Person = new Person(String.format("Person %d", i));
|
||||
entityManager.persist(Person);
|
||||
}
|
||||
|
||||
txn.commit();
|
||||
} catch (RuntimeException e) {
|
||||
if ( txn != null && txn.isActive()) txn.rollback();
|
||||
if (txn != null && txn.isActive()) txn.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
if (entityManager != null) {
|
||||
|
@ -240,18 +240,18 @@ public class BatchTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
int batchSize = 25;
|
||||
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
|
||||
scrollableResults = session
|
||||
.createQuery( "select p from Person p" )
|
||||
.setCacheMode( CacheMode.IGNORE )
|
||||
.scroll( ScrollMode.FORWARD_ONLY );
|
||||
.createQuery("select p from Person p")
|
||||
.setCacheMode(CacheMode.IGNORE)
|
||||
.scroll(ScrollMode.FORWARD_ONLY);
|
||||
|
||||
int count = 0;
|
||||
while ( scrollableResults.next() ) {
|
||||
Person Person = (Person) scrollableResults.get( );
|
||||
while (scrollableResults.next()) {
|
||||
Person Person = (Person) scrollableResults.get();
|
||||
processPerson(Person);
|
||||
if ( ++count % batchSize == 0 ) {
|
||||
if (++count % batchSize == 0) {
|
||||
//flush a batch of updates and release memory:
|
||||
entityManager.flush();
|
||||
entityManager.clear();
|
||||
|
@ -260,7 +260,7 @@ public class BatchTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
txn.commit();
|
||||
} catch (RuntimeException e) {
|
||||
if ( txn != null && txn.isActive()) txn.rollback();
|
||||
if (txn != null && txn.isActive()) txn.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
if (scrollableResults != null) {
|
||||
|
@ -281,25 +281,25 @@ public class BatchTest extends BaseEntityManagerFunctionalTestCase {
|
|||
Transaction txn = null;
|
||||
ScrollableResults scrollableResults = null;
|
||||
try {
|
||||
SessionFactory sessionFactory = entityManagerFactory().unwrap( SessionFactory.class );
|
||||
SessionFactory sessionFactory = entityManagerFactory().unwrap(SessionFactory.class);
|
||||
statelessSession = sessionFactory.openStatelessSession();
|
||||
|
||||
txn = statelessSession.getTransaction();
|
||||
txn.begin();
|
||||
|
||||
scrollableResults = statelessSession
|
||||
.createQuery( "select p from Person p" )
|
||||
.createQuery("select p from Person p")
|
||||
.scroll(ScrollMode.FORWARD_ONLY);
|
||||
|
||||
while ( scrollableResults.next() ) {
|
||||
while (scrollableResults.next()) {
|
||||
Person Person = (Person) scrollableResults.get();
|
||||
processPerson(Person);
|
||||
statelessSession.update( Person );
|
||||
statelessSession.update(Person);
|
||||
}
|
||||
|
||||
txn.commit();
|
||||
} catch (RuntimeException e) {
|
||||
if ( txn != null && txn.getStatus() == TransactionStatus.ACTIVE) txn.rollback();
|
||||
if (txn != null && txn.getStatus() == TransactionStatus.ACTIVE) txn.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
if (scrollableResults != null) {
|
||||
|
@ -313,8 +313,8 @@ public class BatchTest extends BaseEntityManagerFunctionalTestCase {
|
|||
}
|
||||
|
||||
private void processPerson(Person Person) {
|
||||
if ( Person.getId() % 1000 == 0 ) {
|
||||
log.infof( "Processing [%s]", Person.getName());
|
||||
if (Person.getId() % 1000 == 0) {
|
||||
log.infof("Processing [%s]", Person.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -77,9 +77,9 @@ public class BootstrapTest {
|
|||
BootstrapServiceRegistryBuilder bootstrapRegistryBuilder =
|
||||
new BootstrapServiceRegistryBuilder();
|
||||
// add a custom ClassLoader
|
||||
bootstrapRegistryBuilder.applyClassLoader( customClassLoader );
|
||||
bootstrapRegistryBuilder.applyClassLoader(customClassLoader);
|
||||
// manually add an Integrator
|
||||
bootstrapRegistryBuilder.applyIntegrator( customIntegrator );
|
||||
bootstrapRegistryBuilder.applyIntegrator(customIntegrator);
|
||||
|
||||
BootstrapServiceRegistry bootstrapRegistry = bootstrapRegistryBuilder.build();
|
||||
//end::bootstrap-bootstrap-native-registry-BootstrapServiceRegistry-example[]
|
||||
|
@ -103,7 +103,7 @@ public class BootstrapTest {
|
|||
new BootstrapServiceRegistryBuilder().build();
|
||||
|
||||
StandardServiceRegistryBuilder standardRegistryBuilder =
|
||||
new StandardServiceRegistryBuilder( bootstrapRegistry );
|
||||
new StandardServiceRegistryBuilder(bootstrapRegistry);
|
||||
//end::bootstrap-bootstrap-native-registry-StandardServiceRegistryBuilder-example[]
|
||||
}
|
||||
|
||||
|
@ -115,7 +115,7 @@ public class BootstrapTest {
|
|||
ServiceRegistry standardRegistry =
|
||||
new StandardServiceRegistryBuilder().build();
|
||||
|
||||
MetadataSources sources = new MetadataSources( standardRegistry );
|
||||
MetadataSources sources = new MetadataSources(standardRegistry);
|
||||
|
||||
// alternatively, we can build the MetadataSources without passing
|
||||
// a service registry, in which case it will build a default
|
||||
|
@ -124,41 +124,41 @@ public class BootstrapTest {
|
|||
// MetadataSources sources = new MetadataSources();
|
||||
|
||||
// add a class using JPA/Hibernate annotations for mapping
|
||||
sources.addAnnotatedClass( MyEntity.class );
|
||||
sources.addAnnotatedClass(MyEntity.class);
|
||||
|
||||
// add the name of a class using JPA/Hibernate annotations for mapping.
|
||||
// differs from above in that accessing the Class is deferred which is
|
||||
// important if using runtime bytecode-enhancement
|
||||
sources.addAnnotatedClassName( "org.hibernate.example.Customer" );
|
||||
sources.addAnnotatedClassName("org.hibernate.example.Customer");
|
||||
|
||||
// Read package-level metadata.
|
||||
sources.addPackage( "hibernate.example" );
|
||||
sources.addPackage("hibernate.example");
|
||||
|
||||
// Read package-level metadata.
|
||||
sources.addPackage( MyEntity.class.getPackage() );
|
||||
sources.addPackage(MyEntity.class.getPackage());
|
||||
|
||||
// Adds the named hbm.xml resource as a source: which performs the
|
||||
// classpath lookup and parses the XML
|
||||
sources.addResource( "org/hibernate/example/Order.hbm.xml" );
|
||||
sources.addResource("org/hibernate/example/Order.hbm.xml");
|
||||
|
||||
// Adds the named JPA orm.xml resource as a source: which performs the
|
||||
// classpath lookup and parses the XML
|
||||
sources.addResource( "org/hibernate/example/Product.orm.xml" );
|
||||
sources.addResource("org/hibernate/example/Product.orm.xml");
|
||||
|
||||
// Read all mapping documents from a directory tree.
|
||||
// Assumes that any file named *.hbm.xml is a mapping document.
|
||||
sources.addDirectory( new File( ".") );
|
||||
sources.addDirectory(new File("."));
|
||||
|
||||
// Read mappings from a particular XML file
|
||||
sources.addFile( new File( "./mapping.xml") );
|
||||
sources.addFile(new File("./mapping.xml"));
|
||||
|
||||
// Read all mappings from a jar file.
|
||||
// Assumes that any file named *.hbm.xml is a mapping document.
|
||||
sources.addJar( new File( "./entities.jar") );
|
||||
sources.addJar(new File("./entities.jar"));
|
||||
|
||||
// Read a mapping as an application resource using the convention that a class named foo.bar.MyEntity is
|
||||
// mapped by a file named foo/bar/MyEntity.hbm.xml which can be resolved as a classpath resource.
|
||||
sources.addClass( MyEntity.class );
|
||||
sources.addClass(MyEntity.class);
|
||||
//end::bootstrap-bootstrap-native-registry-MetadataSources-example[]
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
|
@ -175,11 +175,11 @@ public class BootstrapTest {
|
|||
ServiceRegistry standardRegistry =
|
||||
new StandardServiceRegistryBuilder().build();
|
||||
|
||||
MetadataSources sources = new MetadataSources( standardRegistry )
|
||||
.addAnnotatedClass( MyEntity.class )
|
||||
.addAnnotatedClassName( "org.hibernate.example.Customer" )
|
||||
.addResource( "org/hibernate/example/Order.hbm.xml" )
|
||||
.addResource( "org/hibernate/example/Product.orm.xml" );
|
||||
MetadataSources sources = new MetadataSources(standardRegistry)
|
||||
.addAnnotatedClass(MyEntity.class)
|
||||
.addAnnotatedClassName("org.hibernate.example.Customer")
|
||||
.addResource("org/hibernate/example/Order.hbm.xml")
|
||||
.addResource("org/hibernate/example/Product.orm.xml");
|
||||
//end::bootstrap-native-metadata-source-example[]
|
||||
}
|
||||
|
||||
|
@ -199,19 +199,19 @@ public class BootstrapTest {
|
|||
ServiceRegistry standardRegistry =
|
||||
new StandardServiceRegistryBuilder().build();
|
||||
|
||||
MetadataSources sources = new MetadataSources( standardRegistry );
|
||||
MetadataSources sources = new MetadataSources(standardRegistry);
|
||||
|
||||
MetadataBuilder metadataBuilder = sources.getMetadataBuilder();
|
||||
|
||||
// Use the JPA-compliant implicit naming strategy
|
||||
metadataBuilder.applyImplicitNamingStrategy(
|
||||
ImplicitNamingStrategyJpaCompliantImpl.INSTANCE );
|
||||
ImplicitNamingStrategyJpaCompliantImpl.INSTANCE);
|
||||
|
||||
// specify the schema name to use for tables, etc when none is explicitly specified
|
||||
metadataBuilder.applyImplicitSchemaName( "my_default_schema" );
|
||||
metadataBuilder.applyImplicitSchemaName("my_default_schema");
|
||||
|
||||
// specify a custom Attribute Converter
|
||||
metadataBuilder.applyAttributeConverter( myAttributeConverter );
|
||||
metadataBuilder.applyAttributeConverter(myAttributeConverter);
|
||||
|
||||
Metadata metadata = metadataBuilder.build();
|
||||
//end::bootstrap-native-metadata-builder-example[]
|
||||
|
@ -228,48 +228,48 @@ public class BootstrapTest {
|
|||
{
|
||||
//tag::bootstrap-native-SessionFactory-example[]
|
||||
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
|
||||
.configure( "org/hibernate/example/hibernate.cfg.xml" )
|
||||
.configure("org/hibernate/example/hibernate.cfg.xml")
|
||||
.build();
|
||||
|
||||
Metadata metadata = new MetadataSources( standardRegistry )
|
||||
.addAnnotatedClass( MyEntity.class )
|
||||
.addAnnotatedClassName( "org.hibernate.example.Customer" )
|
||||
.addResource( "org/hibernate/example/Order.hbm.xml" )
|
||||
.addResource( "org/hibernate/example/Product.orm.xml" )
|
||||
Metadata metadata = new MetadataSources(standardRegistry)
|
||||
.addAnnotatedClass(MyEntity.class)
|
||||
.addAnnotatedClassName("org.hibernate.example.Customer")
|
||||
.addResource("org/hibernate/example/Order.hbm.xml")
|
||||
.addResource("org/hibernate/example/Product.orm.xml")
|
||||
.getMetadataBuilder()
|
||||
.applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE )
|
||||
.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
|
||||
.build();
|
||||
|
||||
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder()
|
||||
.applyBeanManager( getBeanManager() )
|
||||
.applyBeanManager(getBeanManager())
|
||||
.build();
|
||||
//end::bootstrap-native-SessionFactory-example[]
|
||||
}
|
||||
{
|
||||
//tag::bootstrap-native-SessionFactoryBuilder-example[]
|
||||
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
|
||||
.configure( "org/hibernate/example/hibernate.cfg.xml" )
|
||||
.configure("org/hibernate/example/hibernate.cfg.xml")
|
||||
.build();
|
||||
|
||||
Metadata metadata = new MetadataSources( standardRegistry )
|
||||
.addAnnotatedClass( MyEntity.class )
|
||||
.addAnnotatedClassName( "org.hibernate.example.Customer" )
|
||||
.addResource( "org/hibernate/example/Order.hbm.xml" )
|
||||
.addResource( "org/hibernate/example/Product.orm.xml" )
|
||||
Metadata metadata = new MetadataSources(standardRegistry)
|
||||
.addAnnotatedClass(MyEntity.class)
|
||||
.addAnnotatedClassName("org.hibernate.example.Customer")
|
||||
.addResource("org/hibernate/example/Order.hbm.xml")
|
||||
.addResource("org/hibernate/example/Product.orm.xml")
|
||||
.getMetadataBuilder()
|
||||
.applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE )
|
||||
.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
|
||||
.build();
|
||||
|
||||
SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
|
||||
|
||||
// Supply a SessionFactory-level Interceptor
|
||||
sessionFactoryBuilder.applyInterceptor( new CustomSessionFactoryInterceptor() );
|
||||
sessionFactoryBuilder.applyInterceptor(new CustomSessionFactoryInterceptor());
|
||||
|
||||
// Add a custom observer
|
||||
sessionFactoryBuilder.addSessionFactoryObservers( new CustomSessionFactoryObserver() );
|
||||
sessionFactoryBuilder.addSessionFactoryObservers(new CustomSessionFactoryObserver());
|
||||
|
||||
// Apply a CDI BeanManager ( for JPA event listeners )
|
||||
sessionFactoryBuilder.applyBeanManager( getBeanManager() );
|
||||
// Apply a CDI BeanManager (for JPA event listeners)
|
||||
sessionFactoryBuilder.applyBeanManager(getBeanManager());
|
||||
|
||||
SessionFactory sessionFactory = sessionFactoryBuilder.build();
|
||||
//end::bootstrap-native-SessionFactoryBuilder-example[]
|
||||
|
@ -285,7 +285,7 @@ public class BootstrapTest {
|
|||
try {
|
||||
//tag::bootstrap-jpa-compliant-EntityManagerFactory-example[]
|
||||
// Create an EMF for our CRM persistence-unit.
|
||||
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "CRM" );
|
||||
EntityManagerFactory emf = Persistence.createEntityManagerFactory("CRM");
|
||||
//end::bootstrap-jpa-compliant-EntityManagerFactory-example[]
|
||||
} catch (Exception ignore) {}
|
||||
}
|
||||
|
@ -296,26 +296,26 @@ public class BootstrapTest {
|
|||
try {
|
||||
//tag::bootstrap-native-EntityManagerFactory-example[]
|
||||
String persistenceUnitName = "CRM";
|
||||
List<String> entityClassNames = new ArrayList<>( );
|
||||
Properties properties = new Properties( );
|
||||
List<String> entityClassNames = new ArrayList<>();
|
||||
Properties properties = new Properties();
|
||||
|
||||
PersistenceUnitInfoImpl persistenceUnitInfo = new PersistenceUnitInfoImpl(
|
||||
persistenceUnitName,
|
||||
entityClassNames,
|
||||
properties
|
||||
);
|
||||
);
|
||||
|
||||
Map<String, Object> integrationSettings = new HashMap<>();
|
||||
integrationSettings.put(
|
||||
AvailableSettings.INTERCEPTOR,
|
||||
new CustomSessionFactoryInterceptor()
|
||||
);
|
||||
);
|
||||
|
||||
EntityManagerFactoryBuilderImpl entityManagerFactoryBuilder =
|
||||
new EntityManagerFactoryBuilderImpl(
|
||||
new PersistenceUnitInfoDescriptor( persistenceUnitInfo ),
|
||||
new PersistenceUnitInfoDescriptor(persistenceUnitInfo),
|
||||
integrationSettings
|
||||
);
|
||||
);
|
||||
|
||||
EntityManagerFactory emf = entityManagerFactoryBuilder.build();
|
||||
//end::bootstrap-native-EntityManagerFactory-example[]
|
||||
|
@ -347,26 +347,26 @@ public class BootstrapTest {
|
|||
// listeners are registered
|
||||
// It is a service so we look it up using the service registry
|
||||
final EventListenerRegistry eventListenerRegistry =
|
||||
serviceRegistry.getService( EventListenerRegistry.class );
|
||||
serviceRegistry.getService(EventListenerRegistry.class);
|
||||
|
||||
// If you wish to have custom determination and handling of "duplicate" listeners,
|
||||
// you would have to add an implementation of the
|
||||
// org.hibernate.event.service.spi.DuplicationStrategy contract like this
|
||||
eventListenerRegistry.addDuplicationStrategy( new CustomDuplicationStrategy() );
|
||||
eventListenerRegistry.addDuplicationStrategy(new CustomDuplicationStrategy());
|
||||
|
||||
// EventListenerRegistry defines 3 ways to register listeners:
|
||||
|
||||
// 1) This form overrides any existing registrations with
|
||||
eventListenerRegistry.setListeners( EventType.AUTO_FLUSH,
|
||||
DefaultAutoFlushEventListener.class );
|
||||
eventListenerRegistry.setListeners(EventType.AUTO_FLUSH,
|
||||
DefaultAutoFlushEventListener.class);
|
||||
|
||||
// 2) This form adds the specified listener(s) to the beginning of the listener chain
|
||||
eventListenerRegistry.prependListeners( EventType.PERSIST,
|
||||
DefaultPersistEventListener.class );
|
||||
eventListenerRegistry.prependListeners(EventType.PERSIST,
|
||||
DefaultPersistEventListener.class);
|
||||
|
||||
// 3) This form adds the specified listener(s) to the end of the listener chain
|
||||
eventListenerRegistry.appendListeners( EventType.MERGE,
|
||||
DefaultMergeEventListener.class );
|
||||
eventListenerRegistry.appendListeners(EventType.MERGE,
|
||||
DefaultMergeEventListener.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -414,7 +414,7 @@ public class BootstrapTest {
|
|||
//tag::bootstrap-jpa-compliant-PersistenceUnit-configurable-example[]
|
||||
@PersistenceUnit(
|
||||
unitName = "CRM"
|
||||
)
|
||||
)
|
||||
private EntityManagerFactory entityManagerFactory;
|
||||
//end::bootstrap-jpa-compliant-PersistenceUnit-configurable-example[]
|
||||
|
||||
|
@ -430,9 +430,9 @@ public class BootstrapTest {
|
|||
@PersistenceProperty(
|
||||
name="org.hibernate.flushMode",
|
||||
value= "MANUAL"
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
)
|
||||
private EntityManager entityManager;
|
||||
//end::bootstrap-jpa-compliant-PersistenceContext-configurable-example[]
|
||||
|
||||
|
@ -568,11 +568,11 @@ public class BootstrapTest {
|
|||
ServiceRegistry standardRegistry =
|
||||
new StandardServiceRegistryBuilder().build();
|
||||
|
||||
MetadataSources sources = new MetadataSources( standardRegistry );
|
||||
MetadataSources sources = new MetadataSources(standardRegistry);
|
||||
|
||||
MetadataBuilder metadataBuilder = sources.getMetadataBuilder();
|
||||
|
||||
metadataBuilder.applyBasicType( BitSetUserType.INSTANCE, "bitset" );
|
||||
metadataBuilder.applyBasicType(BitSetUserType.INSTANCE, "bitset");
|
||||
//end::basic-custom-type-register-UserType-example[]
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
|
|
|
@ -35,7 +35,7 @@ public class FirstLevelCacheTest extends BaseEntityManagerFunctionalTestCase {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings( "unchecked" )
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void addConfigOptions(Map options) {
|
||||
options.put(AvailableSettings.USE_SECOND_LEVEL_CACHE, Boolean.TRUE.toString());
|
||||
options.put(AvailableSettings.CACHE_REGION_FACTORY, "jcache");
|
||||
|
@ -43,20 +43,20 @@ public class FirstLevelCacheTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testCache() {
|
||||
Person aPerson = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist( new Person() );
|
||||
entityManager.persist( new Person() );
|
||||
Person aPerson = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist(new Person());
|
||||
entityManager.persist(new Person());
|
||||
Person person = new Person();
|
||||
entityManager.persist( person );
|
||||
entityManager.persist(person);
|
||||
return person;
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
List<Object> dtos = new ArrayList<>( );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
List<Object> dtos = new ArrayList<>();
|
||||
//tag::caching-management-jpa-detach-example[]
|
||||
for(Person person : entityManager.createQuery("select p from Person p", Person.class)
|
||||
.getResultList()) {
|
||||
dtos.add(toDTO(person));
|
||||
entityManager.detach( person );
|
||||
entityManager.detach(person);
|
||||
}
|
||||
//end::caching-management-jpa-detach-example[]
|
||||
//tag::caching-management-clear-example[]
|
||||
|
@ -67,17 +67,17 @@ public class FirstLevelCacheTest extends BaseEntityManagerFunctionalTestCase {
|
|||
Person person = aPerson;
|
||||
|
||||
//tag::caching-management-contains-example[]
|
||||
entityManager.contains( person );
|
||||
entityManager.contains(person);
|
||||
|
||||
//end::caching-management-contains-example[]
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
List<Object> dtos = new ArrayList<>( );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
List<Object> dtos = new ArrayList<>();
|
||||
//tag::caching-management-native-evict-example[]
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
for(Person person : (List<Person>) session.createQuery("select p from Person p").list()) {
|
||||
dtos.add(toDTO(person));
|
||||
session.evict( person );
|
||||
session.evict(person);
|
||||
}
|
||||
//end::caching-management-native-evict-example[]
|
||||
//tag::caching-management-clear-example[]
|
||||
|
@ -87,7 +87,7 @@ public class FirstLevelCacheTest extends BaseEntityManagerFunctionalTestCase {
|
|||
Person person = aPerson;
|
||||
|
||||
//tag::caching-management-contains-example[]
|
||||
session.contains( person );
|
||||
session.contains(person);
|
||||
//end::caching-management-contains-example[]
|
||||
});
|
||||
}
|
||||
|
|
|
@ -64,36 +64,36 @@ public class NonStrictReadWriteCacheTest extends BaseEntityManagerFunctionalTest
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings( "unchecked" )
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void addConfigOptions(Map options) {
|
||||
options.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, Boolean.TRUE.toString() );
|
||||
options.put( AvailableSettings.CACHE_REGION_FACTORY, "jcache" );
|
||||
options.put(AvailableSettings.USE_SECOND_LEVEL_CACHE, Boolean.TRUE.toString());
|
||||
options.put(AvailableSettings.CACHE_REGION_FACTORY, "jcache");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCache() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person();
|
||||
entityManager.persist( person );
|
||||
Phone home = new Phone( "123-456-7890" );
|
||||
Phone office = new Phone( "098-765-4321" );
|
||||
person.addPhone( home );
|
||||
person.addPhone( office );
|
||||
entityManager.persist(person);
|
||||
Phone home = new Phone("123-456-7890");
|
||||
Phone office = new Phone("098-765-4321");
|
||||
person.addPhone(home);
|
||||
person.addPhone(office);
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
person.getPhones().size();
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Log collection from cache" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Log collection from cache");
|
||||
//tag::caching-collection-example[]
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
person.getPhones().size();
|
||||
//end::caching-collection-example[]
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Load from cache" );
|
||||
entityManager.find( Person.class, 1L ).getPhones().size();
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Load from cache");
|
||||
entityManager.find(Person.class, 1L).getPhones().size();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -112,7 +112,7 @@ public class NonStrictReadWriteCacheTest extends BaseEntityManagerFunctionalTest
|
|||
//tag::caching-collection-mapping-example[]
|
||||
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
|
||||
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
|
||||
private List<Phone> phones = new ArrayList<>( );
|
||||
private List<Phone> phones = new ArrayList<>();
|
||||
//end::caching-collection-mapping-example[]
|
||||
|
||||
@Version
|
||||
|
@ -141,8 +141,8 @@ public class NonStrictReadWriteCacheTest extends BaseEntityManagerFunctionalTest
|
|||
}
|
||||
|
||||
public void addPhone(Phone phone) {
|
||||
phones.add( phone );
|
||||
phone.setPerson( this );
|
||||
phones.add(phone);
|
||||
phone.setPerson(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ import static org.junit.Assert.assertNotNull;
|
|||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@Ignore
|
||||
//@FailureExpected( jiraKey = "HHH-12146", message = "No idea why those changes cause this to fail, especially in the way it does" )
|
||||
//@FailureExpected(jiraKey = "HHH-12146", message = "No idea why those changes cause this to fail, especially in the way it does")
|
||||
public class SecondLevelCacheTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
|
@ -50,202 +50,202 @@ public class SecondLevelCacheTest extends BaseEntityManagerFunctionalTestCase {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings( "unchecked" )
|
||||
@SuppressWarnings("unchecked")
|
||||
protected void addConfigOptions(Map options) {
|
||||
options.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, Boolean.TRUE.toString() );
|
||||
options.put( AvailableSettings.CACHE_REGION_FACTORY, "jcache" );
|
||||
options.put( AvailableSettings.USE_QUERY_CACHE, Boolean.TRUE.toString() );
|
||||
options.put( AvailableSettings.GENERATE_STATISTICS, Boolean.TRUE.toString() );
|
||||
options.put(AvailableSettings.USE_SECOND_LEVEL_CACHE, Boolean.TRUE.toString());
|
||||
options.put(AvailableSettings.CACHE_REGION_FACTORY, "jcache");
|
||||
options.put(AvailableSettings.USE_QUERY_CACHE, Boolean.TRUE.toString());
|
||||
options.put(AvailableSettings.GENERATE_STATISTICS, Boolean.TRUE.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCache() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist( new Person() );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist(new Person());
|
||||
Person aPerson= new Person();
|
||||
aPerson.setName( "John Doe" );
|
||||
aPerson.setCode( "unique-code" );
|
||||
entityManager.persist( aPerson );
|
||||
aPerson.setName("John Doe");
|
||||
aPerson.setCode("unique-code");
|
||||
entityManager.persist(aPerson);
|
||||
return aPerson;
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Jpa load by id" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Jpa load by id");
|
||||
//tag::caching-entity-jpa-example[]
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
//end::caching-entity-jpa-example[]
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Native load by id" );
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Native load by id");
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::caching-entity-native-example[]
|
||||
Person person = session.get( Person.class, 1L );
|
||||
Person person = session.get(Person.class, 1L);
|
||||
//end::caching-entity-native-example[]
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Native load by natural-id" );
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Native load by natural-id");
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::caching-entity-natural-id-example[]
|
||||
Person person = session
|
||||
.byNaturalId( Person.class )
|
||||
.using( "code", "unique-code")
|
||||
.byNaturalId(Person.class)
|
||||
.using("code", "unique-code")
|
||||
.load();
|
||||
//end::caching-entity-natural-id-example[]
|
||||
assertNotNull(person);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Jpa query cache" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Jpa query cache");
|
||||
//tag::caching-query-jpa-example[]
|
||||
List<Person> persons = entityManager.createQuery(
|
||||
"select p " +
|
||||
"from Person p " +
|
||||
"where p.name = :name", Person.class)
|
||||
.setParameter( "name", "John Doe")
|
||||
.setHint( "org.hibernate.cacheable", "true")
|
||||
.setParameter("name", "John Doe")
|
||||
.setHint("org.hibernate.cacheable", "true")
|
||||
.getResultList();
|
||||
//end::caching-query-jpa-example[]
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Native query cache" );
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Native query cache");
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::caching-query-native-example[]
|
||||
List<Person> persons = session.createQuery(
|
||||
"select p " +
|
||||
"from Person p " +
|
||||
"where p.name = :name")
|
||||
.setParameter( "name", "John Doe")
|
||||
.setParameter("name", "John Doe")
|
||||
.setCacheable(true)
|
||||
.list();
|
||||
//end::caching-query-native-example[]
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Jpa query cache region" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Jpa query cache region");
|
||||
//tag::caching-query-region-jpa-example[]
|
||||
List<Person> persons = entityManager.createQuery(
|
||||
"select p " +
|
||||
"from Person p " +
|
||||
"where p.id > :id", Person.class)
|
||||
.setParameter( "id", 0L)
|
||||
.setHint( QueryHints.HINT_CACHEABLE, "true")
|
||||
.setHint( QueryHints.HINT_CACHE_REGION, "query.cache.person" )
|
||||
.setParameter("id", 0L)
|
||||
.setHint(QueryHints.HINT_CACHEABLE, "true")
|
||||
.setHint(QueryHints.HINT_CACHE_REGION, "query.cache.person")
|
||||
.getResultList();
|
||||
//end::caching-query-region-jpa-example[]
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Native query cache" );
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Native query cache");
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::caching-query-region-native-example[]
|
||||
List<Person> persons = session.createQuery(
|
||||
"select p " +
|
||||
"from Person p " +
|
||||
"where p.id > :id")
|
||||
.setParameter( "id", 0L)
|
||||
.setParameter("id", 0L)
|
||||
.setCacheable(true)
|
||||
.setCacheRegion( "query.cache.person" )
|
||||
.setCacheRegion("query.cache.person")
|
||||
.list();
|
||||
//end::caching-query-region-native-example[]
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Jpa query cache store mode " );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Jpa query cache store mode ");
|
||||
//tag::caching-query-region-store-mode-jpa-example[]
|
||||
List<Person> persons = entityManager.createQuery(
|
||||
"select p " +
|
||||
"from Person p " +
|
||||
"where p.id > :id", Person.class)
|
||||
.setParameter( "id", 0L)
|
||||
.setHint( QueryHints.HINT_CACHEABLE, "true")
|
||||
.setHint( QueryHints.HINT_CACHE_REGION, "query.cache.person" )
|
||||
.setHint( "jakarta.persistence.cache.storeMode", CacheStoreMode.REFRESH )
|
||||
.setParameter("id", 0L)
|
||||
.setHint(QueryHints.HINT_CACHEABLE, "true")
|
||||
.setHint(QueryHints.HINT_CACHE_REGION, "query.cache.person")
|
||||
.setHint("jakarta.persistence.cache.storeMode", CacheStoreMode.REFRESH)
|
||||
.getResultList();
|
||||
//end::caching-query-region-store-mode-jpa-example[]
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Native query cache store mode" );
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Native query cache store mode");
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::caching-query-region-store-mode-native-example[]
|
||||
List<Person> persons = session.createQuery(
|
||||
"select p " +
|
||||
"from Person p " +
|
||||
"where p.id > :id")
|
||||
.setParameter( "id", 0L)
|
||||
.setParameter("id", 0L)
|
||||
.setCacheable(true)
|
||||
.setCacheRegion( "query.cache.person" )
|
||||
.setCacheMode( CacheMode.REFRESH )
|
||||
.setCacheRegion("query.cache.person")
|
||||
.setCacheMode(CacheMode.REFRESH)
|
||||
.list();
|
||||
//end::caching-query-region-store-mode-native-example[]
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::caching-statistics-example[]
|
||||
Statistics statistics = session.getSessionFactory().getStatistics();
|
||||
CacheRegionStatistics secondLevelCacheStatistics =
|
||||
statistics.getDomainDataRegionStatistics( "query.cache.person" );
|
||||
statistics.getDomainDataRegionStatistics("query.cache.person");
|
||||
long hitCount = secondLevelCacheStatistics.getHitCount();
|
||||
long missCount = secondLevelCacheStatistics.getMissCount();
|
||||
double hitRatio = (double) hitCount / ( hitCount + missCount );
|
||||
double hitRatio = (double) hitCount / (hitCount + missCount);
|
||||
//end::caching-statistics-example[]
|
||||
return hitRatio;
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "Native query cache store mode" );
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("Native query cache store mode");
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::caching-query-region-native-evict-example[]
|
||||
session.getSessionFactory().getCache().evictQueryRegion( "query.cache.person" );
|
||||
session.getSessionFactory().getCache().evictQueryRegion("query.cache.person");
|
||||
//end::caching-query-region-native-evict-example[]
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::caching-management-cache-mode-entity-jpa-example[]
|
||||
Map<String, Object> hints = new HashMap<>( );
|
||||
hints.put( "jakarta.persistence.cache.retrieveMode" , CacheRetrieveMode.USE );
|
||||
hints.put( "jakarta.persistence.cache.storeMode" , CacheStoreMode.REFRESH );
|
||||
Person person = entityManager.find( Person.class, 1L , hints);
|
||||
Map<String, Object> hints = new HashMap<>();
|
||||
hints.put("jakarta.persistence.cache.retrieveMode" , CacheRetrieveMode.USE);
|
||||
hints.put("jakarta.persistence.cache.storeMode" , CacheStoreMode.REFRESH);
|
||||
Person person = entityManager.find(Person.class, 1L , hints);
|
||||
//end::caching-management-cache-mode-entity-jpa-example[]
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::caching-management-cache-mode-entity-native-example[]
|
||||
session.setCacheMode( CacheMode.REFRESH );
|
||||
Person person = session.get( Person.class, 1L );
|
||||
session.setCacheMode(CacheMode.REFRESH);
|
||||
Person person = session.get(Person.class, 1L);
|
||||
//end::caching-management-cache-mode-entity-native-example[]
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::caching-management-cache-mode-query-jpa-example[]
|
||||
List<Person> persons = entityManager.createQuery(
|
||||
"select p from Person p", Person.class)
|
||||
.setHint( QueryHints.HINT_CACHEABLE, "true")
|
||||
.setHint( "jakarta.persistence.cache.retrieveMode" , CacheRetrieveMode.USE )
|
||||
.setHint( "jakarta.persistence.cache.storeMode" , CacheStoreMode.REFRESH )
|
||||
.setHint(QueryHints.HINT_CACHEABLE, "true")
|
||||
.setHint("jakarta.persistence.cache.retrieveMode" , CacheRetrieveMode.USE)
|
||||
.setHint("jakarta.persistence.cache.storeMode" , CacheStoreMode.REFRESH)
|
||||
.getResultList();
|
||||
//end::caching-management-cache-mode-query-jpa-example[]
|
||||
|
||||
//tag::caching-management-evict-jpa-example[]
|
||||
entityManager.getEntityManagerFactory().getCache().evict( Person.class );
|
||||
entityManager.getEntityManagerFactory().getCache().evict(Person.class);
|
||||
//end::caching-management-evict-jpa-example[]
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
//tag::caching-management-cache-mode-query-native-example[]
|
||||
List<Person> persons = session.createQuery(
|
||||
"select p from Person p" )
|
||||
.setCacheable( true )
|
||||
.setCacheMode( CacheMode.REFRESH )
|
||||
"select p from Person p")
|
||||
.setCacheable(true)
|
||||
.setCacheMode(CacheMode.REFRESH)
|
||||
.list();
|
||||
//end::caching-management-cache-mode-query-native-example[]
|
||||
|
||||
//tag::caching-management-evict-native-example[]
|
||||
session.getSessionFactory().getCache().evictQueryRegion( "query.cache.person" );
|
||||
session.getSessionFactory().getCache().evictQueryRegion("query.cache.person");
|
||||
//end::caching-management-evict-native-example[]
|
||||
});
|
||||
}
|
||||
|
|
|
@ -29,20 +29,20 @@ public class ArrayTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
String[] phones = new String[2];
|
||||
phones[0] = "028-234-9876";
|
||||
phones[1] = "072-122-9876";
|
||||
person.setPhones( phones );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
person.setPhones(phones);
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
String[] phones = new String[1];
|
||||
phones[0] = "072-122-9876";
|
||||
person.setPhones( phones );
|
||||
} );
|
||||
person.setPhones(phones);
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-array-binary-example[]
|
||||
|
|
|
@ -36,18 +36,18 @@ public class BasicTypeCollectionTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
doInHibernate(this::sessionFactory, session -> {
|
||||
Person person = new Person();
|
||||
person.id = 1L;
|
||||
session.persist( person );
|
||||
session.persist(person);
|
||||
|
||||
//tag::collections-comma-delimited-collection-lifecycle-example[]
|
||||
person.phones.add( "027-123-4567" );
|
||||
person.phones.add( "028-234-9876" );
|
||||
person.phones.add("027-123-4567");
|
||||
person.phones.add("028-234-9876");
|
||||
session.flush();
|
||||
person.getPhones().remove( 0 );
|
||||
person.getPhones().remove(0);
|
||||
//end::collections-comma-delimited-collection-lifecycle-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-comma-delimited-collection-example[]
|
||||
|
@ -57,8 +57,8 @@ public class BasicTypeCollectionTest extends BaseCoreFunctionalTestCase {
|
|||
@Id
|
||||
private Long id;
|
||||
|
||||
@JavaType( CommaDelimitedStringsJavaTypeDescriptor.class )
|
||||
@JdbcTypeCode( Types.VARCHAR )
|
||||
@JavaType(CommaDelimitedStringsJavaTypeDescriptor.class)
|
||||
@JdbcTypeCode(Types.VARCHAR)
|
||||
private List<String> phones = new ArrayList<>();
|
||||
|
||||
public List<String> getPhones() {
|
||||
|
|
|
@ -33,49 +33,49 @@ public class BasicTypeElementCollectionTest extends BaseEntityManagerFunctionalT
|
|||
@Override
|
||||
public void buildEntityManagerFactory() {
|
||||
super.buildEntityManagerFactory();
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person();
|
||||
person.id = 1L;
|
||||
person.phones.add( "027-123-4567" );
|
||||
person.phones.add( "028-234-9876" );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
person.phones.add("027-123-4567");
|
||||
person.phones.add("028-234-9876");
|
||||
entityManager.persist(person);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProxies() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
try {
|
||||
//tag::collections-collection-proxy-usage-example[]
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
//Throws java.lang.ClassCastException: org.hibernate.collection.internal.PersistentBag cannot be cast to java.util.ArrayList
|
||||
ArrayList<String> phones = (ArrayList<String>) person.getPhones();
|
||||
//end::collections-collection-proxy-usage-example[]
|
||||
}
|
||||
catch (Exception expected) {
|
||||
log.error( "Failure", expected );
|
||||
log.error("Failure", expected);
|
||||
}
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
log.info( "Clear element collection and add element" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
log.info("Clear element collection and add element");
|
||||
//tag::collections-value-type-collection-lifecycle-example[]
|
||||
person.getPhones().clear();
|
||||
person.getPhones().add( "123-456-7890" );
|
||||
person.getPhones().add( "456-000-1234" );
|
||||
person.getPhones().add("123-456-7890");
|
||||
person.getPhones().add("456-000-1234");
|
||||
//end::collections-value-type-collection-lifecycle-example[]
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
log.info( "Remove one element" );
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
log.info("Remove one element");
|
||||
//tag::collections-value-type-collection-remove-example[]
|
||||
person.getPhones().remove( 0 );
|
||||
person.getPhones().remove(0);
|
||||
//end::collections-value-type-collection-remove-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-collection-proxy-entity-example[]
|
||||
|
|
|
@ -33,20 +33,20 @@ public class BasicTypeOrderColumnElementCollectionTest extends BaseEntityManager
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person();
|
||||
person.id = 1L;
|
||||
person.getPhones().add( "123-456-7890" );
|
||||
person.getPhones().add( "456-000-1234" );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
log.info( "Remove one element" );
|
||||
person.getPhones().add("123-456-7890");
|
||||
person.getPhones().add("456-000-1234");
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
log.info("Remove one element");
|
||||
//tag::collections-value-type-collection-order-column-remove-example[]
|
||||
person.getPhones().remove( 0 );
|
||||
person.getPhones().remove(0);
|
||||
//end::collections-value-type-collection-order-column-remove-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
|
|
@ -38,14 +38,14 @@ public class BidirectionalBagOrphanRemovalTest extends BaseEntityManagerFunction
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
entityManager.persist( person );
|
||||
person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
entityManager.persist(person);
|
||||
person.addPhone(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.addPhone(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.flush();
|
||||
person.removePhone( person.getPhones().get( 0 ) );
|
||||
} );
|
||||
person.removePhone(person.getPhones().get(0));
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
@ -71,13 +71,13 @@ public class BidirectionalBagOrphanRemovalTest extends BaseEntityManagerFunction
|
|||
}
|
||||
|
||||
public void addPhone(Phone phone) {
|
||||
phones.add( phone );
|
||||
phone.setPerson( this );
|
||||
phones.add(phone);
|
||||
phone.setPerson(this);
|
||||
}
|
||||
|
||||
public void removePhone(Phone phone) {
|
||||
phones.remove( phone );
|
||||
phone.setPerson( null );
|
||||
phones.remove(phone);
|
||||
phone.setPerson(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -127,19 +127,19 @@ public class BidirectionalBagOrphanRemovalTest extends BaseEntityManagerFunction
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,16 +38,16 @@ public class BidirectionalBagTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
entityManager.persist( person );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
entityManager.persist(person);
|
||||
//tag::collections-bidirectional-bag-lifecycle-example[]
|
||||
person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
person.addPhone(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.addPhone(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.flush();
|
||||
person.removePhone( person.getPhones().get( 0 ) );
|
||||
person.removePhone(person.getPhones().get(0));
|
||||
//end::collections-bidirectional-bag-lifecycle-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-bidirectional-bag-example[]
|
||||
|
@ -77,13 +77,13 @@ public class BidirectionalBagTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
//tag::collections-bidirectional-bag-example[]
|
||||
public void addPhone(Phone phone) {
|
||||
phones.add( phone );
|
||||
phone.setPerson( this );
|
||||
phones.add(phone);
|
||||
phone.setPerson(this);
|
||||
}
|
||||
|
||||
public void removePhone(Phone phone) {
|
||||
phones.remove( phone );
|
||||
phone.setPerson( null );
|
||||
phones.remove(phone);
|
||||
phone.setPerson(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,19 +138,19 @@ public class BidirectionalBagTest extends BaseEntityManagerFunctionalTestCase {
|
|||
//tag::collections-bidirectional-bag-example[]
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
//end::collections-bidirectional-bag-example[]
|
||||
|
|
|
@ -42,25 +42,25 @@ public class BidirectionalComparatorSortedSetTest extends BaseEntityManagerFunct
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
entityManager.persist( person );
|
||||
person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
entityManager.persist(person);
|
||||
person.addPhone(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.addPhone(new Phone(2L, "mobile", "072-122-9876"));
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 2, phones.size() );
|
||||
phones.stream().forEach( phone -> log.infov( "Phone number %s", phone.getNumber() ) );
|
||||
person.removePhone( phones.iterator().next() );
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Assert.assertEquals(2, phones.size());
|
||||
phones.stream().forEach(phone -> log.infov("Phone number %s", phone.getNumber()));
|
||||
person.removePhone(phones.iterator().next());
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
@ -84,20 +84,20 @@ public class BidirectionalComparatorSortedSetTest extends BaseEntityManagerFunct
|
|||
}
|
||||
|
||||
public void addPhone(Phone phone) {
|
||||
phones.add( phone );
|
||||
phone.setPerson( this );
|
||||
phones.add(phone);
|
||||
phone.setPerson(this);
|
||||
}
|
||||
|
||||
public void removePhone(Phone phone) {
|
||||
phones.remove( phone );
|
||||
phone.setPerson( null );
|
||||
phones.remove(phone);
|
||||
phone.setPerson(null);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ReverseComparator implements Comparator<Phone> {
|
||||
@Override
|
||||
public int compare(Phone o1, Phone o2) {
|
||||
return o2.compareTo( o1 );
|
||||
return o2.compareTo(o1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,24 +147,24 @@ public class BidirectionalComparatorSortedSetTest extends BaseEntityManagerFunct
|
|||
|
||||
@Override
|
||||
public int compareTo(Phone o) {
|
||||
return number.compareTo( o.getNumber() );
|
||||
return number.compareTo(o.getNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,18 +43,18 @@ public class BidirectionalMapTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
person.addPhone( new Phone( PhoneType.LAND_LINE, "028-234-9876", Timestamp.valueOf( now ) ) );
|
||||
person.addPhone( new Phone( PhoneType.MOBILE, "072-122-9876", Timestamp.valueOf( now.minusDays( 1 ) ) ) );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
person.addPhone(new Phone(PhoneType.LAND_LINE, "028-234-9876", Timestamp.valueOf(now)));
|
||||
person.addPhone(new Phone(PhoneType.MOBILE, "072-122-9876", Timestamp.valueOf(now.minusDays(1))));
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Map<PhoneType, Phone> phones = person.getPhoneRegister();
|
||||
Assert.assertEquals( 2, phones.size() );
|
||||
} );
|
||||
Assert.assertEquals(2, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
public enum PhoneType {
|
||||
|
@ -91,8 +91,8 @@ public class BidirectionalMapTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
//tag::collections-map-bidirectional-example[]
|
||||
public void addPhone(Phone phone) {
|
||||
phone.setPerson( this );
|
||||
phoneRegister.put( phone.getType(), phone );
|
||||
phone.setPerson(this);
|
||||
phoneRegister.put(phone.getType(), phone);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -39,17 +39,17 @@ public class BidirectionalOrderByListTest extends BaseEntityManagerFunctionalTes
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
entityManager.persist( person );
|
||||
person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
entityManager.persist(person);
|
||||
person.addPhone(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.addPhone(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.flush();
|
||||
person.removePhone( person.getPhones().get( 0 ) );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.find( Person.class, 1L ).getPhones().size();
|
||||
} );
|
||||
person.removePhone(person.getPhones().get(0));
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.find(Person.class, 1L).getPhones().size();
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
@ -76,13 +76,13 @@ public class BidirectionalOrderByListTest extends BaseEntityManagerFunctionalTes
|
|||
}
|
||||
|
||||
public void addPhone(Phone phone) {
|
||||
phones.add( phone );
|
||||
phone.setPerson( this );
|
||||
phones.add(phone);
|
||||
phone.setPerson(this);
|
||||
}
|
||||
|
||||
public void removePhone(Phone phone) {
|
||||
phones.remove( phone );
|
||||
phone.setPerson( null );
|
||||
phones.remove(phone);
|
||||
phone.setPerson(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,19 +132,19 @@ public class BidirectionalOrderByListTest extends BaseEntityManagerFunctionalTes
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,17 +39,17 @@ public class BidirectionalOrderColumnListTest extends BaseEntityManagerFunctiona
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
entityManager.persist( person );
|
||||
person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
entityManager.persist(person);
|
||||
person.addPhone(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.addPhone(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.flush();
|
||||
person.removePhone( person.getPhones().get( 0 ) );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.find( Person.class, 1L ).getPhones().size();
|
||||
} );
|
||||
person.removePhone(person.getPhones().get(0));
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.find(Person.class, 1L).getPhones().size();
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
@ -76,13 +76,13 @@ public class BidirectionalOrderColumnListTest extends BaseEntityManagerFunctiona
|
|||
}
|
||||
|
||||
public void addPhone(Phone phone) {
|
||||
phones.add( phone );
|
||||
phone.setPerson( this );
|
||||
phones.add(phone);
|
||||
phone.setPerson(this);
|
||||
}
|
||||
|
||||
public void removePhone(Phone phone) {
|
||||
phones.remove( phone );
|
||||
phone.setPerson( null );
|
||||
phones.remove(phone);
|
||||
phone.setPerson(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,19 +132,19 @@ public class BidirectionalOrderColumnListTest extends BaseEntityManagerFunctiona
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,24 +39,24 @@ public class BidirectionalSetTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
entityManager.persist( person );
|
||||
person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
entityManager.persist(person);
|
||||
person.addPhone(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.addPhone(new Phone(2L, "mobile", "072-122-9876"));
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 2, phones.size() );
|
||||
person.removePhone( phones.iterator().next() );
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Assert.assertEquals(2, phones.size());
|
||||
person.removePhone(phones.iterator().next());
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-bidirectional-set-example[]
|
||||
|
@ -86,13 +86,13 @@ public class BidirectionalSetTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
//tag::collections-bidirectional-set-example[]
|
||||
public void addPhone(Phone phone) {
|
||||
phones.add( phone );
|
||||
phone.setPerson( this );
|
||||
phones.add(phone);
|
||||
phone.setPerson(this);
|
||||
}
|
||||
|
||||
public void removePhone(Phone phone) {
|
||||
phones.remove( phone );
|
||||
phone.setPerson( null );
|
||||
phones.remove(phone);
|
||||
phone.setPerson(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,19 +147,19 @@ public class BidirectionalSetTest extends BaseEntityManagerFunctionalTestCase {
|
|||
//tag::collections-bidirectional-set-example[]
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
//end::collections-bidirectional-set-example[]
|
||||
|
|
|
@ -41,25 +41,25 @@ public class BidirectionalSortedSetTest extends BaseEntityManagerFunctionalTestC
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
entityManager.persist( person );
|
||||
person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
entityManager.persist(person);
|
||||
person.addPhone(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.addPhone(new Phone(2L, "mobile", "072-122-9876"));
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 2, phones.size() );
|
||||
phones.stream().forEach( phone -> log.infov( "Phone number %s", phone.getNumber() ) );
|
||||
person.removePhone( phones.iterator().next() );
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Assert.assertEquals(2, phones.size());
|
||||
phones.stream().forEach(phone -> log.infov("Phone number %s", phone.getNumber()));
|
||||
person.removePhone(phones.iterator().next());
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
@ -86,13 +86,13 @@ public class BidirectionalSortedSetTest extends BaseEntityManagerFunctionalTestC
|
|||
}
|
||||
|
||||
public void addPhone(Phone phone) {
|
||||
phones.add( phone );
|
||||
phone.setPerson( this );
|
||||
phones.add(phone);
|
||||
phone.setPerson(this);
|
||||
}
|
||||
|
||||
public void removePhone(Phone phone) {
|
||||
phones.remove( phone );
|
||||
phone.setPerson( null );
|
||||
phones.remove(phone);
|
||||
phone.setPerson(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -142,24 +142,24 @@ public class BidirectionalSortedSetTest extends BaseEntityManagerFunctionalTestC
|
|||
|
||||
@Override
|
||||
public int compareTo(Phone o) {
|
||||
return number.compareTo( o.getNumber() );
|
||||
return number.compareTo(o.getNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,23 +41,23 @@ public class ElementCollectionMapTest extends BaseEntityManagerFunctionalTestCas
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
//tag::collections-map-value-type-entity-key-add-example[]
|
||||
person.getPhoneRegister().put(
|
||||
new Phone( PhoneType.LAND_LINE, "028-234-9876" ), new Date()
|
||||
new Phone(PhoneType.LAND_LINE, "028-234-9876"), new Date()
|
||||
);
|
||||
person.getPhoneRegister().put(
|
||||
new Phone( PhoneType.MOBILE, "072-122-9876" ), new Date()
|
||||
new Phone(PhoneType.MOBILE, "072-122-9876"), new Date()
|
||||
);
|
||||
//end::collections-map-value-type-entity-key-add-example[]
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Map<Phone, Date> phones = person.getPhoneRegister();
|
||||
Assert.assertEquals( 2, phones.size() );
|
||||
} );
|
||||
Assert.assertEquals(2, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-map-value-type-entity-key-example[]
|
||||
|
|
|
@ -34,15 +34,15 @@ public class EmbeddableTypeElementCollectionTest extends BaseEntityManagerFuncti
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person();
|
||||
person.id = 1L;
|
||||
//tag::collections-embeddable-type-collection-lifecycle-example[]
|
||||
person.getPhones().add( new Phone( "landline", "028-234-9876" ) );
|
||||
person.getPhones().add( new Phone( "mobile", "072-122-9876" ) );
|
||||
person.getPhones().add(new Phone("landline", "028-234-9876"));
|
||||
person.getPhones().add(new Phone("mobile", "072-122-9876"));
|
||||
//end::collections-embeddable-type-collection-lifecycle-example[]
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
entityManager.persist(person);
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-embeddable-type-collection-lifecycle-entity-example[]
|
||||
|
|
|
@ -42,33 +42,33 @@ public class MapKeyClassTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void testLifecycle() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::collections-map-key-class-persist-example[]
|
||||
Person person = new Person();
|
||||
person.setId( 1L );
|
||||
person.getCallRegister().put( new MobilePhone( "01", "234", "567" ), 101 );
|
||||
person.getCallRegister().put( new MobilePhone( "01", "234", "789" ), 102 );
|
||||
person.setId(1L);
|
||||
person.getCallRegister().put(new MobilePhone("01", "234", "567"), 101);
|
||||
person.getCallRegister().put(new MobilePhone("01", "234", "789"), 102);
|
||||
|
||||
entityManager.persist( person );
|
||||
entityManager.persist(person);
|
||||
//end::collections-map-key-class-persist-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::collections-map-key-class-fetch-example[]
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
assertEquals( 2, person.getCallRegister().size() );
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
assertEquals(2, person.getCallRegister().size());
|
||||
|
||||
assertEquals(
|
||||
Integer.valueOf( 101 ),
|
||||
person.getCallRegister().get( MobilePhone.fromString( "01-234-567" ) )
|
||||
Integer.valueOf(101),
|
||||
person.getCallRegister().get(MobilePhone.fromString("01-234-567"))
|
||||
);
|
||||
|
||||
assertEquals(
|
||||
Integer.valueOf( 102 ),
|
||||
person.getCallRegister().get( MobilePhone.fromString( "01-234-789" ) )
|
||||
Integer.valueOf(102),
|
||||
person.getCallRegister().get(MobilePhone.fromString("01-234-789"))
|
||||
);
|
||||
//end::collections-map-key-class-fetch-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-map-key-class-mapping-example[]
|
||||
|
@ -84,8 +84,8 @@ public class MapKeyClassTest extends BaseEntityManagerFunctionalTestCase {
|
|||
name = "call_register",
|
||||
joinColumns = @JoinColumn(name = "person_id")
|
||||
)
|
||||
@MapKeyColumn( name = "call_timestamp_epoch" )
|
||||
@MapKeyClass( MobilePhone.class )
|
||||
@MapKeyColumn(name = "call_timestamp_epoch")
|
||||
@MapKeyClass(MobilePhone.class)
|
||||
@Column(name = "call_register")
|
||||
private Map<PhoneNumber, Integer> callRegister = new HashMap<>();
|
||||
|
||||
|
@ -114,9 +114,9 @@ public class MapKeyClassTest extends BaseEntityManagerFunctionalTestCase {
|
|||
implements PhoneNumber {
|
||||
|
||||
static PhoneNumber fromString(String phoneNumber) {
|
||||
String[] tokens = phoneNumber.split( "-" );
|
||||
if ( tokens.length != 3 ) {
|
||||
throw new IllegalArgumentException( "invalid phone number: " + phoneNumber );
|
||||
String[] tokens = phoneNumber.split("-");
|
||||
if (tokens.length != 3) {
|
||||
throw new IllegalArgumentException("invalid phone number: " + phoneNumber);
|
||||
}
|
||||
int i = 0;
|
||||
return new MobilePhone(
|
||||
|
@ -159,21 +159,21 @@ public class MapKeyClassTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
MobilePhone that = (MobilePhone) o;
|
||||
return Objects.equals( countryCode, that.countryCode ) &&
|
||||
Objects.equals( operatorCode, that.operatorCode ) &&
|
||||
Objects.equals( subscriberCode, that.subscriberCode );
|
||||
return Objects.equals(countryCode, that.countryCode) &&
|
||||
Objects.equals(operatorCode, that.operatorCode) &&
|
||||
Objects.equals(subscriberCode, that.subscriberCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( countryCode, operatorCode, subscriberCode );
|
||||
return Objects.hash(countryCode, operatorCode, subscriberCode);
|
||||
}
|
||||
}
|
||||
//end::collections-map-key-class-type-mapping-example[]
|
||||
|
|
|
@ -58,13 +58,13 @@ public class MapKeyTypeTest extends BaseEntityManagerFunctionalTestCase {
|
|||
2017, 5, 23, 18, 22, 19
|
||||
);
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
PersonDummy person = new PersonDummy();
|
||||
person.setId( 1L );
|
||||
person.getPhoneRegister().put( Timestamp.valueOf( firstCall ).getTime(), 101 );
|
||||
person.getPhoneRegister().put( Timestamp.valueOf( secondCall ).getTime(), 102 );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
person.setId(1L);
|
||||
person.getPhoneRegister().put(Timestamp.valueOf(firstCall).getTime(), 101);
|
||||
person.getPhoneRegister().put(Timestamp.valueOf(secondCall).getTime(), 102);
|
||||
entityManager.persist(person);
|
||||
});
|
||||
|
||||
EntityManagerFactory entityManagerFactory = null;
|
||||
try {
|
||||
|
@ -80,22 +80,22 @@ public class MapKeyTypeTest extends BaseEntityManagerFunctionalTestCase {
|
|||
"none"
|
||||
);
|
||||
entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(
|
||||
new TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ),
|
||||
new TestingPersistenceUnitDescriptorImpl(getClass().getSimpleName()),
|
||||
settings
|
||||
).build().unwrap( SessionFactoryImplementor.class );
|
||||
).build().unwrap(SessionFactoryImplementor.class);
|
||||
|
||||
final EntityManagerFactory emf = entityManagerFactory;
|
||||
|
||||
doInJPA( () -> emf, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
doInJPA(() -> emf, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
assertEquals(
|
||||
Integer.valueOf( 101 ),
|
||||
person.getCallRegister().get( Timestamp.valueOf( firstCall ) )
|
||||
Integer.valueOf(101),
|
||||
person.getCallRegister().get(Timestamp.valueOf(firstCall))
|
||||
);
|
||||
} );
|
||||
});
|
||||
}
|
||||
finally {
|
||||
if ( entityManagerFactory != null ) {
|
||||
if (entityManagerFactory != null) {
|
||||
entityManagerFactory.close();
|
||||
}
|
||||
}
|
||||
|
@ -113,7 +113,7 @@ public class MapKeyTypeTest extends BaseEntityManagerFunctionalTestCase {
|
|||
name = "call_register",
|
||||
joinColumns = @JoinColumn(name = "person_id")
|
||||
)
|
||||
@MapKeyColumn( name = "call_timestamp_epoch" )
|
||||
@MapKeyColumn(name = "call_timestamp_epoch")
|
||||
@Column(name = "phone_number")
|
||||
private Map<Long, Integer> callRegister = new HashMap<>();
|
||||
|
||||
|
@ -139,11 +139,11 @@ public class MapKeyTypeTest extends BaseEntityManagerFunctionalTestCase {
|
|||
name = "call_register",
|
||||
joinColumns = @JoinColumn(name = "person_id")
|
||||
)
|
||||
@MapKeyJdbcTypeCode( Types.BIGINT )
|
||||
@MapKeyJdbcTypeCode(Types.BIGINT)
|
||||
// todo (6.0) : figure out why `@MapKeyTemporal` did not work. imo it should
|
||||
// @MapKeyTemporal( TemporalType.TIMESTAMP )
|
||||
@MapKeyJavaType( JdbcTimestampJavaTypeDescriptor.class )
|
||||
@MapKeyColumn( name = "call_timestamp_epoch" )
|
||||
// @MapKeyTemporal(TemporalType.TIMESTAMP)
|
||||
@MapKeyJavaType(JdbcTimestampJavaTypeDescriptor.class)
|
||||
@MapKeyColumn(name = "call_timestamp_epoch")
|
||||
@Column(name = "phone_number")
|
||||
private Map<Date, Integer> callRegister = new HashMap<>();
|
||||
|
||||
|
|
|
@ -40,14 +40,14 @@ public class OrderColumnListIndexBaseTest extends BaseEntityManagerFunctionalTes
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::collections-customizing-ordered-list-ordinal-persist-example[]
|
||||
Person person = new Person( 1L );
|
||||
entityManager.persist( person );
|
||||
person.addPhone( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.addPhone( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
Person person = new Person(1L);
|
||||
entityManager.persist(person);
|
||||
person.addPhone(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.addPhone(new Phone(2L, "mobile", "072-122-9876"));
|
||||
//end::collections-customizing-ordered-list-ordinal-persist-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
@ -75,13 +75,13 @@ public class OrderColumnListIndexBaseTest extends BaseEntityManagerFunctionalTes
|
|||
}
|
||||
|
||||
public void addPhone(Phone phone) {
|
||||
phones.add( phone );
|
||||
phone.setPerson( this );
|
||||
phones.add(phone);
|
||||
phone.setPerson(this);
|
||||
}
|
||||
|
||||
public void removePhone(Phone phone) {
|
||||
phones.remove( phone );
|
||||
phone.setPerson( null );
|
||||
phones.remove(phone);
|
||||
phone.setPerson(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -131,19 +131,19 @@ public class OrderColumnListIndexBaseTest extends BaseEntityManagerFunctionalTes
|
|||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,10 +41,10 @@ public class OrderedBySQLTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person();
|
||||
person.setId( 1L );
|
||||
person.setName( "Vlad Mihalcea" );
|
||||
person.setId(1L);
|
||||
person.setName("Vlad Mihalcea");
|
||||
|
||||
person.addArticle(
|
||||
new Article(
|
||||
|
@ -58,17 +58,17 @@ public class OrderedBySQLTest extends BaseEntityManagerFunctionalTestCase {
|
|||
"Associations, Lazy fetching, Concurrency Control, Second-level Caching"
|
||||
)
|
||||
);
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::collections-customizing-ordered-by-sql-clause-fetching-example[]
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
assertEquals(
|
||||
"High-Performance Hibernate",
|
||||
person.getArticles().get( 0 ).getName()
|
||||
person.getArticles().get(0).getName()
|
||||
);
|
||||
//end::collections-customizing-ordered-by-sql-clause-fetching-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-customizing-ordered-by-sql-clause-mapping-example[]
|
||||
|
@ -113,8 +113,8 @@ public class OrderedBySQLTest extends BaseEntityManagerFunctionalTestCase {
|
|||
}
|
||||
|
||||
public void addArticle(Article article) {
|
||||
article.setPerson( this );
|
||||
articles.add( article );
|
||||
article.setPerson(this);
|
||||
articles.add(article);
|
||||
}
|
||||
//tag::collections-customizing-ordered-by-sql-clause-mapping-example[]
|
||||
}
|
||||
|
|
|
@ -41,25 +41,25 @@ public class QueueTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
person.getPhones().add( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.getPhones().add( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
person.getPhones().add(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.getPhones().add(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::collections-custom-collection-example[]
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Queue<Phone> phones = person.getPhones();
|
||||
Phone head = phones.peek();
|
||||
assertSame(head, phones.poll());
|
||||
assertEquals( 1, phones.size() );
|
||||
assertEquals(1, phones.size());
|
||||
//end::collections-custom-collection-example[]
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
person.getPhones().clear();
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-custom-collection-mapping-example[]
|
||||
|
@ -70,7 +70,7 @@ public class QueueTest extends BaseEntityManagerFunctionalTestCase {
|
|||
private Long id;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@CollectionType( type = "org.hibernate.userguide.collections.type.QueueType")
|
||||
@CollectionType(type = "org.hibernate.userguide.collections.type.QueueType")
|
||||
private Collection<Phone> phones = new LinkedList<>();
|
||||
|
||||
//Constructors are omitted for brevity
|
||||
|
@ -130,24 +130,24 @@ public class QueueTest extends BaseEntityManagerFunctionalTestCase {
|
|||
//tag::collections-custom-collection-mapping-example[]
|
||||
@Override
|
||||
public int compareTo(Phone o) {
|
||||
return number.compareTo( o.getNumber() );
|
||||
return number.compareTo(o.getNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
//end::collections-custom-collection-mapping-example[]
|
||||
|
|
|
@ -35,18 +35,18 @@ public class UnidirectionalBagTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::collections-unidirectional-bag-lifecycle-example[]
|
||||
Person person = new Person( 1L );
|
||||
person.getPhones().add( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.getPhones().add( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
entityManager.persist( person );
|
||||
Person person = new Person(1L);
|
||||
person.getPhones().add(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.getPhones().add(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.persist(person);
|
||||
//end::collections-unidirectional-bag-lifecycle-example[]
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
person.getPhones().remove( 0 );
|
||||
} );
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
person.getPhones().remove(0);
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-unidirectional-bag-example[]
|
||||
|
|
|
@ -41,25 +41,25 @@ public class UnidirectionalComparatorSortedSetTest extends BaseEntityManagerFunc
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
person.getPhones().add( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.getPhones().add( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
person.getPhones().add(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.getPhones().add(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 2, phones.size() );
|
||||
phones.stream().forEach( phone -> log.infov( "Phone number %s", phone.getNumber() ) );
|
||||
phones.remove( phones.iterator().next() );
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Assert.assertEquals(2, phones.size());
|
||||
phones.stream().forEach(phone -> log.infov("Phone number %s", phone.getNumber()));
|
||||
phones.remove(phones.iterator().next());
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-unidirectional-sorted-set-custom-comparator-example[]
|
||||
|
@ -94,7 +94,7 @@ public class UnidirectionalComparatorSortedSetTest extends BaseEntityManagerFunc
|
|||
|
||||
@Override
|
||||
public int compare(Phone o1, Phone o2) {
|
||||
return o2.compareTo( o1 );
|
||||
return o2.compareTo(o1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -138,24 +138,24 @@ public class UnidirectionalComparatorSortedSetTest extends BaseEntityManagerFunc
|
|||
//tag::collections-unidirectional-sorted-set-custom-comparator-example[]
|
||||
@Override
|
||||
public int compareTo(Phone o) {
|
||||
return number.compareTo( o.getNumber() );
|
||||
return number.compareTo(o.getNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
//end::collections-unidirectional-sorted-set-custom-comparator-example[]
|
||||
|
|
|
@ -45,22 +45,22 @@ public class UnidirectionalMapTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
person.addPhone(
|
||||
new Phone( PhoneType.LAND_LINE, "028-234-9876", Timestamp.valueOf( now ) )
|
||||
new Phone(PhoneType.LAND_LINE, "028-234-9876", Timestamp.valueOf(now))
|
||||
);
|
||||
person.addPhone(
|
||||
new Phone( PhoneType.MOBILE, "072-122-9876", Timestamp.valueOf( now.minusDays( 1 ) ) )
|
||||
new Phone(PhoneType.MOBILE, "072-122-9876", Timestamp.valueOf(now.minusDays(1)))
|
||||
);
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Map<Date, Phone> phones = person.getPhoneRegister();
|
||||
Assert.assertEquals( 2, phones.size() );
|
||||
} );
|
||||
Assert.assertEquals(2, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-map-unidirectional-example[]
|
||||
|
@ -101,7 +101,7 @@ public class UnidirectionalMapTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
//tag::collections-map-unidirectional-example[]
|
||||
public void addPhone(Phone phone) {
|
||||
phoneRegister.put( phone.getSince(), phone );
|
||||
phoneRegister.put(phone.getSince(), phone);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -36,16 +36,16 @@ public class UnidirectionalOrderColumnListTest extends BaseEntityManagerFunction
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
person.getPhones().add( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.getPhones().add( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
person.getPhones().remove( 0 );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
person.getPhones().add(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.getPhones().add(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
person.getPhones().remove(0);
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
|
|
|
@ -36,16 +36,16 @@ public class UnidirectionalOrderedByListTest extends BaseEntityManagerFunctional
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
person.getPhones().add( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.getPhones().add( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
person.getPhones().remove( 0 );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
person.getPhones().add(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.getPhones().add(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
person.getPhones().remove(0);
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-unidirectional-ordered-list-order-by-example[]
|
||||
|
|
|
@ -38,24 +38,24 @@ public class UnidirectionalSetTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
person.getPhones().add( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.getPhones().add( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
person.getPhones().add(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.getPhones().add(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 2, phones.size() );
|
||||
phones.remove( phones.iterator().next() );
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Assert.assertEquals(2, phones.size());
|
||||
phones.remove(phones.iterator().next());
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-unidirectional-set-example[]
|
||||
|
@ -124,19 +124,19 @@ public class UnidirectionalSetTest extends BaseEntityManagerFunctionalTestCase {
|
|||
//tag::collections-unidirectional-set-example[]
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
//end::collections-unidirectional-set-example[]
|
||||
|
|
|
@ -40,25 +40,25 @@ public class UnidirectionalSortedSetTest extends BaseEntityManagerFunctionalTest
|
|||
|
||||
@Test
|
||||
public void testLifecycle() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person( 1L );
|
||||
person.getPhones().add( new Phone( 1L, "landline", "028-234-9876" ) );
|
||||
person.getPhones().add( new Phone( 2L, "mobile", "072-122-9876" ) );
|
||||
entityManager.persist( person );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person(1L);
|
||||
person.getPhones().add(new Phone(1L, "landline", "028-234-9876"));
|
||||
person.getPhones().add(new Phone(2L, "mobile", "072-122-9876"));
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 2, phones.size() );
|
||||
phones.stream().forEach( phone -> log.infov( "Phone number %s", phone.getNumber() ) );
|
||||
phones.remove( phones.iterator().next() );
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find( Person.class, 1L );
|
||||
Assert.assertEquals(2, phones.size());
|
||||
phones.stream().forEach(phone -> log.infov("Phone number %s", phone.getNumber()));
|
||||
phones.remove(phones.iterator().next());
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
Set<Phone> phones = person.getPhones();
|
||||
Assert.assertEquals( 1, phones.size() );
|
||||
} );
|
||||
Assert.assertEquals(1, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
//tag::collections-unidirectional-sorted-set-natural-comparator-example[]
|
||||
|
@ -129,24 +129,24 @@ public class UnidirectionalSortedSetTest extends BaseEntityManagerFunctionalTest
|
|||
//tag::collections-unidirectional-sorted-set-natural-comparator-example[]
|
||||
@Override
|
||||
public int compareTo(Phone o) {
|
||||
return number.compareTo( o.getNumber() );
|
||||
return number.compareTo(o.getNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Phone phone = (Phone) o;
|
||||
return Objects.equals( number, phone.number );
|
||||
return Objects.equals(number, phone.number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( number );
|
||||
return Objects.hash(number);
|
||||
}
|
||||
}
|
||||
//end::collections-unidirectional-sorted-set-natural-comparator-example[]
|
||||
|
|
|
@ -29,32 +29,32 @@ public class CommaDelimitedStringsJavaTypeDescriptor extends AbstractClassJavaTy
|
|||
new MutableMutabilityPlan<List>() {
|
||||
@Override
|
||||
protected List deepCopyNotNull(List value) {
|
||||
return new ArrayList( value );
|
||||
return new ArrayList(value);
|
||||
}
|
||||
}
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(List value) {
|
||||
return ( (List<String>) value ).stream().collect( Collectors.joining( DELIMITER ) );
|
||||
return ((List<String>) value).stream().collect(Collectors.joining(DELIMITER));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List fromString(CharSequence string) {
|
||||
List<String> values = new ArrayList<>();
|
||||
Collections.addAll( values, string.toString().split( DELIMITER ) );
|
||||
Collections.addAll(values, string.toString().split(DELIMITER));
|
||||
return values;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> X unwrap(List value, Class<X> type, WrapperOptions options) {
|
||||
return (X) toString( value );
|
||||
return (X) toString(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> List wrap(X value, WrapperOptions options) {
|
||||
return fromString( (CharSequence) value );
|
||||
return fromString((CharSequence) value);
|
||||
}
|
||||
}
|
||||
//end::collections-comma-delimited-collection-example[]
|
||||
|
|
|
@ -17,11 +17,11 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
|||
public class PersistentQueue extends PersistentBag implements Queue {
|
||||
|
||||
public PersistentQueue(SharedSessionContractImplementor session) {
|
||||
super( session );
|
||||
super(session);
|
||||
}
|
||||
|
||||
public PersistentQueue(SharedSessionContractImplementor session, List list) {
|
||||
super( session, list );
|
||||
super(session, list);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -39,7 +39,7 @@ public class PersistentQueue extends PersistentBag implements Queue {
|
|||
int size = size();
|
||||
if(size > 0) {
|
||||
Object first = get(0);
|
||||
remove( 0 );
|
||||
remove(0);
|
||||
return first;
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
|
@ -52,7 +52,7 @@ public class PersistentQueue extends PersistentBag implements Queue {
|
|||
|
||||
@Override
|
||||
public Object peek() {
|
||||
return size() > 0 ? get( 0 ) : null;
|
||||
return size() > 0 ? get(0) : null;
|
||||
}
|
||||
}
|
||||
//end::collections-custom-collection-mapping-example[]
|
|
@ -25,30 +25,30 @@ public class QueueType implements UserCollectionType {
|
|||
public PersistentCollection instantiate(
|
||||
SharedSessionContractImplementor session,
|
||||
CollectionPersister persister) throws HibernateException {
|
||||
return new PersistentQueue( session );
|
||||
return new PersistentQueue(session);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PersistentCollection wrap(
|
||||
SharedSessionContractImplementor session,
|
||||
Object collection) {
|
||||
return new PersistentQueue( session, (List) collection );
|
||||
return new PersistentQueue(session, (List) collection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator getElementsIterator(Object collection) {
|
||||
return ( (Queue) collection ).iterator();
|
||||
return ((Queue) collection).iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object collection, Object entity) {
|
||||
return ( (Queue) collection ).contains( entity );
|
||||
return ((Queue) collection).contains(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object indexOf(Object collection, Object entity) {
|
||||
int i = ( (List) collection ).indexOf( entity );
|
||||
return ( i < 0 ) ? null : i;
|
||||
int i = ((List) collection).indexOf(entity);
|
||||
return (i < 0) ? null : i;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -62,7 +62,7 @@ public class QueueType implements UserCollectionType {
|
|||
throws HibernateException {
|
||||
Queue result = (Queue) target;
|
||||
result.clear();
|
||||
result.addAll( (Queue) original );
|
||||
result.addAll((Queue) original);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -63,83 +63,83 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Before
|
||||
public void init() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person person1 = new Person("John Doe" );
|
||||
person1.setNickName( "JD" );
|
||||
person1.setAddress( "Earth" );
|
||||
person1.setCreatedOn( LocalDateTime.of( 2000, 1, 1, 0, 0, 0 ) ) ;
|
||||
person1.getAddresses().put( AddressType.HOME, "Home address" );
|
||||
person1.getAddresses().put( AddressType.OFFICE, "Office address" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person1 = new Person("John Doe");
|
||||
person1.setNickName("JD");
|
||||
person1.setAddress("Earth");
|
||||
person1.setCreatedOn(LocalDateTime.of(2000, 1, 1, 0, 0, 0)) ;
|
||||
person1.getAddresses().put(AddressType.HOME, "Home address");
|
||||
person1.getAddresses().put(AddressType.OFFICE, "Office address");
|
||||
entityManager.persist(person1);
|
||||
|
||||
Person person2 = new Person("Mrs. John Doe" );
|
||||
person2.setAddress( "Earth" );
|
||||
person2.setCreatedOn( LocalDateTime.of( 2000, 1, 2, 12, 0, 0 ) ) ;
|
||||
Person person2 = new Person("Mrs. John Doe");
|
||||
person2.setAddress("Earth");
|
||||
person2.setCreatedOn(LocalDateTime.of(2000, 1, 2, 12, 0, 0)) ;
|
||||
entityManager.persist(person2);
|
||||
|
||||
Person person3 = new Person("Dr_ John Doe" );
|
||||
Person person3 = new Person("Dr_ John Doe");
|
||||
entityManager.persist(person3);
|
||||
|
||||
Phone phone1 = new Phone( "123-456-7890" );
|
||||
phone1.setId( 1L );
|
||||
phone1.setType( PhoneType.MOBILE );
|
||||
person1.addPhone( phone1 );
|
||||
phone1.getRepairTimestamps().add( LocalDateTime.of( 2005, 1, 1, 12, 0, 0 ) );
|
||||
phone1.getRepairTimestamps().add( LocalDateTime.of( 2006, 1, 1, 12, 0, 0 ) );
|
||||
Phone phone1 = new Phone("123-456-7890");
|
||||
phone1.setId(1L);
|
||||
phone1.setType(PhoneType.MOBILE);
|
||||
person1.addPhone(phone1);
|
||||
phone1.getRepairTimestamps().add(LocalDateTime.of(2005, 1, 1, 12, 0, 0));
|
||||
phone1.getRepairTimestamps().add(LocalDateTime.of(2006, 1, 1, 12, 0, 0));
|
||||
|
||||
Call call11 = new Call();
|
||||
call11.setDuration( 12 );
|
||||
call11.setTimestamp( LocalDateTime.of( 2000, 1, 1, 0, 0, 0 ) );
|
||||
call11.setDuration(12);
|
||||
call11.setTimestamp(LocalDateTime.of(2000, 1, 1, 0, 0, 0));
|
||||
|
||||
Call call12 = new Call();
|
||||
call12.setDuration( 33 );
|
||||
call12.setTimestamp( LocalDateTime.of( 2000, 1, 1, 1, 0, 0 ) );
|
||||
call12.setDuration(33);
|
||||
call12.setTimestamp(LocalDateTime.of(2000, 1, 1, 1, 0, 0));
|
||||
|
||||
phone1.addCall(call11);
|
||||
phone1.addCall(call12);
|
||||
|
||||
Phone phone2 = new Phone( "098_765-4321" );
|
||||
phone2.setId( 2L );
|
||||
phone2.setType( PhoneType.LAND_LINE );
|
||||
Phone phone2 = new Phone("098_765-4321");
|
||||
phone2.setId(2L);
|
||||
phone2.setType(PhoneType.LAND_LINE);
|
||||
|
||||
Phone phone3 = new Phone( "098-765-4320" );
|
||||
phone3.setId( 3L );
|
||||
phone3.setType( PhoneType.LAND_LINE );
|
||||
Phone phone3 = new Phone("098-765-4320");
|
||||
phone3.setId(3L);
|
||||
phone3.setType(PhoneType.LAND_LINE);
|
||||
|
||||
person2.addPhone( phone2 );
|
||||
person2.addPhone( phone3 );
|
||||
person2.addPhone(phone2);
|
||||
person2.addPhone(phone3);
|
||||
|
||||
CreditCardPayment creditCardPayment = new CreditCardPayment();
|
||||
creditCardPayment.setCompleted( true );
|
||||
creditCardPayment.setAmount( BigDecimal.ZERO );
|
||||
creditCardPayment.setPerson( person1 );
|
||||
creditCardPayment.setCompleted(true);
|
||||
creditCardPayment.setAmount(BigDecimal.ZERO);
|
||||
creditCardPayment.setPerson(person1);
|
||||
|
||||
WireTransferPayment wireTransferPayment = new WireTransferPayment();
|
||||
wireTransferPayment.setCompleted( true );
|
||||
wireTransferPayment.setAmount( BigDecimal.valueOf( 100 ) );
|
||||
wireTransferPayment.setPerson( person2 );
|
||||
wireTransferPayment.setCompleted(true);
|
||||
wireTransferPayment.setAmount(BigDecimal.valueOf(100));
|
||||
wireTransferPayment.setPerson(person2);
|
||||
|
||||
entityManager.persist( creditCardPayment );
|
||||
entityManager.persist( wireTransferPayment );
|
||||
entityManager.persist(creditCardPayment);
|
||||
entityManager.persist(wireTransferPayment);
|
||||
|
||||
Partner partner = new Partner( "John Doe" );
|
||||
entityManager.persist( partner );
|
||||
Partner partner = new Partner("John Doe");
|
||||
entityManager.persist(partner);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_criteria_typedquery_entity_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-typedquery-entity-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Person> criteria = builder.createQuery( Person.class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
criteria.select( root );
|
||||
criteria.where( builder.equal( root.get( Person_.name ), "John Doe" ) );
|
||||
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
|
||||
Root<Person> root = criteria.from(Person.class);
|
||||
criteria.select(root);
|
||||
criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
|
||||
|
||||
List<Person> persons = entityManager.createQuery( criteria ).getResultList();
|
||||
List<Person> persons = entityManager.createQuery(criteria).getResultList();
|
||||
//end::criteria-typedquery-entity-example[]
|
||||
assertEquals(1, persons.size());
|
||||
});
|
||||
|
@ -148,16 +148,16 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void test_criteria_typedquery_expression_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-typedquery-expression-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<String> criteria = builder.createQuery( String.class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
criteria.select( root.get( Person_.nickName ) );
|
||||
criteria.where( builder.equal( root.get( Person_.name ), "John Doe" ) );
|
||||
CriteriaQuery<String> criteria = builder.createQuery(String.class);
|
||||
Root<Person> root = criteria.from(Person.class);
|
||||
criteria.select(root.get(Person_.nickName));
|
||||
criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
|
||||
|
||||
List<String> nickNames = entityManager.createQuery( criteria ).getResultList();
|
||||
List<String> nickNames = entityManager.createQuery(criteria).getResultList();
|
||||
//end::criteria-typedquery-expression-example[]
|
||||
assertEquals(1, nickNames.size());
|
||||
});
|
||||
|
@ -166,20 +166,20 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void test_criteria_typedquery_multiselect_explicit_array_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-typedquery-multiselect-array-explicit-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Object[]> criteria = builder.createQuery( Object[].class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
CriteriaQuery<Object[]> criteria = builder.createQuery(Object[].class);
|
||||
Root<Person> root = criteria.from(Person.class);
|
||||
|
||||
Path<Long> idPath = root.get( Person_.id );
|
||||
Path<String> nickNamePath = root.get( Person_.nickName);
|
||||
Path<Long> idPath = root.get(Person_.id);
|
||||
Path<String> nickNamePath = root.get(Person_.nickName);
|
||||
|
||||
criteria.select( builder.array( idPath, nickNamePath ) );
|
||||
criteria.where( builder.equal( root.get( Person_.name ), "John Doe" ) );
|
||||
criteria.select(builder.array(idPath, nickNamePath));
|
||||
criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
|
||||
|
||||
List<Object[]> idAndNickNames = entityManager.createQuery( criteria ).getResultList();
|
||||
List<Object[]> idAndNickNames = entityManager.createQuery(criteria).getResultList();
|
||||
//end::criteria-typedquery-multiselect-array-explicit-example[]
|
||||
assertEquals(1, idAndNickNames.size());
|
||||
});
|
||||
|
@ -188,20 +188,20 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void test_criteria_typedquery_multiselect_implicit_array_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-typedquery-multiselect-array-implicit-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Object[]> criteria = builder.createQuery( Object[].class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
CriteriaQuery<Object[]> criteria = builder.createQuery(Object[].class);
|
||||
Root<Person> root = criteria.from(Person.class);
|
||||
|
||||
Path<Long> idPath = root.get( Person_.id );
|
||||
Path<String> nickNamePath = root.get( Person_.nickName);
|
||||
Path<Long> idPath = root.get(Person_.id);
|
||||
Path<String> nickNamePath = root.get(Person_.nickName);
|
||||
|
||||
criteria.multiselect( idPath, nickNamePath );
|
||||
criteria.where( builder.equal( root.get( Person_.name ), "John Doe" ) );
|
||||
criteria.multiselect(idPath, nickNamePath);
|
||||
criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
|
||||
|
||||
List<Object[]> idAndNickNames = entityManager.createQuery( criteria ).getResultList();
|
||||
List<Object[]> idAndNickNames = entityManager.createQuery(criteria).getResultList();
|
||||
//end::criteria-typedquery-multiselect-array-implicit-example[]
|
||||
assertEquals(1, idAndNickNames.size());
|
||||
});
|
||||
|
@ -210,21 +210,21 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void test_criteria_typedquery_wrapper_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-typedquery-wrapper-example[]
|
||||
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<PersonWrapper> criteria = builder.createQuery( PersonWrapper.class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
CriteriaQuery<PersonWrapper> criteria = builder.createQuery(PersonWrapper.class);
|
||||
Root<Person> root = criteria.from(Person.class);
|
||||
|
||||
Path<Long> idPath = root.get( Person_.id );
|
||||
Path<String> nickNamePath = root.get( Person_.nickName);
|
||||
Path<Long> idPath = root.get(Person_.id);
|
||||
Path<String> nickNamePath = root.get(Person_.nickName);
|
||||
|
||||
criteria.select( builder.construct( PersonWrapper.class, idPath, nickNamePath ) );
|
||||
criteria.where( builder.equal( root.get( Person_.name ), "John Doe" ) );
|
||||
criteria.select(builder.construct(PersonWrapper.class, idPath, nickNamePath));
|
||||
criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
|
||||
|
||||
List<PersonWrapper> wrappers = entityManager.createQuery( criteria ).getResultList();
|
||||
List<PersonWrapper> wrappers = entityManager.createQuery(criteria).getResultList();
|
||||
//end::criteria-typedquery-wrapper-example[]
|
||||
assertEquals(1, wrappers.size());
|
||||
});
|
||||
|
@ -233,30 +233,30 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void test_criteria_tuple_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-tuple-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Tuple> criteria = builder.createQuery( Tuple.class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
|
||||
Root<Person> root = criteria.from(Person.class);
|
||||
|
||||
Path<Long> idPath = root.get( Person_.id );
|
||||
Path<String> nickNamePath = root.get( Person_.nickName);
|
||||
Path<Long> idPath = root.get(Person_.id);
|
||||
Path<String> nickNamePath = root.get(Person_.nickName);
|
||||
|
||||
criteria.multiselect( idPath, nickNamePath );
|
||||
criteria.where( builder.equal( root.get( Person_.name ), "John Doe" ) );
|
||||
criteria.multiselect(idPath, nickNamePath);
|
||||
criteria.where(builder.equal(root.get(Person_.name), "John Doe"));
|
||||
|
||||
List<Tuple> tuples = entityManager.createQuery( criteria ).getResultList();
|
||||
List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
|
||||
|
||||
for ( Tuple tuple : tuples ) {
|
||||
Long id = tuple.get( idPath );
|
||||
String nickName = tuple.get( nickNamePath );
|
||||
for (Tuple tuple : tuples) {
|
||||
Long id = tuple.get(idPath);
|
||||
String nickName = tuple.get(nickNamePath);
|
||||
}
|
||||
|
||||
//or using indices
|
||||
for ( Tuple tuple : tuples ) {
|
||||
Long id = (Long) tuple.get( 0 );
|
||||
String nickName = (String) tuple.get( 1 );
|
||||
for (Tuple tuple : tuples) {
|
||||
Long id = (Long) tuple.get(0);
|
||||
String nickName = (String) tuple.get(1);
|
||||
}
|
||||
//end::criteria-tuple-example[]
|
||||
assertEquals(1, tuples.size());
|
||||
|
@ -266,12 +266,12 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void test_criteria_from_root_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-from-root-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Person> criteria = builder.createQuery( Person.class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
|
||||
Root<Person> root = criteria.from(Person.class);
|
||||
//end::criteria-from-root-example[]
|
||||
});
|
||||
}
|
||||
|
@ -279,29 +279,29 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void test_criteria_from_multiple_root_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String address = "Earth";
|
||||
String prefix = "J%";
|
||||
//tag::criteria-from-multiple-root-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Tuple> criteria = builder.createQuery( Tuple.class );
|
||||
CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
|
||||
|
||||
Root<Person> personRoot = criteria.from( Person.class );
|
||||
Root<Partner> partnerRoot = criteria.from( Partner.class );
|
||||
criteria.multiselect( personRoot, partnerRoot );
|
||||
Root<Person> personRoot = criteria.from(Person.class);
|
||||
Root<Partner> partnerRoot = criteria.from(Partner.class);
|
||||
criteria.multiselect(personRoot, partnerRoot);
|
||||
|
||||
Predicate personRestriction = builder.and(
|
||||
builder.equal( personRoot.get( Person_.address ), address ),
|
||||
builder.isNotEmpty( personRoot.get( Person_.phones ) )
|
||||
builder.equal(personRoot.get(Person_.address), address),
|
||||
builder.isNotEmpty(personRoot.get(Person_.phones))
|
||||
);
|
||||
Predicate partnerRestriction = builder.and(
|
||||
builder.like( partnerRoot.get( Partner_.name ), prefix ),
|
||||
builder.equal( partnerRoot.get( Partner_.version ), 0 )
|
||||
builder.like(partnerRoot.get(Partner_.name), prefix),
|
||||
builder.equal(partnerRoot.get(Partner_.version), 0)
|
||||
);
|
||||
criteria.where( builder.and( personRestriction, partnerRestriction ) );
|
||||
criteria.where(builder.and(personRestriction, partnerRestriction));
|
||||
|
||||
List<Tuple> tuples = entityManager.createQuery( criteria ).getResultList();
|
||||
List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
|
||||
//end::criteria-from-multiple-root-example[]
|
||||
assertEquals(2, tuples.size());
|
||||
});
|
||||
|
@ -310,64 +310,64 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void test_criteria_from_join_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-from-join-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Phone> criteria = builder.createQuery( Phone.class );
|
||||
Root<Phone> root = criteria.from( Phone.class );
|
||||
CriteriaQuery<Phone> criteria = builder.createQuery(Phone.class);
|
||||
Root<Phone> root = criteria.from(Phone.class);
|
||||
|
||||
// Phone.person is a @ManyToOne
|
||||
Join<Phone, Person> personJoin = root.join( Phone_.person );
|
||||
Join<Phone, Person> personJoin = root.join(Phone_.person);
|
||||
// Person.addresses is an @ElementCollection
|
||||
Join<Person, String> addressesJoin = personJoin.join( Person_.addresses );
|
||||
Join<Person, String> addressesJoin = personJoin.join(Person_.addresses);
|
||||
|
||||
criteria.where( builder.isNotEmpty( root.get( Phone_.calls ) ) );
|
||||
criteria.where(builder.isNotEmpty(root.get(Phone_.calls)));
|
||||
|
||||
List<Phone> phones = entityManager.createQuery( criteria ).getResultList();
|
||||
List<Phone> phones = entityManager.createQuery(criteria).getResultList();
|
||||
//end::criteria-from-join-example[]
|
||||
assertEquals( 1, phones.size() );
|
||||
assertEquals(1, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_criteria_from_fetch_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-from-fetch-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Phone> criteria = builder.createQuery( Phone.class );
|
||||
Root<Phone> root = criteria.from( Phone.class );
|
||||
CriteriaQuery<Phone> criteria = builder.createQuery(Phone.class);
|
||||
Root<Phone> root = criteria.from(Phone.class);
|
||||
|
||||
// Phone.person is a @ManyToOne
|
||||
Fetch<Phone, Person> personFetch = root.fetch( Phone_.person );
|
||||
Fetch<Phone, Person> personFetch = root.fetch(Phone_.person);
|
||||
// Person.addresses is an @ElementCollection
|
||||
Fetch<Person, String> addressesJoin = personFetch.fetch( Person_.addresses );
|
||||
Fetch<Person, String> addressesJoin = personFetch.fetch(Person_.addresses);
|
||||
|
||||
criteria.where( builder.isNotEmpty( root.get( Phone_.calls ) ) );
|
||||
criteria.where(builder.isNotEmpty(root.get(Phone_.calls)));
|
||||
|
||||
List<Phone> phones = entityManager.createQuery( criteria ).getResultList();
|
||||
List<Phone> phones = entityManager.createQuery(criteria).getResultList();
|
||||
//end::criteria-from-fetch-example[]
|
||||
assertEquals( 1, phones.size() );
|
||||
assertEquals(1, phones.size());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_criteria_param_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-param-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Person> criteria = builder.createQuery( Person.class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
CriteriaQuery<Person> criteria = builder.createQuery(Person.class);
|
||||
Root<Person> root = criteria.from(Person.class);
|
||||
|
||||
ParameterExpression<String> nickNameParameter = builder.parameter( String.class );
|
||||
criteria.where( builder.equal( root.get( Person_.nickName ), nickNameParameter ) );
|
||||
ParameterExpression<String> nickNameParameter = builder.parameter(String.class);
|
||||
criteria.where(builder.equal(root.get(Person_.nickName), nickNameParameter));
|
||||
|
||||
TypedQuery<Person> query = entityManager.createQuery( criteria );
|
||||
query.setParameter( nickNameParameter, "JD" );
|
||||
TypedQuery<Person> query = entityManager.createQuery(criteria);
|
||||
query.setParameter(nickNameParameter, "JD");
|
||||
List<Person> persons = query.getResultList();
|
||||
//end::criteria-param-example[]
|
||||
assertEquals(1, persons.size());
|
||||
|
@ -377,21 +377,21 @@ public class CriteriaTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void test_criteria_group_by_example() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::criteria-group-by-example[]
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Tuple> criteria = builder.createQuery( Tuple.class );
|
||||
Root<Person> root = criteria.from( Person.class );
|
||||
CriteriaQuery<Tuple> criteria = builder.createQuery(Tuple.class);
|
||||
Root<Person> root = criteria.from(Person.class);
|
||||
|
||||
criteria.groupBy(root.get("address"));
|
||||
criteria.multiselect(root.get("address"), builder.count(root));
|
||||
|
||||
List<Tuple> tuples = entityManager.createQuery( criteria ).getResultList();
|
||||
List<Tuple> tuples = entityManager.createQuery(criteria).getResultList();
|
||||
|
||||
for ( Tuple tuple : tuples ) {
|
||||
String name = (String) tuple.get( 0 );
|
||||
Long count = (Long) tuple.get( 1 );
|
||||
for (Tuple tuple : tuples) {
|
||||
String name = (String) tuple.get(0);
|
||||
Long count = (Long) tuple.get(1);
|
||||
}
|
||||
//end::criteria-group-by-example[]
|
||||
assertEquals(2, tuples.size());
|
||||
|
|
|
@ -47,16 +47,16 @@ public class CustomRevisionEntityTest extends BaseEntityManagerFunctionalTestCas
|
|||
@Test
|
||||
public void test() {
|
||||
//tag::envers-revisionlog-RevisionEntity-persist-example[]
|
||||
CurrentUser.INSTANCE.logIn( "Vlad Mihalcea" );
|
||||
CurrentUser.INSTANCE.logIn("Vlad Mihalcea");
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = new Customer();
|
||||
customer.setId( 1L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe" );
|
||||
customer.setId(1L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
|
||||
CurrentUser.INSTANCE.logOut();
|
||||
//end::envers-revisionlog-RevisionEntity-persist-example[]
|
||||
|
@ -73,7 +73,7 @@ public class CustomRevisionEntityTest extends BaseEntityManagerFunctionalTestCas
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
@ -119,7 +119,7 @@ public class CustomRevisionEntityTest extends BaseEntityManagerFunctionalTestCas
|
|||
private static final ThreadLocal<String> storage = new ThreadLocal<>();
|
||||
|
||||
public void logIn(String user) {
|
||||
storage.set( user );
|
||||
storage.set(user);
|
||||
}
|
||||
|
||||
public void logOut() {
|
||||
|
@ -135,7 +135,7 @@ public class CustomRevisionEntityTest extends BaseEntityManagerFunctionalTestCas
|
|||
//tag::envers-revisionlog-RevisionEntity-example[]
|
||||
@Entity(name = "CustomRevisionEntity")
|
||||
@Table(name = "CUSTOM_REV_INFO")
|
||||
@RevisionEntity( CustomRevisionEntityListener.class )
|
||||
@RevisionEntity(CustomRevisionEntityListener.class)
|
||||
public static class CustomRevisionEntity extends DefaultRevisionEntity {
|
||||
|
||||
private String username;
|
||||
|
@ -144,7 +144,7 @@ public class CustomRevisionEntityTest extends BaseEntityManagerFunctionalTestCas
|
|||
return username;
|
||||
}
|
||||
|
||||
public void setUsername( String username ) {
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
}
|
||||
|
@ -153,9 +153,9 @@ public class CustomRevisionEntityTest extends BaseEntityManagerFunctionalTestCas
|
|||
//tag::envers-revisionlog-RevisionListener-example[]
|
||||
public static class CustomRevisionEntityListener implements RevisionListener {
|
||||
|
||||
public void newRevision( Object revisionEntity ) {
|
||||
public void newRevision(Object revisionEntity) {
|
||||
CustomRevisionEntity customRevisionEntity =
|
||||
( CustomRevisionEntity ) revisionEntity;
|
||||
(CustomRevisionEntity) revisionEntity;
|
||||
|
||||
customRevisionEntity.setUsername(
|
||||
CurrentUser.INSTANCE.get()
|
||||
|
|
|
@ -41,98 +41,98 @@ public class DefaultAuditTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-audited-insert-example[]
|
||||
Customer customer = new Customer();
|
||||
customer.setId( 1L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe" );
|
||||
customer.setId(1L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
|
||||
entityManager.persist( customer );
|
||||
entityManager.persist(customer);
|
||||
//end::envers-audited-insert-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-audited-update-example[]
|
||||
Customer customer = entityManager.find( Customer.class, 1L );
|
||||
customer.setLastName( "Doe Jr." );
|
||||
Customer customer = entityManager.find(Customer.class, 1L);
|
||||
customer.setLastName("Doe Jr.");
|
||||
//end::envers-audited-update-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-audited-delete-example[]
|
||||
Customer customer = entityManager.getReference( Customer.class, 1L );
|
||||
entityManager.remove( customer );
|
||||
Customer customer = entityManager.getReference(Customer.class, 1L);
|
||||
entityManager.remove(customer);
|
||||
//end::envers-audited-delete-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
//tag::envers-audited-revisions-example[]
|
||||
List<Number> revisions = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
return AuditReaderFactory.get( entityManager ).getRevisions(
|
||||
List<Number> revisions = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
return AuditReaderFactory.get(entityManager).getRevisions(
|
||||
Customer.class,
|
||||
1L
|
||||
);
|
||||
} );
|
||||
});
|
||||
//end::envers-audited-revisions-example[]
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-audited-rev1-example[]
|
||||
Customer customer = (Customer) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, revisions.get( 0 ) )
|
||||
.forEntitiesAtRevision(Customer.class, revisions.get(0))
|
||||
.getSingleResult();
|
||||
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
//end::envers-audited-rev1-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-audited-rev2-example[]
|
||||
Customer customer = (Customer) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, revisions.get( 1 ) )
|
||||
.forEntitiesAtRevision(Customer.class, revisions.get(1))
|
||||
.getSingleResult();
|
||||
|
||||
assertEquals("Doe Jr.", customer.getLastName());
|
||||
//end::envers-audited-rev2-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-audited-rev3-example[]
|
||||
try {
|
||||
Customer customer = (Customer) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, revisions.get( 2 ) )
|
||||
.forEntitiesAtRevision(Customer.class, revisions.get(2))
|
||||
.getSingleResult();
|
||||
|
||||
fail("The Customer was deleted at this revision: " + revisions.get( 2 ));
|
||||
fail("The Customer was deleted at this revision: " + revisions.get(2));
|
||||
}
|
||||
catch (NoResultException expected) {
|
||||
}
|
||||
//end::envers-audited-rev3-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-audited-rev4-example[]
|
||||
Customer customer = (Customer) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision(
|
||||
Customer.class,
|
||||
Customer.class.getName(),
|
||||
revisions.get( 2 ),
|
||||
true )
|
||||
revisions.get(2),
|
||||
true)
|
||||
.getSingleResult();
|
||||
|
||||
assertEquals( Long.valueOf( 1L ), customer.getId() );
|
||||
assertNull( customer.getFirstName() );
|
||||
assertNull( customer.getLastName() );
|
||||
assertNull( customer.getCreatedOn() );
|
||||
assertEquals(Long.valueOf(1L), customer.getId());
|
||||
assertNull(customer.getFirstName());
|
||||
assertNull(customer.getLastName());
|
||||
assertNull(customer.getCreatedOn());
|
||||
//end::envers-audited-rev4-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::envers-audited-mapping-example[]
|
||||
|
@ -147,7 +147,7 @@ public class DefaultAuditTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
|
|
@ -46,14 +46,14 @@ public class EntityTypeChangeAuditDefaultTrackingTest extends BaseEntityManagerF
|
|||
@Test
|
||||
public void testLifecycle() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = new Customer();
|
||||
customer.setId( 1L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe" );
|
||||
customer.setId(1L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
|
||||
EntityManagerFactory entityManagerFactory = null;
|
||||
try {
|
||||
|
@ -71,24 +71,24 @@ public class EntityTypeChangeAuditDefaultTrackingTest extends BaseEntityManagerF
|
|||
);
|
||||
entityManagerFactory = Bootstrap
|
||||
.getEntityManagerFactoryBuilder(
|
||||
new TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ),
|
||||
settings )
|
||||
new TestingPersistenceUnitDescriptorImpl(getClass().getSimpleName()),
|
||||
settings)
|
||||
.build()
|
||||
.unwrap( SessionFactoryImplementor.class );
|
||||
.unwrap(SessionFactoryImplementor.class);
|
||||
|
||||
final EntityManagerFactory emf = entityManagerFactory;
|
||||
|
||||
doInJPA( () -> emf, entityManager -> {
|
||||
doInJPA(() -> emf, entityManager -> {
|
||||
ApplicationCustomer customer = new ApplicationCustomer();
|
||||
customer.setId( 2L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe Jr." );
|
||||
customer.setId(2L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe Jr.");
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
}
|
||||
finally {
|
||||
if ( entityManagerFactory != null ) {
|
||||
if (entityManagerFactory != null) {
|
||||
entityManagerFactory.close();
|
||||
}
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ public class EntityTypeChangeAuditDefaultTrackingTest extends BaseEntityManagerF
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
@ -154,7 +154,7 @@ public class EntityTypeChangeAuditDefaultTrackingTest extends BaseEntityManagerF
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
|
|
@ -54,28 +54,28 @@ public class EntityTypeChangeAuditTest extends BaseEntityManagerFunctionalTestCa
|
|||
@Test
|
||||
public void test() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = new Customer();
|
||||
customer.setId( 1L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe" );
|
||||
customer.setId(1L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-tracking-modified-entities-queries-example1[]
|
||||
assertEquals(
|
||||
"org.hibernate.userguide.envers.EntityTypeChangeAuditTest$Customer",
|
||||
AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.getCrossTypeRevisionChangesReader()
|
||||
.findEntityTypes( 1 )
|
||||
.findEntityTypes(1)
|
||||
.iterator().next()
|
||||
.getFirst()
|
||||
);
|
||||
);
|
||||
//end::envers-tracking-modified-entities-queries-example1[]
|
||||
} );
|
||||
});
|
||||
|
||||
EntityManagerFactory entityManagerFactory = null;
|
||||
try {
|
||||
|
@ -85,41 +85,41 @@ public class EntityTypeChangeAuditTest extends BaseEntityManagerFunctionalTestCa
|
|||
Arrays.asList(
|
||||
ApplicationCustomer.class,
|
||||
CustomTrackingRevisionEntity.class
|
||||
)
|
||||
);
|
||||
)
|
||||
);
|
||||
settings.put(
|
||||
AvailableSettings.HBM2DDL_AUTO,
|
||||
"update"
|
||||
);
|
||||
);
|
||||
entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(
|
||||
new TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ),
|
||||
new TestingPersistenceUnitDescriptorImpl(getClass().getSimpleName()),
|
||||
settings
|
||||
).build().unwrap( SessionFactoryImplementor.class );
|
||||
).build().unwrap(SessionFactoryImplementor.class);
|
||||
|
||||
final EntityManagerFactory emf = entityManagerFactory;
|
||||
|
||||
doInJPA( () -> emf, entityManager -> {
|
||||
ApplicationCustomer customer = entityManager.find( ApplicationCustomer.class, 1L );
|
||||
customer.setLastName( "Doe Jr." );
|
||||
} );
|
||||
doInJPA(() -> emf, entityManager -> {
|
||||
ApplicationCustomer customer = entityManager.find(ApplicationCustomer.class, 1L);
|
||||
customer.setLastName("Doe Jr.");
|
||||
});
|
||||
|
||||
doInJPA( () -> emf, entityManager -> {
|
||||
doInJPA(() -> emf, entityManager -> {
|
||||
//tag::envers-tracking-modified-entities-queries-example2[]
|
||||
assertEquals(
|
||||
"org.hibernate.userguide.envers.EntityTypeChangeAuditTest$ApplicationCustomer",
|
||||
AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.getCrossTypeRevisionChangesReader()
|
||||
// 52 is the next id on the DB due to allocationSize defaulting to 50
|
||||
.findEntityTypes( 52 )
|
||||
.findEntityTypes(52)
|
||||
.iterator().next()
|
||||
.getFirst()
|
||||
);
|
||||
);
|
||||
//end::envers-tracking-modified-entities-queries-example2[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
finally {
|
||||
if ( entityManagerFactory != null ) {
|
||||
if (entityManagerFactory != null) {
|
||||
entityManagerFactory.close();
|
||||
}
|
||||
}
|
||||
|
@ -137,7 +137,7 @@ public class EntityTypeChangeAuditTest extends BaseEntityManagerFunctionalTestCa
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
@ -192,7 +192,7 @@ public class EntityTypeChangeAuditTest extends BaseEntityManagerFunctionalTestCa
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
@ -244,9 +244,9 @@ public class EntityTypeChangeAuditTest extends BaseEntityManagerFunctionalTestCa
|
|||
@ElementCollection
|
||||
@JoinTable(
|
||||
name = "REVCHANGES",
|
||||
joinColumns = @JoinColumn( name = "REV" )
|
||||
)
|
||||
@Column( name = "ENTITYNAME" )
|
||||
joinColumns = @JoinColumn(name = "REV")
|
||||
)
|
||||
@Column(name = "ENTITYNAME")
|
||||
@ModifiedEntityNames
|
||||
private Set<String> modifiedEntityNames = new HashSet<>();
|
||||
|
||||
|
|
|
@ -61,14 +61,14 @@ public class EntityTypeChangeAuditTrackingRevisionListenerTest extends BaseEntit
|
|||
@Test
|
||||
public void testLifecycle() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = new Customer();
|
||||
customer.setId( 1L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe" );
|
||||
customer.setId(1L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
|
||||
EntityManagerFactory entityManagerFactory = null;
|
||||
try {
|
||||
|
@ -86,24 +86,24 @@ public class EntityTypeChangeAuditTrackingRevisionListenerTest extends BaseEntit
|
|||
"update"
|
||||
);
|
||||
entityManagerFactory = Bootstrap.getEntityManagerFactoryBuilder(
|
||||
new TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ),
|
||||
new TestingPersistenceUnitDescriptorImpl(getClass().getSimpleName()),
|
||||
settings
|
||||
).build().unwrap( SessionFactoryImplementor.class );
|
||||
).build().unwrap(SessionFactoryImplementor.class);
|
||||
|
||||
final EntityManagerFactory emf = entityManagerFactory;
|
||||
|
||||
doInJPA( () -> emf, entityManager -> {
|
||||
doInJPA(() -> emf, entityManager -> {
|
||||
ApplicationCustomer customer = new ApplicationCustomer();
|
||||
customer.setId( 2L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe Jr." );
|
||||
customer.setId(2L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe Jr.");
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
|
||||
doInJPA( () -> emf, entityManager -> {
|
||||
doInJPA(() -> emf, entityManager -> {
|
||||
//tag::envers-tracking-modified-entities-revchanges-query-example[]
|
||||
AuditReader auditReader = AuditReaderFactory.get( entityManager );
|
||||
AuditReader auditReader = AuditReaderFactory.get(entityManager);
|
||||
|
||||
List<Number> revisions = auditReader.getRevisions(
|
||||
ApplicationCustomer.class,
|
||||
|
@ -112,11 +112,11 @@ public class EntityTypeChangeAuditTrackingRevisionListenerTest extends BaseEntit
|
|||
|
||||
CustomTrackingRevisionEntity revEntity = auditReader.findRevision(
|
||||
CustomTrackingRevisionEntity.class,
|
||||
revisions.get( 0 )
|
||||
revisions.get(0)
|
||||
);
|
||||
|
||||
Set<EntityType> modifiedEntityTypes = revEntity.getModifiedEntityTypes();
|
||||
assertEquals( 1, modifiedEntityTypes.size() );
|
||||
assertEquals(1, modifiedEntityTypes.size());
|
||||
|
||||
EntityType entityType = modifiedEntityTypes.iterator().next();
|
||||
assertEquals(
|
||||
|
@ -124,10 +124,10 @@ public class EntityTypeChangeAuditTrackingRevisionListenerTest extends BaseEntit
|
|||
entityType.getEntityClassName()
|
||||
);
|
||||
//end::envers-tracking-modified-entities-revchanges-query-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
finally {
|
||||
if ( entityManagerFactory != null ) {
|
||||
if (entityManagerFactory != null) {
|
||||
entityManagerFactory.close();
|
||||
}
|
||||
}
|
||||
|
@ -144,7 +144,7 @@ public class EntityTypeChangeAuditTrackingRevisionListenerTest extends BaseEntit
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
@ -193,7 +193,7 @@ public class EntityTypeChangeAuditTrackingRevisionListenerTest extends BaseEntit
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
@ -239,13 +239,13 @@ public class EntityTypeChangeAuditTrackingRevisionListenerTest extends BaseEntit
|
|||
String entityName,
|
||||
Object entityId,
|
||||
RevisionType revisionType,
|
||||
Object revisionEntity ) {
|
||||
Object revisionEntity) {
|
||||
String type = entityClass.getName();
|
||||
( (CustomTrackingRevisionEntity) revisionEntity ).addModifiedEntityType( type );
|
||||
((CustomTrackingRevisionEntity) revisionEntity).addModifiedEntityType(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void newRevision( Object revisionEntity ) {
|
||||
public void newRevision(Object revisionEntity) {
|
||||
}
|
||||
}
|
||||
//end::envers-tracking-modified-entities-revchanges-EntityTrackingRevisionListener-example[]
|
||||
|
@ -253,7 +253,7 @@ public class EntityTypeChangeAuditTrackingRevisionListenerTest extends BaseEntit
|
|||
//tag::envers-tracking-modified-entities-revchanges-RevisionEntity-example[]
|
||||
@Entity(name = "CustomTrackingRevisionEntity")
|
||||
@Table(name = "TRACKING_REV_INFO")
|
||||
@RevisionEntity( CustomTrackingRevisionListener.class )
|
||||
@RevisionEntity(CustomTrackingRevisionListener.class)
|
||||
public static class CustomTrackingRevisionEntity {
|
||||
|
||||
@Id
|
||||
|
@ -277,8 +277,8 @@ public class EntityTypeChangeAuditTrackingRevisionListenerTest extends BaseEntit
|
|||
return modifiedEntityTypes;
|
||||
}
|
||||
|
||||
public void addModifiedEntityType(String entityClassName ) {
|
||||
modifiedEntityTypes.add( new EntityType( this, entityClassName ) );
|
||||
public void addModifiedEntityType(String entityClassName) {
|
||||
modifiedEntityTypes.add(new EntityType(this, entityClassName));
|
||||
}
|
||||
}
|
||||
//end::envers-tracking-modified-entities-revchanges-RevisionEntity-example[]
|
||||
|
|
|
@ -40,21 +40,21 @@ public class ModifiedFlagsAuditTest extends BaseEntityManagerFunctionalTestCase
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = new Customer();
|
||||
customer.setId( 1L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe" );
|
||||
customer.setId(1L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-tracking-properties-changes-example[]
|
||||
Customer customer = entityManager.find( Customer.class, 1L );
|
||||
customer.setLastName( "Doe Jr." );
|
||||
Customer customer = entityManager.find(Customer.class, 1L);
|
||||
customer.setLastName("Doe Jr.");
|
||||
//end::envers-tracking-properties-changes-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::envers-tracking-properties-changes-mapping-example[]
|
||||
|
@ -69,7 +69,7 @@ public class ModifiedFlagsAuditTest extends BaseEntityManagerFunctionalTestCase
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
|
|
@ -59,126 +59,126 @@ public class QueryAuditAdressCountryTest extends BaseEntityManagerFunctionalTest
|
|||
@Test
|
||||
@SkipForDialect(value = HSQLDialect.class, comment = "No idea why this fails. Looks like a HSQLDB bug")
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Country country = new Country();
|
||||
country.setId( 1L );
|
||||
country.setName( "România" );
|
||||
entityManager.persist( country );
|
||||
country.setId(1L);
|
||||
country.setName("România");
|
||||
entityManager.persist(country);
|
||||
|
||||
Address address = new Address();
|
||||
address.setId( 1L );
|
||||
address.setCountry( country );
|
||||
address.setCity( "Cluj-Napoca" );
|
||||
address.setStreet( "Bulevardul Eroilor" );
|
||||
address.setStreetNumber( "1 A" );
|
||||
entityManager.persist( address );
|
||||
address.setId(1L);
|
||||
address.setCountry(country);
|
||||
address.setCity("Cluj-Napoca");
|
||||
address.setStreet("Bulevardul Eroilor");
|
||||
address.setStreetNumber("1 A");
|
||||
entityManager.persist(address);
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setId( 1L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe" );
|
||||
customer.setAddress( address );
|
||||
customer.setId(1L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
customer.setAddress(address);
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-querying-entity-relation-nested-join-restriction[]
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, 1 )
|
||||
.traverseRelation( "address", JoinType.INNER )
|
||||
.traverseRelation( "country", JoinType.INNER )
|
||||
.add( AuditEntity.property( "name" ).eq( "România" ) )
|
||||
.forEntitiesAtRevision(Customer.class, 1)
|
||||
.traverseRelation("address", JoinType.INNER)
|
||||
.traverseRelation("country", JoinType.INNER)
|
||||
.add(AuditEntity.property("name").eq("România"))
|
||||
.getResultList();
|
||||
|
||||
assertEquals( 1, customers.size() );
|
||||
assertEquals(1, customers.size());
|
||||
//end::envers-querying-entity-relation-nested-join-restriction[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-querying-entity-relation-join-multiple-restrictions[]
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, 1 )
|
||||
.traverseRelation( "address", JoinType.LEFT, "a" )
|
||||
.forEntitiesAtRevision(Customer.class, 1)
|
||||
.traverseRelation("address", JoinType.LEFT, "a")
|
||||
.add(
|
||||
AuditEntity.or(
|
||||
AuditEntity.property( "a", "city" ).eq( "Cluj-Napoca" ),
|
||||
AuditEntity.relatedId( "country" ).eq( null )
|
||||
AuditEntity.property("a", "city").eq("Cluj-Napoca"),
|
||||
AuditEntity.relatedId("country").eq(null)
|
||||
)
|
||||
)
|
||||
.getResultList();
|
||||
//end::envers-querying-entity-relation-join-multiple-restrictions[]
|
||||
|
||||
assertEquals( 1, customers.size() );
|
||||
} );
|
||||
assertEquals(1, customers.size());
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-querying-entity-relation-nested-join-multiple-restrictions[]
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, 1 )
|
||||
.traverseRelation( "address", JoinType.INNER, "a" )
|
||||
.traverseRelation( "country", JoinType.INNER, "cn" )
|
||||
.forEntitiesAtRevision(Customer.class, 1)
|
||||
.traverseRelation("address", JoinType.INNER, "a")
|
||||
.traverseRelation("country", JoinType.INNER, "cn")
|
||||
.up()
|
||||
.up()
|
||||
.add(
|
||||
AuditEntity.disjunction()
|
||||
.add( AuditEntity.property( "a", "city" ).eq( "Cluj-Napoca" ) )
|
||||
.add( AuditEntity.property( "cn", "name" ).eq( "România" ) )
|
||||
.add(AuditEntity.property("a", "city").eq("Cluj-Napoca"))
|
||||
.add(AuditEntity.property("cn", "name").eq("România"))
|
||||
)
|
||||
.addOrder( AuditEntity.property( "createdOn" ).asc() )
|
||||
.addOrder(AuditEntity.property("createdOn").asc())
|
||||
.getResultList();
|
||||
//end::envers-querying-entity-relation-nested-join-multiple-restrictions[]
|
||||
|
||||
assertEquals( 1, customers.size() );
|
||||
} );
|
||||
assertEquals(1, customers.size());
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-querying-entity-relation-nested-join-multiple-restrictions-combined-entities[]
|
||||
Customer customer = entityManager.createQuery(
|
||||
"select c " +
|
||||
"from Customer c " +
|
||||
"join fetch c.address a " +
|
||||
"join fetch a.country " +
|
||||
"where c.id = :id", Customer.class )
|
||||
.setParameter( "id", 1L )
|
||||
"where c.id = :id", Customer.class)
|
||||
.setParameter("id", 1L)
|
||||
.getSingleResult();
|
||||
|
||||
customer.setLastName( "Doe Sr." );
|
||||
customer.setLastName("Doe Sr.");
|
||||
|
||||
customer.getAddress().setCity(
|
||||
customer.getAddress().getCountry().getName()
|
||||
);
|
||||
//end::envers-querying-entity-relation-nested-join-multiple-restrictions-combined-entities[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
|
||||
//tag::envers-querying-entity-relation-nested-join-multiple-restrictions-combined[]
|
||||
List<Number> revisions = AuditReaderFactory.get( entityManager ).getRevisions(
|
||||
List<Number> revisions = AuditReaderFactory.get(entityManager).getRevisions(
|
||||
Customer.class,
|
||||
1L
|
||||
);
|
||||
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, revisions.get( revisions.size() - 1 ) )
|
||||
.traverseRelation( "address", JoinType.INNER, "a" )
|
||||
.traverseRelation( "country", JoinType.INNER, "cn" )
|
||||
.forEntitiesAtRevision(Customer.class, revisions.get(revisions.size() - 1))
|
||||
.traverseRelation("address", JoinType.INNER, "a")
|
||||
.traverseRelation("country", JoinType.INNER, "cn")
|
||||
.up()
|
||||
.up()
|
||||
.add( AuditEntity.property( "a", "city" ).eqProperty( "cn", "name" ) )
|
||||
.add(AuditEntity.property("a", "city").eqProperty("cn", "name"))
|
||||
.getResultList();
|
||||
//end::envers-querying-entity-relation-nested-join-multiple-restrictions-combined[]
|
||||
|
||||
assertEquals( 1, customers.size() );
|
||||
} );
|
||||
assertEquals(1, customers.size());
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
@ -194,7 +194,7 @@ public class QueryAuditAdressCountryTest extends BaseEntityManagerFunctionalTest
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
|
|
@ -59,165 +59,165 @@ public class QueryAuditTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Address address = new Address();
|
||||
address.setId( 1L );
|
||||
address.setCountry( "România" );
|
||||
address.setCity( "Cluj-Napoca" );
|
||||
address.setStreet( "Bulevardul Eroilor" );
|
||||
address.setStreetNumber( "1 A" );
|
||||
entityManager.persist( address );
|
||||
address.setId(1L);
|
||||
address.setCountry("România");
|
||||
address.setCity("Cluj-Napoca");
|
||||
address.setStreet("Bulevardul Eroilor");
|
||||
address.setStreetNumber("1 A");
|
||||
entityManager.persist(address);
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setId( 1L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe" );
|
||||
customer.setAddress( address );
|
||||
customer.setId(1L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
customer.setAddress(address);
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.find( Customer.class, 1L );
|
||||
customer.setLastName( "Doe Jr." );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.find(Customer.class, 1L);
|
||||
customer.setLastName("Doe Jr.");
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.getReference( Customer.class, 1L );
|
||||
entityManager.remove( customer );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.getReference(Customer.class, 1L);
|
||||
entityManager.remove(customer);
|
||||
});
|
||||
|
||||
List<Number> revisions = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
return AuditReaderFactory.get( entityManager ).getRevisions(
|
||||
List<Number> revisions = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
return AuditReaderFactory.get(entityManager).getRevisions(
|
||||
Customer.class,
|
||||
1L
|
||||
);
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::entities-at-revision-example[]
|
||||
Customer customer = (Customer) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, revisions.get( 0 ) )
|
||||
.forEntitiesAtRevision(Customer.class, revisions.get(0))
|
||||
.getSingleResult();
|
||||
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
//end::entities-at-revision-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::entities-filtering-example[]
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, true, true )
|
||||
.add( AuditEntity.property( "firstName" ).eq( "John" ) )
|
||||
.forRevisionsOfEntity(Customer.class, true, true)
|
||||
.add(AuditEntity.property("firstName").eq("John"))
|
||||
.getResultList();
|
||||
|
||||
assertEquals(2, customers.size());
|
||||
assertEquals( "Doe", customers.get( 0 ).getLastName() );
|
||||
assertEquals( "Doe Jr.", customers.get( 1 ).getLastName() );
|
||||
assertEquals("Doe", customers.get(0).getLastName());
|
||||
assertEquals("Doe Jr.", customers.get(1).getLastName());
|
||||
//end::entities-filtering-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::entities-filtering-by-entity-example[]
|
||||
Address address = entityManager.getReference( Address.class, 1L );
|
||||
Address address = entityManager.getReference(Address.class, 1L);
|
||||
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, true, true )
|
||||
.add( AuditEntity.property( "address" ).eq( address ) )
|
||||
.forRevisionsOfEntity(Customer.class, true, true)
|
||||
.add(AuditEntity.property("address").eq(address))
|
||||
.getResultList();
|
||||
|
||||
assertEquals(2, customers.size());
|
||||
//end::entities-filtering-by-entity-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::entities-filtering-by-entity-identifier-example[]
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, true, true )
|
||||
.add( AuditEntity.relatedId( "address" ).eq( 1L ) )
|
||||
.forRevisionsOfEntity(Customer.class, true, true)
|
||||
.add(AuditEntity.relatedId("address").eq(1L))
|
||||
.getResultList();
|
||||
|
||||
assertEquals(2, customers.size());
|
||||
//end::entities-filtering-by-entity-identifier-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::entities-in-clause-filtering-by-entity-identifier-example[]
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, true, true )
|
||||
.add( AuditEntity.relatedId( "address" ).in( new Object[] { 1L, 2L } ) )
|
||||
.forRevisionsOfEntity(Customer.class, true, true)
|
||||
.add(AuditEntity.relatedId("address").in(new Object[] { 1L, 2L }))
|
||||
.getResultList();
|
||||
|
||||
assertEquals(2, customers.size());
|
||||
//end::entities-in-clause-filtering-by-entity-identifier-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::entities-filtering-and-pagination[]
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, true, true )
|
||||
.addOrder( AuditEntity.property( "lastName" ).desc() )
|
||||
.add( AuditEntity.relatedId( "address" ).eq( 1L ) )
|
||||
.setFirstResult( 1 )
|
||||
.setMaxResults( 2 )
|
||||
.forRevisionsOfEntity(Customer.class, true, true)
|
||||
.addOrder(AuditEntity.property("lastName").desc())
|
||||
.add(AuditEntity.relatedId("address").eq(1L))
|
||||
.setFirstResult(1)
|
||||
.setMaxResults(2)
|
||||
.getResultList();
|
||||
|
||||
assertEquals(1, customers.size());
|
||||
//end::entities-filtering-and-pagination[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::revisions-of-entity-query-example[]
|
||||
AuditQuery query = AuditReaderFactory.get( entityManager )
|
||||
AuditQuery query = AuditReaderFactory.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, false, true );
|
||||
.forRevisionsOfEntity(Customer.class, false, true);
|
||||
//end::revisions-of-entity-query-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::revisions-of-entity-query-by-revision-number-example[]
|
||||
Number revision = (Number) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, false, true )
|
||||
.addProjection( AuditEntity.revisionNumber().min() )
|
||||
.add( AuditEntity.id().eq( 1L ) )
|
||||
.add( AuditEntity.revisionNumber().gt( 2 ) )
|
||||
.forRevisionsOfEntity(Customer.class, false, true)
|
||||
.addProjection(AuditEntity.revisionNumber().min())
|
||||
.add(AuditEntity.id().eq(1L))
|
||||
.add(AuditEntity.revisionNumber().gt(2))
|
||||
.getSingleResult();
|
||||
//end::revisions-of-entity-query-by-revision-number-example[]
|
||||
|
||||
assertEquals( 3, revision );
|
||||
} );
|
||||
assertEquals(3, revision);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::revisions-of-entity-query-minimize-example[]
|
||||
Number revision = (Number) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, false, true )
|
||||
.addProjection( AuditEntity.revisionNumber().min() )
|
||||
.add( AuditEntity.id().eq( 1L ) )
|
||||
.forRevisionsOfEntity(Customer.class, false, true)
|
||||
.addProjection(AuditEntity.revisionNumber().min())
|
||||
.add(AuditEntity.id().eq(1L))
|
||||
.add(
|
||||
AuditEntity.property( "createdOn" )
|
||||
AuditEntity.property("createdOn")
|
||||
.minimize()
|
||||
.add( AuditEntity.property( "createdOn" )
|
||||
.add(AuditEntity.property("createdOn")
|
||||
.ge(
|
||||
Timestamp.from(
|
||||
LocalDateTime.now()
|
||||
.minusDays( 1 )
|
||||
.toInstant( ZoneOffset.UTC )
|
||||
.minusDays(1)
|
||||
.toInstant(ZoneOffset.UTC)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -225,54 +225,54 @@ public class QueryAuditTest extends BaseEntityManagerFunctionalTestCase {
|
|||
.getSingleResult();
|
||||
//end::revisions-of-entity-query-minimize-example[]
|
||||
|
||||
assertEquals( 1, revision );
|
||||
} );
|
||||
assertEquals(1, revision);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
{
|
||||
//tag::envers-querying-entity-relation-inner-join[]
|
||||
AuditQuery innerJoinAuditQuery = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, 1 )
|
||||
.traverseRelation( "address", JoinType.INNER );
|
||||
.forEntitiesAtRevision(Customer.class, 1)
|
||||
.traverseRelation("address", JoinType.INNER);
|
||||
//end::envers-querying-entity-relation-inner-join[]
|
||||
}
|
||||
{
|
||||
//tag::envers-querying-entity-relation-left-join[]
|
||||
AuditQuery innerJoinAuditQuery = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, 1 )
|
||||
.traverseRelation( "address", JoinType.LEFT );
|
||||
.forEntitiesAtRevision(Customer.class, 1)
|
||||
.traverseRelation("address", JoinType.LEFT);
|
||||
//end::envers-querying-entity-relation-left-join[]
|
||||
}
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-querying-entity-relation-join-restriction[]
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, 1 )
|
||||
.traverseRelation( "address", JoinType.INNER )
|
||||
.add( AuditEntity.property( "country" ).eq( "România" ) )
|
||||
.forEntitiesAtRevision(Customer.class, 1)
|
||||
.traverseRelation("address", JoinType.INNER)
|
||||
.add(AuditEntity.property("country").eq("România"))
|
||||
.getResultList();
|
||||
//end::envers-querying-entity-relation-join-restriction[]
|
||||
|
||||
assertEquals( 1, customers.size() );
|
||||
} );
|
||||
assertEquals(1, customers.size());
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::aggregate-max-revision-with-entity-example[]
|
||||
List<Customer> results = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, true, false )
|
||||
.add( AuditEntity.revisionNumber().maximize().computeAggregationInInstanceContext() )
|
||||
.forRevisionsOfEntity(Customer.class, true, false)
|
||||
.add(AuditEntity.revisionNumber().maximize().computeAggregationInInstanceContext())
|
||||
.getResultList();
|
||||
//end::aggregate-max-revision-with-entity-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Audited
|
||||
|
@ -286,7 +286,7 @@ public class QueryAuditTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
|
|
@ -54,81 +54,81 @@ public class QueryAuditWithModifiedFlagTest extends BaseEntityManagerFunctionalT
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Address address = new Address();
|
||||
address.setId( 1L );
|
||||
address.setCountry( "România" );
|
||||
address.setCity( "Cluj-Napoca" );
|
||||
address.setStreet( "Bulevardul Eroilor" );
|
||||
address.setStreetNumber( "1 A" );
|
||||
entityManager.persist( address );
|
||||
address.setId(1L);
|
||||
address.setCountry("România");
|
||||
address.setCity("Cluj-Napoca");
|
||||
address.setStreet("Bulevardul Eroilor");
|
||||
address.setStreetNumber("1 A");
|
||||
entityManager.persist(address);
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setId( 1L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe" );
|
||||
customer.setAddress( address );
|
||||
customer.setId(1L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
customer.setAddress(address);
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.find( Customer.class, 1L );
|
||||
customer.setLastName( "Doe Jr." );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.find(Customer.class, 1L);
|
||||
customer.setLastName("Doe Jr.");
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.getReference( Customer.class, 1L );
|
||||
entityManager.remove( customer );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.getReference(Customer.class, 1L);
|
||||
entityManager.remove(customer);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-tracking-properties-changes-queries-hasChanged-example[]
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, false, true )
|
||||
.add( AuditEntity.id().eq( 1L ) )
|
||||
.add( AuditEntity.property( "lastName" ).hasChanged() )
|
||||
.forRevisionsOfEntity(Customer.class, false, true)
|
||||
.add(AuditEntity.id().eq(1L))
|
||||
.add(AuditEntity.property("lastName").hasChanged())
|
||||
.getResultList();
|
||||
//end::envers-tracking-properties-changes-queries-hasChanged-example[]
|
||||
|
||||
assertEquals( 3, customers.size() );
|
||||
} );
|
||||
assertEquals(3, customers.size());
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-tracking-properties-changes-queries-hasChanged-and-hasNotChanged-example[]
|
||||
List<Customer> customers = AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forRevisionsOfEntity( Customer.class, false, true )
|
||||
.add( AuditEntity.id().eq( 1L ) )
|
||||
.add( AuditEntity.property( "lastName" ).hasChanged() )
|
||||
.add( AuditEntity.property( "firstName" ).hasNotChanged() )
|
||||
.forRevisionsOfEntity(Customer.class, false, true)
|
||||
.add(AuditEntity.id().eq(1L))
|
||||
.add(AuditEntity.property("lastName").hasChanged())
|
||||
.add(AuditEntity.property("firstName").hasNotChanged())
|
||||
.getResultList();
|
||||
//end::envers-tracking-properties-changes-queries-hasChanged-and-hasNotChanged-example[]
|
||||
|
||||
assertEquals( 1, customers.size() );
|
||||
} );
|
||||
assertEquals(1, customers.size());
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::envers-tracking-properties-changes-queries-at-revision-example[]
|
||||
Customer customer = (Customer) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesModifiedAtRevision( Customer.class, 2 )
|
||||
.add( AuditEntity.id().eq( 1L ) )
|
||||
.add( AuditEntity.property( "lastName" ).hasChanged() )
|
||||
.add( AuditEntity.property( "firstName" ).hasNotChanged() )
|
||||
.forEntitiesModifiedAtRevision(Customer.class, 2)
|
||||
.add(AuditEntity.id().eq(1L))
|
||||
.add(AuditEntity.property("lastName").hasChanged())
|
||||
.add(AuditEntity.property("firstName").hasNotChanged())
|
||||
.getSingleResult();
|
||||
//end::envers-tracking-properties-changes-queries-at-revision-example[]
|
||||
|
||||
assertNotNull( customer );
|
||||
} );
|
||||
assertNotNull(customer);
|
||||
});
|
||||
}
|
||||
|
||||
//tag::envers-tracking-properties-changes-queries-entity-example[]
|
||||
@Audited( withModifiedFlag = true )
|
||||
@Audited(withModifiedFlag = true)
|
||||
//end::envers-tracking-properties-changes-queries-entity-example[]
|
||||
@Entity(name = "Customer")
|
||||
public static class Customer {
|
||||
|
@ -140,7 +140,7 @@ public class QueryAuditWithModifiedFlagTest extends BaseEntityManagerFunctionalT
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
|
|
@ -55,82 +55,82 @@ public class ValidityStrategyAuditTest extends BaseEntityManagerFunctionalTestCa
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = new Customer();
|
||||
customer.setId( 1L );
|
||||
customer.setFirstName( "John" );
|
||||
customer.setLastName( "Doe" );
|
||||
customer.setId(1L);
|
||||
customer.setFirstName("John");
|
||||
customer.setLastName("Doe");
|
||||
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.find( Customer.class, 1L );
|
||||
customer.setLastName( "Doe Jr." );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.find(Customer.class, 1L);
|
||||
customer.setLastName("Doe Jr.");
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.getReference( Customer.class, 1L );
|
||||
entityManager.remove( customer );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = entityManager.getReference(Customer.class, 1L);
|
||||
entityManager.remove(customer);
|
||||
});
|
||||
|
||||
List<Number> revisions = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
return AuditReaderFactory.get( entityManager ).getRevisions(
|
||||
List<Number> revisions = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
return AuditReaderFactory.get(entityManager).getRevisions(
|
||||
Customer.class,
|
||||
1L
|
||||
);
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = (Customer) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, revisions.get( 0 ) )
|
||||
.forEntitiesAtRevision(Customer.class, revisions.get(0))
|
||||
.getSingleResult();
|
||||
|
||||
assertEquals("Doe", customer.getLastName());
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = (Customer) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, revisions.get( 1 ) )
|
||||
.forEntitiesAtRevision(Customer.class, revisions.get(1))
|
||||
.getSingleResult();
|
||||
|
||||
assertEquals("Doe Jr.", customer.getLastName());
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
try {
|
||||
Customer customer = (Customer) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision( Customer.class, revisions.get( 2 ) )
|
||||
.forEntitiesAtRevision(Customer.class, revisions.get(2))
|
||||
.getSingleResult();
|
||||
|
||||
fail("The Customer was deleted at this revision: " + revisions.get( 2 ));
|
||||
fail("The Customer was deleted at this revision: " + revisions.get(2));
|
||||
}
|
||||
catch (NoResultException expected) {
|
||||
}
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Customer customer = (Customer) AuditReaderFactory
|
||||
.get( entityManager )
|
||||
.get(entityManager)
|
||||
.createQuery()
|
||||
.forEntitiesAtRevision(
|
||||
Customer.class,
|
||||
Customer.class.getName(),
|
||||
revisions.get( 2 ),
|
||||
true )
|
||||
revisions.get(2),
|
||||
true)
|
||||
.getSingleResult();
|
||||
|
||||
assertEquals( Long.valueOf( 1L ), customer.getId() );
|
||||
assertNull( customer.getFirstName() );
|
||||
assertNull( customer.getLastName() );
|
||||
assertNull( customer.getCreatedOn() );
|
||||
} );
|
||||
assertEquals(Long.valueOf(1L), customer.getId());
|
||||
assertNull(customer.getFirstName());
|
||||
assertNull(customer.getLastName());
|
||||
assertNull(customer.getCreatedOn());
|
||||
});
|
||||
}
|
||||
|
||||
@Audited
|
||||
|
@ -144,7 +144,7 @@ public class ValidityStrategyAuditTest extends BaseEntityManagerFunctionalTestCa
|
|||
|
||||
private String lastName;
|
||||
|
||||
@Temporal( TemporalType.TIMESTAMP )
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@Column(name = "created_on")
|
||||
@CreationTimestamp
|
||||
private Date createdOn;
|
||||
|
|
|
@ -11,23 +11,23 @@ import java.time.ZoneOffset;
|
|||
public class DefaultEntityListener {
|
||||
|
||||
public void onPersist(Object entity) {
|
||||
if ( entity instanceof BaseEntity ) {
|
||||
if (entity instanceof BaseEntity) {
|
||||
BaseEntity baseEntity = (BaseEntity) entity;
|
||||
baseEntity.setCreatedOn( now() );
|
||||
baseEntity.setCreatedOn(now());
|
||||
}
|
||||
}
|
||||
|
||||
public void onUpdate(Object entity) {
|
||||
if ( entity instanceof BaseEntity ) {
|
||||
if (entity instanceof BaseEntity) {
|
||||
BaseEntity baseEntity = (BaseEntity) entity;
|
||||
baseEntity.setUpdatedOn( now() );
|
||||
baseEntity.setUpdatedOn(now());
|
||||
}
|
||||
}
|
||||
|
||||
private Timestamp now() {
|
||||
return Timestamp.from(
|
||||
LocalDateTime.now().toInstant( ZoneOffset.UTC )
|
||||
);
|
||||
LocalDateTime.now().toInstant(ZoneOffset.UTC)
|
||||
);
|
||||
}
|
||||
}
|
||||
//end::events-default-listener-mapping-example[]
|
||||
|
|
|
@ -40,49 +40,49 @@ public class DefaultEntityListenerTest extends BaseEntityManagerFunctionalTestCa
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::events-default-listener-persist-example[]
|
||||
Person author = new Person();
|
||||
author.setId( 1L );
|
||||
author.setName( "Vlad Mihalcea" );
|
||||
author.setId(1L);
|
||||
author.setName("Vlad Mihalcea");
|
||||
|
||||
entityManager.persist( author );
|
||||
entityManager.persist(author);
|
||||
|
||||
Book book = new Book();
|
||||
book.setId( 1L );
|
||||
book.setTitle( "High-Performance Java Persistence" );
|
||||
book.setAuthor( author );
|
||||
book.setId(1L);
|
||||
book.setTitle("High-Performance Java Persistence");
|
||||
book.setAuthor(author);
|
||||
|
||||
entityManager.persist( book );
|
||||
entityManager.persist(book);
|
||||
//end::events-default-listener-persist-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::events-default-listener-update-example[]
|
||||
Person author = entityManager.find( Person.class, 1L );
|
||||
author.setName( "Vlad-Alexandru Mihalcea" );
|
||||
Person author = entityManager.find(Person.class, 1L);
|
||||
author.setName("Vlad-Alexandru Mihalcea");
|
||||
|
||||
Book book = entityManager.find( Book.class, 1L );
|
||||
book.setTitle( "High-Performance Java Persistence 2nd Edition" );
|
||||
Book book = entityManager.find(Book.class, 1L);
|
||||
book.setTitle("High-Performance Java Persistence 2nd Edition");
|
||||
//end::events-default-listener-update-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclude() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::events-exclude-default-listener-persist-example[]
|
||||
Publisher publisher = new Publisher();
|
||||
publisher.setId( 1L );
|
||||
publisher.setName( "Amazon" );
|
||||
publisher.setId(1L);
|
||||
publisher.setName("Amazon");
|
||||
|
||||
entityManager.persist( publisher );
|
||||
entityManager.persist(publisher);
|
||||
//end::events-exclude-default-listener-persist-example[]
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Publisher publisher = entityManager.find( Publisher.class, 1L );
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Publisher publisher = entityManager.find(Publisher.class, 1L);
|
||||
assertNull(publisher.getCreatedOn());
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::events-default-listener-mapping-example[]
|
||||
|
|
|
@ -33,7 +33,7 @@ import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
|||
*/
|
||||
public class InterceptorTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger( InterceptorTest.class );
|
||||
private static final Logger LOGGER = Logger.getLogger(InterceptorTest.class);
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
|
@ -44,11 +44,11 @@ public class InterceptorTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Before
|
||||
public void init() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist( new Customer( "John Doe" ) );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.persist(new Customer("John Doe"));
|
||||
Customer customer = new Customer();
|
||||
entityManager.persist( customer );
|
||||
} );
|
||||
entityManager.persist(customer);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -56,15 +56,15 @@ public class InterceptorTest extends BaseEntityManagerFunctionalTestCase {
|
|||
EntityManagerFactory entityManagerFactory = entityManagerFactory();
|
||||
Serializable customerId = 1L;
|
||||
//tag::events-interceptors-session-scope-example[]
|
||||
SessionFactory sessionFactory = entityManagerFactory.unwrap( SessionFactory.class );
|
||||
SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
|
||||
Session session = sessionFactory
|
||||
.withOptions()
|
||||
.interceptor(new LoggingInterceptor() )
|
||||
.interceptor(new LoggingInterceptor())
|
||||
.openSession();
|
||||
session.getTransaction().begin();
|
||||
|
||||
Customer customer = session.get( Customer.class, customerId );
|
||||
customer.setName( "Mr. John Doe" );
|
||||
Customer customer = session.get(Customer.class, customerId);
|
||||
customer.setName("Mr. John Doe");
|
||||
//Entity Customer#1 changed from [John Doe, 0] to [Mr. John Doe, 0]
|
||||
|
||||
session.getTransaction().commit();
|
||||
|
@ -77,19 +77,19 @@ public class InterceptorTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
Serializable customerId = 1L;
|
||||
//tag::events-interceptors-session-factory-scope-example[]
|
||||
SessionFactory sessionFactory = new MetadataSources( new StandardServiceRegistryBuilder().build() )
|
||||
.addAnnotatedClass( Customer.class )
|
||||
SessionFactory sessionFactory = new MetadataSources(new StandardServiceRegistryBuilder().build())
|
||||
.addAnnotatedClass(Customer.class)
|
||||
.getMetadataBuilder()
|
||||
.build()
|
||||
.getSessionFactoryBuilder()
|
||||
.applyInterceptor( new LoggingInterceptor() )
|
||||
.applyInterceptor(new LoggingInterceptor())
|
||||
.build();
|
||||
//end::events-interceptors-session-factory-scope-example[]
|
||||
Session session = sessionFactory.openSession();
|
||||
session.getTransaction().begin();
|
||||
|
||||
Customer customer = session.get( Customer.class, customerId );
|
||||
customer.setName( "Mr. John Doe" );
|
||||
Customer customer = session.get(Customer.class, customerId);
|
||||
customer.setName("Mr. John Doe");
|
||||
//Entity Customer#1 changed from [John Doe, 0] to [Mr. John Doe, 0]
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
@ -135,13 +135,13 @@ public class InterceptorTest extends BaseEntityManagerFunctionalTestCase {
|
|||
Object[] previousState,
|
||||
String[] propertyNames,
|
||||
Type[] types) {
|
||||
LOGGER.debugv( "Entity {0}#{1} changed from {2} to {3}",
|
||||
LOGGER.debugv("Entity {0}#{1} changed from {2} to {3}",
|
||||
entity.getClass().getSimpleName(),
|
||||
id,
|
||||
Arrays.toString( previousState ),
|
||||
Arrays.toString( currentState )
|
||||
Arrays.toString(previousState),
|
||||
Arrays.toString(currentState)
|
||||
);
|
||||
return super.onFlushDirty( entity, id, currentState,
|
||||
return super.onFlushDirty(entity, id, currentState,
|
||||
previousState, propertyNames, types
|
||||
);
|
||||
}
|
||||
|
|
|
@ -36,25 +36,25 @@ public class BatchFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
for ( long i = 0; i < 10; i++ ) {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
for (long i = 0; i < 10; i++) {
|
||||
Department department = new Department();
|
||||
department.id = i;
|
||||
entityManager.persist( department );
|
||||
entityManager.persist(department);
|
||||
|
||||
for ( int j = 0; j < Math.random() * 5; j++ ) {
|
||||
for (int j = 0; j < Math.random() * 5; j++) {
|
||||
Employee employee = new Employee();
|
||||
employee.id = (i * 5) + j;
|
||||
employee.name = String.format( "John %d", employee.getId() );
|
||||
employee.name = String.format("John %d", employee.getId());
|
||||
employee.department = department;
|
||||
entityManager.persist( employee );
|
||||
department.employees.add( employee );
|
||||
entityManager.persist(employee);
|
||||
department.employees.add(employee);
|
||||
}
|
||||
entityManager.flush();
|
||||
}
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::fetching-batch-fetching-example[]
|
||||
List<Department> departments = entityManager.createQuery(
|
||||
"select d " +
|
||||
|
@ -63,7 +63,7 @@ public class BatchFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
"where e.name like 'John%'", Department.class)
|
||||
.getResultList();
|
||||
|
||||
for ( Department department : departments ) {
|
||||
for (Department department : departments) {
|
||||
log.infof(
|
||||
"Department %d has {} employees",
|
||||
department.getId(),
|
||||
|
@ -71,7 +71,7 @@ public class BatchFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
);
|
||||
}
|
||||
//end::fetching-batch-fetching-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::fetching-batch-mapping-example[]
|
||||
|
|
|
@ -36,35 +36,35 @@ public class DirectVsQueryFetchingTest extends BaseEntityManagerFunctionalTestCa
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Department department = new Department();
|
||||
department.id = 1L;
|
||||
entityManager.persist( department );
|
||||
entityManager.persist(department);
|
||||
|
||||
Employee employee1 = new Employee();
|
||||
employee1.id = 1L;
|
||||
employee1.username = "user1";
|
||||
employee1.department = department;
|
||||
entityManager.persist( employee1 );
|
||||
entityManager.persist(employee1);
|
||||
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::fetching-direct-vs-query-direct-fetching-example[]
|
||||
Employee employee = entityManager.find( Employee.class, 1L );
|
||||
Employee employee = entityManager.find(Employee.class, 1L);
|
||||
//end::fetching-direct-vs-query-direct-fetching-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::fetching-direct-vs-query-entity-query-example[]
|
||||
Employee employee = entityManager.createQuery(
|
||||
"select e " +
|
||||
"from Employee e " +
|
||||
"where e.id = :id", Employee.class)
|
||||
.setParameter( "id", 1L )
|
||||
.setParameter("id", 1L)
|
||||
.getSingleResult();
|
||||
//end::fetching-direct-vs-query-entity-query-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::fetching-direct-vs-query-domain-model-example[]
|
||||
|
|
|
@ -40,30 +40,30 @@ public class FetchModeJoinTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
for ( long i = 0; i < 2; i++ ) {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
for (long i = 0; i < 2; i++) {
|
||||
Department department = new Department();
|
||||
department.id = i + 1;
|
||||
entityManager.persist( department );
|
||||
entityManager.persist(department);
|
||||
|
||||
for ( long j = 0; j < 3; j++ ) {
|
||||
for (long j = 0; j < 3; j++) {
|
||||
Employee employee1 = new Employee();
|
||||
employee1.username = String.format( "user %d_%d", i, j );
|
||||
employee1.username = String.format("user %d_%d", i, j);
|
||||
employee1.department = department;
|
||||
entityManager.persist( employee1 );
|
||||
entityManager.persist(employee1);
|
||||
}
|
||||
}
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::fetching-strategies-fetch-mode-join-example[]
|
||||
Department department = entityManager.find( Department.class, 1L );
|
||||
Department department = entityManager.find(Department.class, 1L);
|
||||
|
||||
log.infof( "Fetched department: %s", department.getId());
|
||||
log.infof("Fetched department: %s", department.getId());
|
||||
|
||||
assertEquals( 3, department.getEmployees().size() );
|
||||
assertEquals(3, department.getEmployees().size());
|
||||
//end::fetching-strategies-fetch-mode-join-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Department")
|
||||
|
|
|
@ -40,34 +40,34 @@ public class FetchModeSelectTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
for ( long i = 0; i < 2; i++ ) {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
for (long i = 0; i < 2; i++) {
|
||||
Department department = new Department();
|
||||
department.id = i + 1;
|
||||
entityManager.persist( department );
|
||||
entityManager.persist(department);
|
||||
|
||||
for ( long j = 0; j < 3; j++ ) {
|
||||
for (long j = 0; j < 3; j++) {
|
||||
Employee employee1 = new Employee();
|
||||
employee1.username = String.format( "user %d_%d", i, j );
|
||||
employee1.username = String.format("user %d_%d", i, j);
|
||||
employee1.department = department;
|
||||
entityManager.persist( employee1 );
|
||||
entityManager.persist(employee1);
|
||||
}
|
||||
}
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::fetching-strategies-fetch-mode-select-example[]
|
||||
List<Department> departments = entityManager.createQuery(
|
||||
"select d from Department d", Department.class )
|
||||
"select d from Department d", Department.class)
|
||||
.getResultList();
|
||||
|
||||
log.infof( "Fetched %d Departments", departments.size());
|
||||
log.infof("Fetched %d Departments", departments.size());
|
||||
|
||||
for (Department department : departments ) {
|
||||
assertEquals( 3, department.getEmployees().size() );
|
||||
for (Department department : departments) {
|
||||
assertEquals(3, department.getEmployees().size());
|
||||
}
|
||||
//end::fetching-strategies-fetch-mode-select-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::fetching-strategies-fetch-mode-select-mapping-example[]
|
||||
|
|
|
@ -40,38 +40,38 @@ public class FetchModeSubselectTest extends BaseEntityManagerFunctionalTestCase
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
for ( long i = 0; i < 2; i++ ) {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
for (long i = 0; i < 2; i++) {
|
||||
Department department = new Department();
|
||||
department.id = i + 1;
|
||||
department.name = String.format( "Department %d", department.id );
|
||||
entityManager.persist( department );
|
||||
department.name = String.format("Department %d", department.id);
|
||||
entityManager.persist(department);
|
||||
|
||||
for ( long j = 0; j < 3; j++ ) {
|
||||
for (long j = 0; j < 3; j++) {
|
||||
Employee employee1 = new Employee();
|
||||
employee1.username = String.format( "user %d_%d", i, j );
|
||||
employee1.username = String.format("user %d_%d", i, j);
|
||||
employee1.department = department;
|
||||
entityManager.persist( employee1 );
|
||||
entityManager.persist(employee1);
|
||||
}
|
||||
}
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::fetching-strategies-fetch-mode-subselect-example[]
|
||||
List<Department> departments = entityManager.createQuery(
|
||||
"select d " +
|
||||
"from Department d " +
|
||||
"where d.name like :token", Department.class )
|
||||
.setParameter( "token", "Department%" )
|
||||
"where d.name like :token", Department.class)
|
||||
.setParameter("token", "Department%")
|
||||
.getResultList();
|
||||
|
||||
log.infof( "Fetched %d Departments", departments.size());
|
||||
log.infof("Fetched %d Departments", departments.size());
|
||||
|
||||
for (Department department : departments ) {
|
||||
assertEquals( 3, department.getEmployees().size() );
|
||||
for (Department department : departments) {
|
||||
assertEquals(3, department.getEmployees().size());
|
||||
}
|
||||
//end::fetching-strategies-fetch-mode-subselect-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Department")
|
||||
|
|
|
@ -49,10 +49,10 @@ public class FetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Department department = new Department();
|
||||
department.id = 1L;
|
||||
entityManager.persist( department );
|
||||
entityManager.persist(department);
|
||||
|
||||
Employee employee1 = new Employee();
|
||||
employee1.id = 1L;
|
||||
|
@ -60,7 +60,7 @@ public class FetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
employee1.password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
employee1.accessLevel = 0;
|
||||
employee1.department = department;
|
||||
entityManager.persist( employee1 );
|
||||
entityManager.persist(employee1);
|
||||
|
||||
Employee employee2 = new Employee();
|
||||
employee2.id = 2L;
|
||||
|
@ -68,11 +68,11 @@ public class FetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
employee2.password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
employee2.accessLevel = 1;
|
||||
employee2.department = department;
|
||||
entityManager.persist( employee2 );
|
||||
entityManager.persist(employee2);
|
||||
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String username = "user1";
|
||||
String password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
//tag::fetching-strategies-no-fetching-example[]
|
||||
|
@ -83,14 +83,14 @@ public class FetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
" e.username = :username and " +
|
||||
" e.password = :password",
|
||||
Employee.class)
|
||||
.setParameter( "username", username)
|
||||
.setParameter( "password", password)
|
||||
.setParameter("username", username)
|
||||
.setParameter("password", password)
|
||||
.getSingleResult();
|
||||
//end::fetching-strategies-no-fetching-example[]
|
||||
assertNotNull(employee);
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String username = "user1";
|
||||
String password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
//tag::fetching-strategies-no-fetching-scalar-example[]
|
||||
|
@ -101,14 +101,14 @@ public class FetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
" e.username = :username and " +
|
||||
" e.password = :password",
|
||||
Integer.class)
|
||||
.setParameter( "username", username)
|
||||
.setParameter( "password", password)
|
||||
.setParameter("username", username)
|
||||
.setParameter("password", password)
|
||||
.getSingleResult();
|
||||
//end::fetching-strategies-no-fetching-scalar-example[]
|
||||
assertEquals( Integer.valueOf(0), accessLevel);
|
||||
} );
|
||||
assertEquals(Integer.valueOf(0), accessLevel);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String username = "user1";
|
||||
String password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
//tag::fetching-strategies-dynamic-fetching-jpql-example[]
|
||||
|
@ -120,32 +120,32 @@ public class FetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
" e.username = :username and " +
|
||||
" e.password = :password",
|
||||
Employee.class)
|
||||
.setParameter( "username", username)
|
||||
.setParameter( "password", password)
|
||||
.setParameter("username", username)
|
||||
.setParameter("password", password)
|
||||
.getSingleResult();
|
||||
//end::fetching-strategies-dynamic-fetching-jpql-example[]
|
||||
assertNotNull(employee);
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String username = "user1";
|
||||
String password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
//tag::fetching-strategies-dynamic-fetching-criteria-example[]
|
||||
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Employee> query = builder.createQuery( Employee.class );
|
||||
Root<Employee> root = query.from( Employee.class );
|
||||
root.fetch( "projects", JoinType.LEFT);
|
||||
CriteriaQuery<Employee> query = builder.createQuery(Employee.class);
|
||||
Root<Employee> root = query.from(Employee.class);
|
||||
root.fetch("projects", JoinType.LEFT);
|
||||
query.select(root).where(
|
||||
builder.and(
|
||||
builder.equal(root.get("username"), username),
|
||||
builder.equal(root.get("password"), password)
|
||||
)
|
||||
);
|
||||
Employee employee = entityManager.createQuery( query ).getSingleResult();
|
||||
Employee employee = entityManager.createQuery(query).getSingleResult();
|
||||
//end::fetching-strategies-dynamic-fetching-criteria-example[]
|
||||
assertNotNull(employee);
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::fetching-strategies-domain-model-example[]
|
||||
|
@ -173,7 +173,7 @@ public class FetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Column(name = "pswd")
|
||||
@ColumnTransformer(
|
||||
read = "decrypt( 'AES', '00', pswd )",
|
||||
read = "decrypt('AES', '00', pswd )",
|
||||
write = "encrypt('AES', '00', ?)"
|
||||
)
|
||||
private String password;
|
||||
|
|
|
@ -51,10 +51,10 @@ public class GraphFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Department department = new Department();
|
||||
department.id = 1L;
|
||||
entityManager.persist( department );
|
||||
entityManager.persist(department);
|
||||
|
||||
Employee employee1 = new Employee();
|
||||
employee1.id = 1L;
|
||||
|
@ -62,7 +62,7 @@ public class GraphFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
employee1.password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
employee1.accessLevel = 0;
|
||||
employee1.department = department;
|
||||
entityManager.persist( employee1 );
|
||||
entityManager.persist(employee1);
|
||||
|
||||
Employee employee2 = new Employee();
|
||||
employee2.id = 2L;
|
||||
|
@ -70,15 +70,15 @@ public class GraphFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
employee2.password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
employee2.accessLevel = 1;
|
||||
employee2.department = department;
|
||||
entityManager.persist( employee2 );
|
||||
entityManager.persist(employee2);
|
||||
|
||||
Project project = new Project();
|
||||
project.id = 1L;
|
||||
project.employees.add( employee1 );
|
||||
entityManager.persist( project );
|
||||
} );
|
||||
project.employees.add(employee1);
|
||||
entityManager.persist(project);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Long userId = 1L;
|
||||
|
||||
//tag::fetching-strategies-dynamic-fetching-entity-graph-example[]
|
||||
|
@ -87,27 +87,27 @@ public class GraphFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
userId,
|
||||
Collections.singletonMap(
|
||||
"jakarta.persistence.fetchgraph",
|
||||
entityManager.getEntityGraph( "employee.projects" )
|
||||
entityManager.getEntityGraph("employee.projects")
|
||||
)
|
||||
);
|
||||
//end::fetching-strategies-dynamic-fetching-entity-graph-example[]
|
||||
assertNotNull(employee);
|
||||
} );
|
||||
});
|
||||
|
||||
//tag::fetching-strategies-dynamic-fetching-entity-subgraph-example[]
|
||||
Project project = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Project project = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
return entityManager.find(
|
||||
Project.class,
|
||||
1L,
|
||||
Collections.singletonMap(
|
||||
"jakarta.persistence.fetchgraph",
|
||||
entityManager.getEntityGraph( "project.employees" )
|
||||
entityManager.getEntityGraph("project.employees")
|
||||
)
|
||||
);
|
||||
} );
|
||||
});
|
||||
//end::fetching-strategies-dynamic-fetching-entity-subgraph-example[]
|
||||
assertEquals(1, project.getEmployees().size());
|
||||
assertEquals( Long.valueOf( 1L ), project.getEmployees().get( 0 ).getDepartment().getId() );
|
||||
assertEquals(Long.valueOf(1L), project.getEmployees().get(0).getDepartment().getId());
|
||||
}
|
||||
|
||||
@Entity(name = "Department")
|
||||
|
@ -154,7 +154,7 @@ public class GraphFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Column(name = "pswd")
|
||||
@ColumnTransformer(
|
||||
read = "decrypt( 'AES', '00', pswd )",
|
||||
read = "decrypt('AES', '00', pswd )",
|
||||
write = "encrypt('AES', '00', ?)"
|
||||
)
|
||||
private String password;
|
||||
|
@ -227,7 +227,7 @@ public class GraphFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
),
|
||||
subgraphs = @NamedSubgraph(
|
||||
name = "project.employees.department",
|
||||
attributeNodes = @NamedAttributeNode( "department" )
|
||||
attributeNodes = @NamedAttributeNode("department")
|
||||
)
|
||||
)
|
||||
public static class Project {
|
||||
|
|
|
@ -53,12 +53,12 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
//tag::fetching-strategies-dynamic-fetching-entity-graph-parsing-example-1[]
|
||||
final EntityGraph<Project> graph = GraphParser.parse(
|
||||
Project.class,
|
||||
"employees( department )",
|
||||
"employees(department)",
|
||||
entityManager
|
||||
);
|
||||
//end::fetching-strategies-dynamic-fetching-entity-graph-parsing-example-1[]
|
||||
|
||||
Assert.assertNotNull( graph );
|
||||
Assert.assertNotNull(graph);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -67,12 +67,12 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
//tag::fetching-strategies-dynamic-fetching-entity-graph-parsing-example-2[]
|
||||
final EntityGraph<Project> graph = GraphParser.parse(
|
||||
Project.class,
|
||||
"employees( username, password, accessLevel, department( employees( username ) ) )",
|
||||
"employees(username, password, accessLevel, department(employees(username)))",
|
||||
entityManager
|
||||
);
|
||||
//end::fetching-strategies-dynamic-fetching-entity-graph-parsing-example-2[]
|
||||
|
||||
Assert.assertNotNull( graph );
|
||||
Assert.assertNotNull(graph);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -81,12 +81,12 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
//tag::fetching-strategies-dynamic-fetching-entity-graph-parsing-key-example-1[]
|
||||
final EntityGraph<Movie> graph = GraphParser.parse(
|
||||
Movie.class,
|
||||
"cast.key( name )",
|
||||
"cast.key(name)",
|
||||
entityManager
|
||||
);
|
||||
//end::fetching-strategies-dynamic-fetching-entity-graph-parsing-key-example-1[]
|
||||
|
||||
Assert.assertNotNull( graph );
|
||||
Assert.assertNotNull(graph);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -95,12 +95,12 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
//tag::fetching-strategies-dynamic-fetching-entity-graph-parsing-key-example-2[]
|
||||
final EntityGraph<Ticket> graph = GraphParser.parse(
|
||||
Ticket.class,
|
||||
"showing.key( movie( cast ) )",
|
||||
"showing.key(movie(cast))",
|
||||
entityManager
|
||||
);
|
||||
//end::fetching-strategies-dynamic-fetching-entity-graph-parsing-key-example-2[]
|
||||
|
||||
Assert.assertNotNull( graph );
|
||||
Assert.assertNotNull(graph);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -108,32 +108,32 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
EntityManager entityManager = getOrCreateEntityManager();
|
||||
//tag::fetching-strategies-dynamic-fetching-entity-graph-merging-example[]
|
||||
final EntityGraph<Project> a = GraphParser.parse(
|
||||
Project.class, "employees( username )", entityManager
|
||||
Project.class, "employees(username)", entityManager
|
||||
);
|
||||
|
||||
final EntityGraph<Project> b = GraphParser.parse(
|
||||
Project.class, "employees( password, accessLevel )", entityManager
|
||||
Project.class, "employees(password, accessLevel)", entityManager
|
||||
);
|
||||
|
||||
final EntityGraph<Project> c = GraphParser.parse(
|
||||
Project.class, "employees( department( employees( username ) ) )", entityManager
|
||||
Project.class, "employees(department(employees(username)))", entityManager
|
||||
);
|
||||
|
||||
final EntityGraph<Project> all = EntityGraphs.merge( entityManager, Project.class, a, b, c );
|
||||
final EntityGraph<Project> all = EntityGraphs.merge(entityManager, Project.class, a, b, c);
|
||||
//end::fetching-strategies-dynamic-fetching-entity-graph-merging-example[]
|
||||
|
||||
final EntityGraph<Project> expected = GraphParser.parse(
|
||||
Project.class,
|
||||
"employees( username, password, accessLevel, department( employees( username ) ) )",
|
||||
"employees(username, password, accessLevel, department(employees(username)))",
|
||||
entityManager
|
||||
);
|
||||
|
||||
Assert.assertTrue( EntityGraphs.areEqual( expected, all ) );
|
||||
Assert.assertTrue(EntityGraphs.areEqual(expected, all));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindExample() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Long userId = 1L;
|
||||
|
||||
//tag::fetching-strategies-dynamic-fetching-entity-graph-apply-example-find[]
|
||||
|
@ -142,16 +142,16 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
userId,
|
||||
Collections.singletonMap(
|
||||
GraphSemantic.FETCH.getJpaHintName(),
|
||||
GraphParser.parse( Employee.class, "username, accessLevel, department", entityManager )
|
||||
GraphParser.parse(Employee.class, "username, accessLevel, department", entityManager)
|
||||
)
|
||||
);
|
||||
//end::fetching-strategies-dynamic-fetching-entity-graph-apply-example-find[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryExample() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::fetching-strategies-dynamic-fetching-entity-graph-apply-example-query[]
|
||||
final String graphString = "username, accessLevel";
|
||||
final String queryString = "select e from Employee e where e.id = 1";
|
||||
|
@ -162,13 +162,13 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
entityManager
|
||||
);
|
||||
|
||||
TypedQuery<Employee> query1 = entityManager.createQuery( queryString, Employee.class );
|
||||
TypedQuery<Employee> query1 = entityManager.createQuery(queryString, Employee.class);
|
||||
query1.setHint(
|
||||
GraphSemantic.FETCH.getJpaHintName(),
|
||||
graph
|
||||
);
|
||||
//end::fetching-strategies-dynamic-fetching-entity-graph-apply-example-query[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
@ -178,7 +178,7 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
// NOTE : for the moment I do not add named (sub)graph annotations because
|
||||
// this is only used for discussing graph parsing
|
||||
|
||||
@Entity( name = "Person" )
|
||||
@Entity(name = "Person")
|
||||
public static class Person {
|
||||
@Id
|
||||
private Integer id;
|
||||
|
@ -186,7 +186,7 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
String name;
|
||||
}
|
||||
|
||||
@Entity( name = "Movie" )
|
||||
@Entity(name = "Movie")
|
||||
public static class Movie {
|
||||
@Id
|
||||
private Integer id;
|
||||
|
@ -198,7 +198,7 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
Map<Person,String> cast;
|
||||
}
|
||||
|
||||
@Entity( name = "Theater" )
|
||||
@Entity(name = "Theater")
|
||||
public static class Theater {
|
||||
@Id
|
||||
private Integer id;
|
||||
|
@ -207,7 +207,7 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
boolean foodService;
|
||||
}
|
||||
|
||||
@Entity( name = "Showing" )
|
||||
@Entity(name = "Showing")
|
||||
public static class Showing {
|
||||
@Embeddable
|
||||
public static class Id implements Serializable {
|
||||
|
@ -227,7 +227,7 @@ public class GraphParsingTest extends AbstractEntityGraphTest {
|
|||
private LocalDateTime endTime;
|
||||
}
|
||||
|
||||
@Entity( name = "Ticket" )
|
||||
@Entity(name = "Ticket")
|
||||
public static class Ticket {
|
||||
@Id
|
||||
private Integer id;
|
||||
|
|
|
@ -41,32 +41,32 @@ public class LazyCollectionTest extends BaseEntityManagerFunctionalTestCase {
|
|||
@Test
|
||||
public void test() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::fetching-LazyCollection-persist-example[]
|
||||
Department department = new Department();
|
||||
department.setId( 1L );
|
||||
entityManager.persist( department );
|
||||
department.setId(1L);
|
||||
entityManager.persist(department);
|
||||
|
||||
for (long i = 1; i <= 3; i++ ) {
|
||||
for (long i = 1; i <= 3; i++) {
|
||||
Employee employee = new Employee();
|
||||
employee.setId( i );
|
||||
employee.setUsername( String.format( "user_%d", i ) );
|
||||
employee.setId(i);
|
||||
employee.setUsername(String.format("user_%d", i));
|
||||
department.addEmployee(employee);
|
||||
}
|
||||
//end::fetching-LazyCollection-persist-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::fetching-LazyCollection-select-example[]
|
||||
Department department = entityManager.find(Department.class, 1L);
|
||||
|
||||
int employeeCount = department.getEmployees().size();
|
||||
|
||||
for(int i = 0; i < employeeCount; i++ ) {
|
||||
log.infof( "Fetched employee: %s", department.getEmployees().get( i ).getUsername());
|
||||
for(int i = 0; i < employeeCount; i++) {
|
||||
log.infof("Fetched employee: %s", department.getEmployees().get(i).getUsername());
|
||||
}
|
||||
//end::fetching-LazyCollection-select-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::fetching-LazyCollection-domain-model-example[]
|
||||
|
@ -78,7 +78,7 @@ public class LazyCollectionTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
|
||||
@OrderColumn(name = "order_id")
|
||||
@LazyCollection( LazyCollectionOption.EXTRA )
|
||||
@LazyCollection(LazyCollectionOption.EXTRA)
|
||||
private List<Employee> employees = new ArrayList<>();
|
||||
|
||||
//Getters and setters omitted for brevity
|
||||
|
@ -102,8 +102,8 @@ public class LazyCollectionTest extends BaseEntityManagerFunctionalTestCase {
|
|||
}
|
||||
|
||||
public void addEmployee(Employee employee) {
|
||||
this.employees.add( employee );
|
||||
employee.setDepartment( this );
|
||||
this.employees.add(employee);
|
||||
employee.setDepartment(this);
|
||||
}
|
||||
//tag::fetching-LazyCollection-domain-model-example[]
|
||||
}
|
||||
|
|
|
@ -47,10 +47,10 @@ public class ProfileFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Department department = new Department();
|
||||
department.id = 1L;
|
||||
entityManager.persist( department );
|
||||
entityManager.persist(department);
|
||||
|
||||
Employee employee1 = new Employee();
|
||||
employee1.id = 1L;
|
||||
|
@ -58,7 +58,7 @@ public class ProfileFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
employee1.password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
employee1.accessLevel = 0;
|
||||
employee1.department = department;
|
||||
entityManager.persist( employee1 );
|
||||
entityManager.persist(employee1);
|
||||
|
||||
Employee employee2 = new Employee();
|
||||
employee2.id = 2L;
|
||||
|
@ -66,22 +66,22 @@ public class ProfileFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
employee2.password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
employee2.accessLevel = 1;
|
||||
employee2.department = department;
|
||||
entityManager.persist( employee2 );
|
||||
entityManager.persist(employee2);
|
||||
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
String username = "user1";
|
||||
String password = "3fabb4de8f1ee2e97d7793bab2db1116";
|
||||
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
|
||||
//tag::fetching-strategies-dynamic-fetching-profile-example[]
|
||||
session.enableFetchProfile( "employee.projects" );
|
||||
Employee employee = session.bySimpleNaturalId( Employee.class ).load( username );
|
||||
session.enableFetchProfile("employee.projects");
|
||||
Employee employee = session.bySimpleNaturalId(Employee.class).load(username);
|
||||
//end::fetching-strategies-dynamic-fetching-profile-example[]
|
||||
assertNotNull(employee);
|
||||
} );
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
@ -120,7 +120,7 @@ public class ProfileFetchingTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Column(name = "pswd")
|
||||
@ColumnTransformer(
|
||||
read = "decrypt( 'AES', '00', pswd )",
|
||||
read = "decrypt('AES', '00', pswd )",
|
||||
write = "encrypt('AES', '00', ?)"
|
||||
)
|
||||
private String password;
|
||||
|
|
|
@ -41,19 +41,19 @@ public class AlwaysFlushTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testFlushSQL() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.createNativeQuery("delete from Person").executeUpdate();
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("testFlushSQL");
|
||||
//tag::flushing-always-flush-sql-example[]
|
||||
Person person = new Person("John Doe");
|
||||
entityManager.persist(person);
|
||||
|
||||
Session session = entityManager.unwrap( Session.class);
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
assertTrue(((Number) session
|
||||
.createNativeQuery("select count(*) from Person")
|
||||
.setHibernateFlushMode( FlushMode.ALWAYS)
|
||||
.setHibernateFlushMode(FlushMode.ALWAYS)
|
||||
.uniqueResult()).intValue() == 1);
|
||||
//end::flushing-always-flush-sql-example[]
|
||||
});
|
||||
|
|
|
@ -44,14 +44,14 @@ public class AutoFlushTest extends BaseEntityManagerFunctionalTestCase {
|
|||
txn = entityManager.getTransaction();
|
||||
txn.begin();
|
||||
|
||||
Person person = new Person( "John Doe" );
|
||||
entityManager.persist( person );
|
||||
log.info( "Entity is in persisted state" );
|
||||
Person person = new Person("John Doe");
|
||||
entityManager.persist(person);
|
||||
log.info("Entity is in persisted state");
|
||||
|
||||
txn.commit();
|
||||
//end::flushing-auto-flush-commit-example[]
|
||||
} catch (RuntimeException e) {
|
||||
if ( txn != null && txn.isActive()) txn.rollback();
|
||||
if (txn != null && txn.isActive()) txn.rollback();
|
||||
throw e;
|
||||
} finally {
|
||||
if (entityManager != null) {
|
||||
|
@ -62,104 +62,104 @@ public class AutoFlushTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testFlushAutoJPQL() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "testFlushAutoJPQL" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("testFlushAutoJPQL");
|
||||
//tag::flushing-auto-flush-jpql-example[]
|
||||
Person person = new Person( "John Doe" );
|
||||
entityManager.persist( person );
|
||||
entityManager.createQuery( "select p from Advertisement p" ).getResultList();
|
||||
entityManager.createQuery( "select p from Person p" ).getResultList();
|
||||
Person person = new Person("John Doe");
|
||||
entityManager.persist(person);
|
||||
entityManager.createQuery("select p from Advertisement p").getResultList();
|
||||
entityManager.createQuery("select p from Person p").getResultList();
|
||||
//end::flushing-auto-flush-jpql-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlushAutoJPQLOverlap() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "testFlushAutoJPQLOverlap" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("testFlushAutoJPQLOverlap");
|
||||
//tag::flushing-auto-flush-jpql-overlap-example[]
|
||||
Person person = new Person( "John Doe" );
|
||||
entityManager.persist( person );
|
||||
entityManager.createQuery( "select p from Person p" ).getResultList();
|
||||
Person person = new Person("John Doe");
|
||||
entityManager.persist(person);
|
||||
entityManager.createQuery("select p from Person p").getResultList();
|
||||
//end::flushing-auto-flush-jpql-overlap-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlushAutoSQL() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.createNativeQuery( "delete from Person" ).executeUpdate();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "testFlushAutoSQL" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.createNativeQuery("delete from Person").executeUpdate();
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("testFlushAutoSQL");
|
||||
//tag::flushing-auto-flush-sql-example[]
|
||||
assertTrue(((Number) entityManager
|
||||
.createNativeQuery( "select count(*) from Person")
|
||||
.getSingleResult()).intValue() == 0 );
|
||||
.createNativeQuery("select count(*) from Person")
|
||||
.getSingleResult()).intValue() == 0);
|
||||
|
||||
Person person = new Person( "John Doe" );
|
||||
entityManager.persist( person );
|
||||
Person person = new Person("John Doe");
|
||||
entityManager.persist(person);
|
||||
|
||||
assertTrue(((Number) entityManager
|
||||
.createNativeQuery( "select count(*) from Person")
|
||||
.getSingleResult()).intValue() == 1 );
|
||||
.createNativeQuery("select count(*) from Person")
|
||||
.getSingleResult()).intValue() == 1);
|
||||
//end::flushing-auto-flush-sql-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlushAutoSQLNativeSession() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.createNativeQuery( "delete from Person" ).executeUpdate();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "testFlushAutoSQLNativeSession" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.createNativeQuery("delete from Person").executeUpdate();
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("testFlushAutoSQLNativeSession");
|
||||
//tag::flushing-auto-flush-sql-native-example[]
|
||||
assertTrue(((Number) entityManager
|
||||
.createNativeQuery( "select count(*) from Person")
|
||||
.getSingleResult()).intValue() == 0 );
|
||||
.createNativeQuery("select count(*) from Person")
|
||||
.getSingleResult()).intValue() == 0);
|
||||
|
||||
Person person = new Person( "John Doe" );
|
||||
entityManager.persist( person );
|
||||
Person person = new Person("John Doe");
|
||||
entityManager.persist(person);
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
|
||||
// For this to work, the Session/EntityManager must be put into COMMIT FlushMode
|
||||
// - this is a change since 5.2 to account for merging EntityManager functionality
|
||||
// directly into Session. Flushing would be the JPA-spec compliant behavior,
|
||||
// so we know do that by default.
|
||||
session.setFlushMode( FlushModeType.COMMIT );
|
||||
session.setFlushMode(FlushModeType.COMMIT);
|
||||
// or using Hibernate's FlushMode enum
|
||||
//session.setHibernateFlushMode( FlushMode.COMMIT );
|
||||
//session.setHibernateFlushMode(FlushMode.COMMIT);
|
||||
|
||||
assertTrue(((Number) session
|
||||
.createNativeQuery( "select count(*) from Person")
|
||||
.uniqueResult()).intValue() == 0 );
|
||||
.createNativeQuery("select count(*) from Person")
|
||||
.uniqueResult()).intValue() == 0);
|
||||
//end::flushing-auto-flush-sql-native-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFlushAutoSQLSynchronization() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.createNativeQuery( "delete from Person" ).executeUpdate();
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
log.info( "testFlushAutoSQLSynchronization" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.createNativeQuery("delete from Person").executeUpdate();
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("testFlushAutoSQLSynchronization");
|
||||
//tag::flushing-auto-flush-sql-synchronization-example[]
|
||||
assertTrue(((Number) entityManager
|
||||
.createNativeQuery( "select count(*) from Person")
|
||||
.getSingleResult()).intValue() == 0 );
|
||||
.createNativeQuery("select count(*) from Person")
|
||||
.getSingleResult()).intValue() == 0);
|
||||
|
||||
Person person = new Person( "John Doe" );
|
||||
entityManager.persist( person );
|
||||
Session session = entityManager.unwrap( Session.class );
|
||||
Person person = new Person("John Doe");
|
||||
entityManager.persist(person);
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
|
||||
assertTrue(((Number) session
|
||||
.createNativeQuery( "select count(*) from Person")
|
||||
.addSynchronizedEntityClass( Person.class )
|
||||
.uniqueResult()).intValue() == 1 );
|
||||
.createNativeQuery("select count(*) from Person")
|
||||
.addSynchronizedEntityClass(Person.class)
|
||||
.uniqueResult()).intValue() == 1);
|
||||
//end::flushing-auto-flush-sql-synchronization-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::flushing-auto-flush-jpql-entity-example[]
|
||||
|
|
|
@ -41,18 +41,18 @@ public class CommitFlushTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testFlushJPQL() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("testFlushJPQL");
|
||||
//tag::flushing-commit-flush-jpql-example[]
|
||||
Person person = new Person("John Doe");
|
||||
entityManager.persist(person);
|
||||
|
||||
entityManager.createQuery("select p from Advertisement p")
|
||||
.setFlushMode( FlushModeType.COMMIT)
|
||||
.setFlushMode(FlushModeType.COMMIT)
|
||||
.getResultList();
|
||||
|
||||
entityManager.createQuery("select p from Person p")
|
||||
.setFlushMode( FlushModeType.COMMIT)
|
||||
.setFlushMode(FlushModeType.COMMIT)
|
||||
.getResultList();
|
||||
//end::flushing-commit-flush-jpql-example[]
|
||||
});
|
||||
|
@ -60,11 +60,11 @@ public class CommitFlushTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testFlushSQL() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.createNativeQuery("delete from Person")
|
||||
.executeUpdate();
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("testFlushSQL");
|
||||
//tag::flushing-commit-flush-sql-example[]
|
||||
Person person = new Person("John Doe");
|
||||
|
|
|
@ -29,24 +29,24 @@ public class FlushOrderTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testOrder() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.createNativeQuery("delete from Person").executeUpdate();
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person person = new Person("John Doe");
|
||||
person.id = 1L;
|
||||
entityManager.persist(person);
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("testFlushSQL");
|
||||
//tag::flushing-order-example[]
|
||||
Person person = entityManager.find( Person.class, 1L);
|
||||
Person person = entityManager.find(Person.class, 1L);
|
||||
entityManager.remove(person);
|
||||
|
||||
Person newPerson = new Person( );
|
||||
newPerson.setId( 2L );
|
||||
newPerson.setName( "John Doe" );
|
||||
entityManager.persist( newPerson );
|
||||
Person newPerson = new Person();
|
||||
newPerson.setId(2L);
|
||||
newPerson.setName("John Doe");
|
||||
entityManager.persist(newPerson);
|
||||
//end::flushing-order-example[]
|
||||
});
|
||||
}
|
||||
|
|
|
@ -35,24 +35,24 @@ public class HibernateAutoFlushTest extends BaseNonConfigCoreFunctionalTestCase
|
|||
|
||||
@Test
|
||||
public void testFlushAutoSQLNativeSession() {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
session.createNativeQuery( "delete from Person" ).executeUpdate();
|
||||
} );
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
log.info( "testFlushAutoSQLNativeSession" );
|
||||
doInHibernate(this::sessionFactory, session -> {
|
||||
session.createNativeQuery("delete from Person").executeUpdate();
|
||||
});
|
||||
doInHibernate(this::sessionFactory, session -> {
|
||||
log.info("testFlushAutoSQLNativeSession");
|
||||
//tag::flushing-auto-flush-sql-native-example[]
|
||||
assertTrue(((Number) session
|
||||
.createNativeQuery( "select count(*) from Person")
|
||||
.getSingleResult()).intValue() == 0 );
|
||||
.createNativeQuery("select count(*) from Person")
|
||||
.getSingleResult()).intValue() == 0);
|
||||
|
||||
Person person = new Person( "John Doe" );
|
||||
session.persist( person );
|
||||
Person person = new Person("John Doe");
|
||||
session.persist(person);
|
||||
|
||||
assertTrue(((Number) session
|
||||
.createNativeQuery( "select count(*) from Person")
|
||||
.uniqueResult()).intValue() == 0 );
|
||||
.createNativeQuery("select count(*) from Person")
|
||||
.uniqueResult()).intValue() == 0);
|
||||
//end::flushing-auto-flush-sql-native-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::flushing-auto-flush-jpql-entity-example[]
|
||||
|
|
|
@ -41,17 +41,17 @@ public class ManualFlushTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testFlushSQL() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.createNativeQuery("delete from Person").executeUpdate();
|
||||
});
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
log.info("testFlushSQL");
|
||||
//tag::flushing-manual-flush-example[]
|
||||
Person person = new Person("John Doe");
|
||||
entityManager.persist(person);
|
||||
|
||||
Session session = entityManager.unwrap( Session.class);
|
||||
session.setHibernateFlushMode( FlushMode.MANUAL );
|
||||
Session session = entityManager.unwrap(Session.class);
|
||||
session.setHibernateFlushMode(FlushMode.MANUAL);
|
||||
|
||||
assertTrue(((Number) entityManager
|
||||
.createQuery("select count(id) from Person")
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -43,7 +43,7 @@ public class PostgreSQLFunctionSelectClauseTest extends BaseEntityManagerFunctio
|
|||
@Override
|
||||
protected void addMappings(Map settings) {
|
||||
//tag::hql-user-defined-functions-register-metadata-builder-example[]
|
||||
settings.put( "hibernate.metadata_builder_contributor",
|
||||
settings.put("hibernate.metadata_builder_contributor",
|
||||
(MetadataBuilderContributor) metadataBuilder ->
|
||||
metadataBuilder.applySqlFunction(
|
||||
"apply_vat",
|
||||
|
@ -58,8 +58,8 @@ public class PostgreSQLFunctionSelectClauseTest extends BaseEntityManagerFunctio
|
|||
|
||||
@Override
|
||||
protected void afterEntityManagerFactoryBuilt() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.unwrap( Session.class ).doWork(
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.unwrap(Session.class).doWork(
|
||||
connection -> {
|
||||
try(Statement statement = connection.createStatement()) {
|
||||
statement.executeUpdate(
|
||||
|
@ -74,22 +74,22 @@ public class PostgreSQLFunctionSelectClauseTest extends BaseEntityManagerFunctio
|
|||
);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Book book = new Book();
|
||||
|
||||
book.setIsbn( "978-9730228236" );
|
||||
book.setTitle( "High-Performance Java Persistence" );
|
||||
book.setAuthor( "Vlad Mihalcea" );
|
||||
book.setPriceCents( 4500 );
|
||||
book.setIsbn("978-9730228236");
|
||||
book.setTitle("High-Performance Java Persistence");
|
||||
book.setAuthor("Vlad Mihalcea");
|
||||
book.setPriceCents(4500);
|
||||
|
||||
entityManager.persist( book );
|
||||
entityManager.persist(book);
|
||||
});
|
||||
}
|
||||
|
||||
@After
|
||||
public void destroy() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.unwrap( Session.class ).doWork(
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.unwrap(Session.class).doWork(
|
||||
connection -> {
|
||||
try(Statement statement = connection.createStatement()) {
|
||||
statement.executeUpdate(
|
||||
|
@ -103,20 +103,20 @@ public class PostgreSQLFunctionSelectClauseTest extends BaseEntityManagerFunctio
|
|||
|
||||
@Test
|
||||
public void testHibernateSelectClauseFunction() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::hql-user-defined-function-postgresql-select-clause-example[]
|
||||
List<Tuple> books = entityManager.createQuery(
|
||||
"select b.title as title, apply_vat(b.priceCents) as price " +
|
||||
"from Book b " +
|
||||
"where b.author = :author ", Tuple.class )
|
||||
.setParameter( "author", "Vlad Mihalcea" )
|
||||
"where b.author = :author ", Tuple.class)
|
||||
.setParameter("author", "Vlad Mihalcea")
|
||||
.getResultList();
|
||||
|
||||
assertEquals( 1, books.size() );
|
||||
assertEquals(1, books.size());
|
||||
|
||||
Tuple book = books.get( 0 );
|
||||
assertEquals( "High-Performance Java Persistence", book.get( "title" ) );
|
||||
assertEquals( 5400, ((Number) book.get( "price" )).intValue() );
|
||||
Tuple book = books.get(0);
|
||||
assertEquals("High-Performance Java Persistence", book.get("title"));
|
||||
assertEquals(5400, ((Number) book.get("price")).intValue());
|
||||
|
||||
//end::hql-user-defined-function-postgresql-select-clause-example[]
|
||||
});
|
||||
|
|
|
@ -38,8 +38,8 @@ public class PostgreSQLFunctionWhereClauseTest extends BaseEntityManagerFunction
|
|||
|
||||
@Override
|
||||
protected void afterEntityManagerFactoryBuilt() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.unwrap( Session.class ).doWork(
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.unwrap(Session.class).doWork(
|
||||
connection -> {
|
||||
try(Statement statement = connection.createStatement()) {
|
||||
//tag::hql-user-defined-function-postgresql-example[]
|
||||
|
@ -56,24 +56,24 @@ public class PostgreSQLFunctionWhereClauseTest extends BaseEntityManagerFunction
|
|||
);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::hql-user-defined-function-postgresql-entity-example[]
|
||||
Book book = new Book();
|
||||
|
||||
book.setIsbn( "978-9730228236" );
|
||||
book.setTitle( "High-Performance Java Persistence" );
|
||||
book.setAuthor( "Vlad Mihalcea" );
|
||||
book.setPriceCents( 4500 );
|
||||
book.setIsbn("978-9730228236");
|
||||
book.setTitle("High-Performance Java Persistence");
|
||||
book.setAuthor("Vlad Mihalcea");
|
||||
book.setPriceCents(4500);
|
||||
|
||||
entityManager.persist( book );
|
||||
entityManager.persist(book);
|
||||
//end::hql-user-defined-function-postgresql-entity-example[]
|
||||
});
|
||||
}
|
||||
|
||||
@After
|
||||
public void destroy() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.unwrap( Session.class ).doWork(
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.unwrap(Session.class).doWork(
|
||||
connection -> {
|
||||
try(Statement statement = connection.createStatement()) {
|
||||
statement.executeUpdate(
|
||||
|
@ -87,18 +87,18 @@ public class PostgreSQLFunctionWhereClauseTest extends BaseEntityManagerFunction
|
|||
|
||||
@Test
|
||||
public void testHibernatePassThroughFunction() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::hql-user-defined-function-postgresql-where-clause-example[]
|
||||
List<Book> books = entityManager.createQuery(
|
||||
"select b " +
|
||||
"from Book b " +
|
||||
"where apply_vat(b.priceCents) = :price ", Book.class )
|
||||
.setParameter( "price", 5400 )
|
||||
"where apply_vat(b.priceCents) = :price ", Book.class)
|
||||
.setParameter("price", 5400)
|
||||
.getResultList();
|
||||
|
||||
assertTrue( books
|
||||
assertTrue(books
|
||||
.stream()
|
||||
.filter( book -> "High-Performance Java Persistence".equals( book.getTitle() ) )
|
||||
.filter(book -> "High-Performance Java Persistence".equals(book.getTitle()))
|
||||
.findAny()
|
||||
.isPresent()
|
||||
);
|
||||
|
@ -108,18 +108,18 @@ public class PostgreSQLFunctionWhereClauseTest extends BaseEntityManagerFunction
|
|||
|
||||
@Test
|
||||
public void testCustomFunction() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::hql-user-defined-function-postgresql-jpql-example[]
|
||||
List<Book> books = entityManager.createQuery(
|
||||
"select b " +
|
||||
"from Book b " +
|
||||
"where function('apply_vat', b.priceCents) = :price ", Book.class )
|
||||
.setParameter( "price", 5400 )
|
||||
"where function('apply_vat', b.priceCents) = :price ", Book.class)
|
||||
.setParameter("price", 5400)
|
||||
.getResultList();
|
||||
|
||||
assertTrue( books
|
||||
assertTrue(books
|
||||
.stream()
|
||||
.filter( book -> "High-Performance Java Persistence".equals( book.getTitle() ) )
|
||||
.filter(book -> "High-Performance Java Persistence".equals(book.getTitle()))
|
||||
.findAny()
|
||||
.isPresent()
|
||||
);
|
||||
|
|
|
@ -41,28 +41,28 @@ public class SelectDistinctTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Before
|
||||
public void init() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person gavinKing = new Person("Gavin", "King" );
|
||||
Person stephanKing = new Person("Stephen", "King" );
|
||||
Person vladMihalcea = new Person("Vlad", "Mihalcea" );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Person gavinKing = new Person("Gavin", "King");
|
||||
Person stephanKing = new Person("Stephen", "King");
|
||||
Person vladMihalcea = new Person("Vlad", "Mihalcea");
|
||||
|
||||
gavinKing.addBook( new Book( "Hibernate in Action" ) );
|
||||
gavinKing.addBook( new Book( "Java Persistence with Hibernate" ) );
|
||||
gavinKing.addBook(new Book("Hibernate in Action"));
|
||||
gavinKing.addBook(new Book("Java Persistence with Hibernate"));
|
||||
|
||||
stephanKing.addBook( new Book( "The Green Mile" ) );
|
||||
stephanKing.addBook(new Book("The Green Mile"));
|
||||
|
||||
vladMihalcea.addBook( new Book( "High-Performance Java Persistence" ) );
|
||||
vladMihalcea.addBook(new Book("High-Performance Java Persistence"));
|
||||
|
||||
entityManager.persist( gavinKing );
|
||||
entityManager.persist( stephanKing );
|
||||
entityManager.persist( vladMihalcea );
|
||||
entityManager.persist(gavinKing);
|
||||
entityManager.persist(stephanKing);
|
||||
entityManager.persist(vladMihalcea);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDistinctProjection() {
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::hql-distinct-projection-query-example[]
|
||||
List<String> lastNames = entityManager.createQuery(
|
||||
"select distinct p.lastName " +
|
||||
|
@ -72,13 +72,13 @@ public class SelectDistinctTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
assertTrue(
|
||||
lastNames.size() == 2 &&
|
||||
lastNames.contains( "King" ) &&
|
||||
lastNames.contains( "Mihalcea" )
|
||||
lastNames.contains("King") &&
|
||||
lastNames.contains("Mihalcea")
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Person") @Table( name = "person")
|
||||
@Entity(name = "Person") @Table(name = "person")
|
||||
public static class Person {
|
||||
|
||||
@Id
|
||||
|
@ -92,7 +92,7 @@ public class SelectDistinctTest extends BaseEntityManagerFunctionalTestCase {
|
|||
private String lastName;
|
||||
|
||||
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL)
|
||||
private List<Book> books = new ArrayList<>( );
|
||||
private List<Book> books = new ArrayList<>();
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
@ -131,12 +131,12 @@ public class SelectDistinctTest extends BaseEntityManagerFunctionalTestCase {
|
|||
}
|
||||
|
||||
public void addBook(Book book) {
|
||||
books.add( book );
|
||||
book.setAuthor( this );
|
||||
books.add(book);
|
||||
book.setAuthor(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "Book") @Table( name = "book")
|
||||
@Entity(name = "Book") @Table(name = "book")
|
||||
public static class Book {
|
||||
|
||||
@Id
|
||||
|
|
|
@ -37,43 +37,43 @@ public class CollectionImmutabilityTest extends BaseEntityManagerFunctionalTestC
|
|||
@Test
|
||||
public void test() {
|
||||
//tag::collection-immutability-persist-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Batch batch = new Batch();
|
||||
batch.setId( 1L );
|
||||
batch.setName( "Change request" );
|
||||
batch.setId(1L);
|
||||
batch.setName("Change request");
|
||||
|
||||
Event event1 = new Event();
|
||||
event1.setId( 1L );
|
||||
event1.setCreatedOn( new Date( ) );
|
||||
event1.setMessage( "Update Hibernate User Guide" );
|
||||
event1.setId(1L);
|
||||
event1.setCreatedOn(new Date());
|
||||
event1.setMessage("Update Hibernate User Guide");
|
||||
|
||||
Event event2 = new Event();
|
||||
event2.setId( 2L );
|
||||
event2.setCreatedOn( new Date( ) );
|
||||
event2.setMessage( "Update Hibernate Getting Started Guide" );
|
||||
event2.setId(2L);
|
||||
event2.setCreatedOn(new Date());
|
||||
event2.setMessage("Update Hibernate Getting Started Guide");
|
||||
|
||||
batch.getEvents().add( event1 );
|
||||
batch.getEvents().add( event2 );
|
||||
batch.getEvents().add(event1);
|
||||
batch.getEvents().add(event2);
|
||||
|
||||
entityManager.persist( batch );
|
||||
} );
|
||||
entityManager.persist(batch);
|
||||
});
|
||||
//end::collection-immutability-persist-example[]
|
||||
//tag::collection-entity-update-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Batch batch = entityManager.find( Batch.class, 1L );
|
||||
log.info( "Change batch name" );
|
||||
batch.setName( "Proposed change request" );
|
||||
} );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Batch batch = entityManager.find(Batch.class, 1L);
|
||||
log.info("Change batch name");
|
||||
batch.setName("Proposed change request");
|
||||
});
|
||||
//end::collection-entity-update-example[]
|
||||
//tag::collection-immutability-update-example[]
|
||||
try {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Batch batch = entityManager.find( Batch.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Batch batch = entityManager.find(Batch.class, 1L);
|
||||
batch.getEvents().clear();
|
||||
} );
|
||||
});
|
||||
}
|
||||
catch ( Exception e ) {
|
||||
log.error( "Immutable collections cannot be modified" );
|
||||
catch (Exception e) {
|
||||
log.error("Immutable collections cannot be modified");
|
||||
}
|
||||
//end::collection-immutability-update-example[]
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ public class CollectionImmutabilityTest extends BaseEntityManagerFunctionalTestC
|
|||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@Immutable
|
||||
private List<Event> events = new ArrayList<>( );
|
||||
private List<Event> events = new ArrayList<>();
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
|
||||
|
|
|
@ -33,25 +33,25 @@ public class EntityImmutabilityTest extends BaseEntityManagerFunctionalTestCase
|
|||
@Test
|
||||
public void test() {
|
||||
//tag::entity-immutability-persist-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Event event = new Event();
|
||||
event.setId( 1L );
|
||||
event.setCreatedOn( new Date( ) );
|
||||
event.setMessage( "Hibernate User Guide rocks!" );
|
||||
event.setId(1L);
|
||||
event.setCreatedOn(new Date());
|
||||
event.setMessage("Hibernate User Guide rocks!");
|
||||
|
||||
entityManager.persist( event );
|
||||
} );
|
||||
entityManager.persist(event);
|
||||
});
|
||||
//end::entity-immutability-persist-example[]
|
||||
//tag::entity-immutability-update-example[]
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Event event = entityManager.find( Event.class, 1L );
|
||||
log.info( "Change event message" );
|
||||
event.setMessage( "Hibernate User Guide" );
|
||||
} );
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Event event = entityManager.find( Event.class, 1L );
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Event event = entityManager.find(Event.class, 1L);
|
||||
log.info("Change event message");
|
||||
event.setMessage("Hibernate User Guide");
|
||||
});
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Event event = entityManager.find(Event.class, 1L);
|
||||
assertEquals("Hibernate User Guide rocks!", event.getMessage());
|
||||
} );
|
||||
});
|
||||
//end::entity-immutability-update-example[]
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ import static org.junit.Assert.assertEquals;
|
|||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@RequiresDialect( H2Dialect.class )
|
||||
@RequiresDialect(H2Dialect.class)
|
||||
public class DiscriminatorNotNullSingleTableTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
|
@ -44,65 +44,65 @@ public class DiscriminatorNotNullSingleTableTest extends BaseEntityManagerFuncti
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::entity-inheritance-single-table-discriminator-value-persist-example[]
|
||||
DebitAccount debitAccount = new DebitAccount();
|
||||
debitAccount.setId( 1L );
|
||||
debitAccount.setOwner( "John Doe" );
|
||||
debitAccount.setBalance( BigDecimal.valueOf( 100 ) );
|
||||
debitAccount.setInterestRate( BigDecimal.valueOf( 1.5d ) );
|
||||
debitAccount.setOverdraftFee( BigDecimal.valueOf( 25 ) );
|
||||
debitAccount.setId(1L);
|
||||
debitAccount.setOwner("John Doe");
|
||||
debitAccount.setBalance(BigDecimal.valueOf(100));
|
||||
debitAccount.setInterestRate(BigDecimal.valueOf(1.5d));
|
||||
debitAccount.setOverdraftFee(BigDecimal.valueOf(25));
|
||||
|
||||
CreditAccount creditAccount = new CreditAccount();
|
||||
creditAccount.setId( 2L );
|
||||
creditAccount.setOwner( "John Doe" );
|
||||
creditAccount.setBalance( BigDecimal.valueOf( 1000 ) );
|
||||
creditAccount.setInterestRate( BigDecimal.valueOf( 1.9d ) );
|
||||
creditAccount.setCreditLimit( BigDecimal.valueOf( 5000 ) );
|
||||
creditAccount.setId(2L);
|
||||
creditAccount.setOwner("John Doe");
|
||||
creditAccount.setBalance(BigDecimal.valueOf(1000));
|
||||
creditAccount.setInterestRate(BigDecimal.valueOf(1.9d));
|
||||
creditAccount.setCreditLimit(BigDecimal.valueOf(5000));
|
||||
|
||||
Account account = new Account();
|
||||
account.setId( 3L );
|
||||
account.setOwner( "John Doe" );
|
||||
account.setBalance( BigDecimal.valueOf( 1000 ) );
|
||||
account.setInterestRate( BigDecimal.valueOf( 1.9d ) );
|
||||
account.setId(3L);
|
||||
account.setOwner("John Doe");
|
||||
account.setBalance(BigDecimal.valueOf(1000));
|
||||
account.setInterestRate(BigDecimal.valueOf(1.9d));
|
||||
|
||||
entityManager.persist( debitAccount );
|
||||
entityManager.persist( creditAccount );
|
||||
entityManager.persist( account );
|
||||
entityManager.persist(debitAccount);
|
||||
entityManager.persist(creditAccount);
|
||||
entityManager.persist(account);
|
||||
|
||||
entityManager.unwrap( Session.class ).doWork( connection -> {
|
||||
entityManager.unwrap(Session.class).doWork(connection -> {
|
||||
try(Statement statement = connection.createStatement()) {
|
||||
statement.executeUpdate(
|
||||
"insert into Account (DTYPE, active, balance, interestRate, owner, id) " +
|
||||
"values ('Other', true, 25, 0.5, 'Vlad', 4)"
|
||||
);
|
||||
}
|
||||
} );
|
||||
});
|
||||
//end::entity-inheritance-single-table-discriminator-value-persist-example[]
|
||||
} );
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::entity-inheritance-single-table-discriminator-value-persist-example[]
|
||||
|
||||
Map<Long, Account> accounts = entityManager.createQuery(
|
||||
"select a from Account a", Account.class )
|
||||
"select a from Account a", Account.class)
|
||||
.getResultList()
|
||||
.stream()
|
||||
.collect( Collectors.toMap( Account::getId, Function.identity()));
|
||||
.collect(Collectors.toMap(Account::getId, Function.identity()));
|
||||
|
||||
assertEquals(4, accounts.size());
|
||||
assertEquals( DebitAccount.class, accounts.get( 1L ).getClass() );
|
||||
assertEquals( CreditAccount.class, accounts.get( 2L ).getClass() );
|
||||
assertEquals( Account.class, accounts.get( 3L ).getClass() );
|
||||
assertEquals( OtherAccount.class, accounts.get( 4L ).getClass() );
|
||||
assertEquals(DebitAccount.class, accounts.get(1L).getClass());
|
||||
assertEquals(CreditAccount.class, accounts.get(2L).getClass());
|
||||
assertEquals(Account.class, accounts.get(3L).getClass());
|
||||
assertEquals(OtherAccount.class, accounts.get(4L).getClass());
|
||||
//end::entity-inheritance-single-table-discriminator-value-persist-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::entity-inheritance-single-table-discriminator-value-example[]
|
||||
@Entity(name = "Account")
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorValue( "null" )
|
||||
@DiscriminatorValue("null")
|
||||
public static class Account {
|
||||
|
||||
@Id
|
||||
|
@ -153,7 +153,7 @@ public class DiscriminatorNotNullSingleTableTest extends BaseEntityManagerFuncti
|
|||
}
|
||||
|
||||
@Entity(name = "DebitAccount")
|
||||
@DiscriminatorValue( "Debit" )
|
||||
@DiscriminatorValue("Debit")
|
||||
public static class DebitAccount extends Account {
|
||||
|
||||
private BigDecimal overdraftFee;
|
||||
|
@ -173,7 +173,7 @@ public class DiscriminatorNotNullSingleTableTest extends BaseEntityManagerFuncti
|
|||
}
|
||||
|
||||
@Entity(name = "CreditAccount")
|
||||
@DiscriminatorValue( "Credit" )
|
||||
@DiscriminatorValue("Credit")
|
||||
public static class CreditAccount extends Account {
|
||||
|
||||
private BigDecimal creditLimit;
|
||||
|
@ -193,7 +193,7 @@ public class DiscriminatorNotNullSingleTableTest extends BaseEntityManagerFuncti
|
|||
}
|
||||
|
||||
@Entity(name = "OtherAccount")
|
||||
@DiscriminatorValue( "not null" )
|
||||
@DiscriminatorValue("not null")
|
||||
public static class OtherAccount extends Account {
|
||||
|
||||
private boolean active;
|
||||
|
|
|
@ -31,7 +31,7 @@ import static org.junit.Assert.assertEquals;
|
|||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
@RequiresDialect( H2Dialect.class )
|
||||
@RequiresDialect(H2Dialect.class)
|
||||
public class DiscriminatorOptionsNotNullSingleTableTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
|
@ -45,62 +45,62 @@ public class DiscriminatorOptionsNotNullSingleTableTest extends BaseEntityManage
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
entityManager.unwrap( Session.class ).doWork( connection -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
entityManager.unwrap(Session.class).doWork(connection -> {
|
||||
try(Statement statement = connection.createStatement()) {
|
||||
//statement.executeUpdate( "ALTER TABLE Account ALTER COLUMN DTYPE SET NULL" );
|
||||
//statement.executeUpdate("ALTER TABLE Account ALTER COLUMN DTYPE SET NULL");
|
||||
}
|
||||
} );
|
||||
});
|
||||
|
||||
|
||||
DebitAccount debitAccount = new DebitAccount();
|
||||
debitAccount.setId( 1L );
|
||||
debitAccount.setOwner( "John Doe" );
|
||||
debitAccount.setBalance( BigDecimal.valueOf( 100 ) );
|
||||
debitAccount.setInterestRate( BigDecimal.valueOf( 1.5d ) );
|
||||
debitAccount.setOverdraftFee( BigDecimal.valueOf( 25 ) );
|
||||
debitAccount.setId(1L);
|
||||
debitAccount.setOwner("John Doe");
|
||||
debitAccount.setBalance(BigDecimal.valueOf(100));
|
||||
debitAccount.setInterestRate(BigDecimal.valueOf(1.5d));
|
||||
debitAccount.setOverdraftFee(BigDecimal.valueOf(25));
|
||||
|
||||
CreditAccount creditAccount = new CreditAccount();
|
||||
creditAccount.setId( 2L );
|
||||
creditAccount.setOwner( "John Doe" );
|
||||
creditAccount.setBalance( BigDecimal.valueOf( 1000 ) );
|
||||
creditAccount.setInterestRate( BigDecimal.valueOf( 1.9d ) );
|
||||
creditAccount.setCreditLimit( BigDecimal.valueOf( 5000 ) );
|
||||
creditAccount.setId(2L);
|
||||
creditAccount.setOwner("John Doe");
|
||||
creditAccount.setBalance(BigDecimal.valueOf(1000));
|
||||
creditAccount.setInterestRate(BigDecimal.valueOf(1.9d));
|
||||
creditAccount.setCreditLimit(BigDecimal.valueOf(5000));
|
||||
|
||||
Account account = new Account();
|
||||
account.setId( 3L );
|
||||
account.setOwner( "John Doe" );
|
||||
account.setBalance( BigDecimal.valueOf( 1000 ) );
|
||||
account.setInterestRate( BigDecimal.valueOf( 1.9d ) );
|
||||
account.setId(3L);
|
||||
account.setOwner("John Doe");
|
||||
account.setBalance(BigDecimal.valueOf(1000));
|
||||
account.setInterestRate(BigDecimal.valueOf(1.9d));
|
||||
|
||||
entityManager.persist( debitAccount );
|
||||
entityManager.persist( creditAccount );
|
||||
entityManager.persist( account );
|
||||
entityManager.persist(debitAccount);
|
||||
entityManager.persist(creditAccount);
|
||||
entityManager.persist(account);
|
||||
|
||||
entityManager.unwrap( Session.class ).doWork( connection -> {
|
||||
entityManager.unwrap(Session.class).doWork(connection -> {
|
||||
try(Statement statement = connection.createStatement()) {
|
||||
statement.executeUpdate(
|
||||
"insert into Account (DTYPE, active, balance, interestRate, owner, id) " +
|
||||
"values ('Other', true, 25, 0.5, 'Vlad', 4)"
|
||||
);
|
||||
}
|
||||
} );
|
||||
} );
|
||||
});
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
Map<Long, Account> accounts = entityManager.createQuery(
|
||||
"select a from Account a", Account.class )
|
||||
"select a from Account a", Account.class)
|
||||
.getResultList()
|
||||
.stream()
|
||||
.collect( Collectors.toMap( Account::getId, Function.identity()));
|
||||
.collect(Collectors.toMap(Account::getId, Function.identity()));
|
||||
|
||||
assertEquals(4, accounts.size());
|
||||
assertEquals( DebitAccount.class, accounts.get( 1L ).getClass() );
|
||||
assertEquals( CreditAccount.class, accounts.get( 2L ).getClass() );
|
||||
assertEquals( Account.class, accounts.get( 3L ).getClass() );
|
||||
assertEquals( OtherAccount.class, accounts.get( 4L ).getClass() );
|
||||
assertEquals(DebitAccount.class, accounts.get(1L).getClass());
|
||||
assertEquals(CreditAccount.class, accounts.get(2L).getClass());
|
||||
assertEquals(Account.class, accounts.get(3L).getClass());
|
||||
assertEquals(OtherAccount.class, accounts.get(4L).getClass());
|
||||
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
@Entity(name = "Account")
|
||||
|
@ -151,7 +151,7 @@ public class DiscriminatorOptionsNotNullSingleTableTest extends BaseEntityManage
|
|||
}
|
||||
|
||||
@Entity(name = "DebitAccount")
|
||||
@DiscriminatorValue( "Debit" )
|
||||
@DiscriminatorValue("Debit")
|
||||
public static class DebitAccount extends Account {
|
||||
|
||||
private BigDecimal overdraftFee;
|
||||
|
@ -166,7 +166,7 @@ public class DiscriminatorOptionsNotNullSingleTableTest extends BaseEntityManage
|
|||
}
|
||||
|
||||
@Entity(name = "CreditAccount")
|
||||
@DiscriminatorValue( "Credit" )
|
||||
@DiscriminatorValue("Credit")
|
||||
public static class CreditAccount extends Account {
|
||||
|
||||
private BigDecimal creditLimit;
|
||||
|
@ -181,7 +181,7 @@ public class DiscriminatorOptionsNotNullSingleTableTest extends BaseEntityManage
|
|||
}
|
||||
|
||||
@Entity(name = "OtherAccount")
|
||||
@DiscriminatorValue( "Other" )
|
||||
@DiscriminatorValue("Other")
|
||||
public static class OtherAccount extends Account {
|
||||
|
||||
private boolean active;
|
||||
|
|
|
@ -35,29 +35,29 @@ public class JoinTablePrimaryKeyJoinColumnTest extends BaseEntityManagerFunction
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
DebitAccount debitAccount = new DebitAccount();
|
||||
debitAccount.setId( 1L );
|
||||
debitAccount.setOwner( "John Doe" );
|
||||
debitAccount.setBalance( BigDecimal.valueOf( 100 ) );
|
||||
debitAccount.setInterestRate( BigDecimal.valueOf( 1.5d ) );
|
||||
debitAccount.setOverdraftFee( BigDecimal.valueOf( 25 ) );
|
||||
debitAccount.setId(1L);
|
||||
debitAccount.setOwner("John Doe");
|
||||
debitAccount.setBalance(BigDecimal.valueOf(100));
|
||||
debitAccount.setInterestRate(BigDecimal.valueOf(1.5d));
|
||||
debitAccount.setOverdraftFee(BigDecimal.valueOf(25));
|
||||
|
||||
CreditAccount creditAccount = new CreditAccount();
|
||||
creditAccount.setId( 2L );
|
||||
creditAccount.setOwner( "John Doe" );
|
||||
creditAccount.setBalance( BigDecimal.valueOf( 1000 ) );
|
||||
creditAccount.setInterestRate( BigDecimal.valueOf( 1.9d ) );
|
||||
creditAccount.setCreditLimit( BigDecimal.valueOf( 5000 ) );
|
||||
creditAccount.setId(2L);
|
||||
creditAccount.setOwner("John Doe");
|
||||
creditAccount.setBalance(BigDecimal.valueOf(1000));
|
||||
creditAccount.setInterestRate(BigDecimal.valueOf(1.9d));
|
||||
creditAccount.setCreditLimit(BigDecimal.valueOf(5000));
|
||||
|
||||
entityManager.persist( debitAccount );
|
||||
entityManager.persist( creditAccount );
|
||||
} );
|
||||
entityManager.persist(debitAccount);
|
||||
entityManager.persist(creditAccount);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
List<Account> accounts =
|
||||
entityManager.createQuery( "select a from Account a" ).getResultList();
|
||||
} );
|
||||
entityManager.createQuery("select a from Account a").getResultList();
|
||||
});
|
||||
}
|
||||
|
||||
//tag::entity-inheritance-joined-table-primary-key-join-column-example[]
|
||||
|
|
|
@ -34,32 +34,32 @@ public class JoinTableTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
DebitAccount debitAccount = new DebitAccount();
|
||||
debitAccount.setId( 1L );
|
||||
debitAccount.setOwner( "John Doe" );
|
||||
debitAccount.setBalance( BigDecimal.valueOf( 100 ) );
|
||||
debitAccount.setInterestRate( BigDecimal.valueOf( 1.5d ) );
|
||||
debitAccount.setOverdraftFee( BigDecimal.valueOf( 25 ) );
|
||||
debitAccount.setId(1L);
|
||||
debitAccount.setOwner("John Doe");
|
||||
debitAccount.setBalance(BigDecimal.valueOf(100));
|
||||
debitAccount.setInterestRate(BigDecimal.valueOf(1.5d));
|
||||
debitAccount.setOverdraftFee(BigDecimal.valueOf(25));
|
||||
|
||||
CreditAccount creditAccount = new CreditAccount();
|
||||
creditAccount.setId( 2L );
|
||||
creditAccount.setOwner( "John Doe" );
|
||||
creditAccount.setBalance( BigDecimal.valueOf( 1000 ) );
|
||||
creditAccount.setInterestRate( BigDecimal.valueOf( 1.9d ) );
|
||||
creditAccount.setCreditLimit( BigDecimal.valueOf( 5000 ) );
|
||||
creditAccount.setId(2L);
|
||||
creditAccount.setOwner("John Doe");
|
||||
creditAccount.setBalance(BigDecimal.valueOf(1000));
|
||||
creditAccount.setInterestRate(BigDecimal.valueOf(1.9d));
|
||||
creditAccount.setCreditLimit(BigDecimal.valueOf(5000));
|
||||
|
||||
entityManager.persist( debitAccount );
|
||||
entityManager.persist( creditAccount );
|
||||
} );
|
||||
entityManager.persist(debitAccount);
|
||||
entityManager.persist(creditAccount);
|
||||
});
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
//tag::entity-inheritance-joined-table-query-example[]
|
||||
List<Account> accounts = entityManager
|
||||
.createQuery( "select a from Account a" )
|
||||
.createQuery("select a from Account a")
|
||||
.getResultList();
|
||||
//end::entity-inheritance-joined-table-query-example[]
|
||||
} );
|
||||
});
|
||||
}
|
||||
|
||||
//tag::entity-inheritance-joined-table-example[]
|
||||
|
|
|
@ -32,24 +32,24 @@ public class MappedSuperclassTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
DebitAccount debitAccount = new DebitAccount();
|
||||
debitAccount.setId( 1L );
|
||||
debitAccount.setOwner( "John Doe" );
|
||||
debitAccount.setBalance( BigDecimal.valueOf( 100 ) );
|
||||
debitAccount.setInterestRate( BigDecimal.valueOf( 1.5d ) );
|
||||
debitAccount.setOverdraftFee( BigDecimal.valueOf( 25 ) );
|
||||
debitAccount.setId(1L);
|
||||
debitAccount.setOwner("John Doe");
|
||||
debitAccount.setBalance(BigDecimal.valueOf(100));
|
||||
debitAccount.setInterestRate(BigDecimal.valueOf(1.5d));
|
||||
debitAccount.setOverdraftFee(BigDecimal.valueOf(25));
|
||||
|
||||
CreditAccount creditAccount = new CreditAccount();
|
||||
creditAccount.setId( 1L );
|
||||
creditAccount.setOwner( "John Doe" );
|
||||
creditAccount.setBalance( BigDecimal.valueOf( 1000 ) );
|
||||
creditAccount.setInterestRate( BigDecimal.valueOf( 1.9d ) );
|
||||
creditAccount.setCreditLimit( BigDecimal.valueOf( 5000 ) );
|
||||
creditAccount.setId(1L);
|
||||
creditAccount.setOwner("John Doe");
|
||||
creditAccount.setBalance(BigDecimal.valueOf(1000));
|
||||
creditAccount.setInterestRate(BigDecimal.valueOf(1.9d));
|
||||
creditAccount.setCreditLimit(BigDecimal.valueOf(5000));
|
||||
|
||||
entityManager.persist( debitAccount );
|
||||
entityManager.persist( creditAccount );
|
||||
} );
|
||||
entityManager.persist(debitAccount);
|
||||
entityManager.persist(creditAccount);
|
||||
});
|
||||
}
|
||||
|
||||
//tag::entity-inheritance-mapped-superclass-example[]
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue