HHH-11290 - Migrate all documentation snippets that derive the source code from extras instead of actual Unit Tests
Fixed in the Identifier chapter
This commit is contained in:
parent
b9b956cd2c
commit
ceaeb81e33
|
@ -1,135 +0,0 @@
|
|||
@Entity
|
||||
public class PersonAddress implements Serializable {
|
||||
|
||||
@Id
|
||||
@ManyToOne
|
||||
private Person person;
|
||||
|
||||
@Id
|
||||
@ManyToOne()
|
||||
private Address address;
|
||||
|
||||
public PersonAddress() {}
|
||||
|
||||
public PersonAddress(Person person, Address address) {
|
||||
this.person = person;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Person getPerson() {
|
||||
return person;
|
||||
}
|
||||
|
||||
public void setPerson(Person person) {
|
||||
this.person = person;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
PersonAddress that = (PersonAddress) o;
|
||||
return Objects.equals(person, that.person) &&
|
||||
Objects.equals(address, that.address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(person, address);
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@NaturalId
|
||||
private String registrationNumber;
|
||||
|
||||
public Person() {}
|
||||
|
||||
public Person(String registrationNumber) {
|
||||
this.registrationNumber = registrationNumber;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Person person = (Person) o;
|
||||
return Objects.equals(registrationNumber, person.registrationNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(registrationNumber);
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
public class Address {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String street;
|
||||
|
||||
private String number;
|
||||
|
||||
private String postalCode;
|
||||
|
||||
public Address() {}
|
||||
|
||||
public Address(String street, String number, String postalCode) {
|
||||
this.street = street;
|
||||
this.number = number;
|
||||
this.postalCode = postalCode;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public String getPostalCode() {
|
||||
return postalCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(street, number, postalCode);
|
||||
}
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
PersonAddress personAddress = entityManager.find(
|
||||
PersonAddress.class,
|
||||
new PersonAddress( person, address )
|
||||
);
|
|
@ -1,18 +0,0 @@
|
|||
@Entity
|
||||
public class Login {
|
||||
|
||||
@EmbeddedId
|
||||
private PK pk;
|
||||
|
||||
@Embeddable
|
||||
public static class PK implements Serializable {
|
||||
|
||||
private String system;
|
||||
|
||||
private String username;
|
||||
|
||||
...
|
||||
}
|
||||
|
||||
...
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
@Entity
|
||||
public class Login {
|
||||
|
||||
@EmbeddedId
|
||||
private PK pk;
|
||||
|
||||
@Embeddable
|
||||
public static class PK implements Serializable {
|
||||
|
||||
@ManyToOne
|
||||
private System system;
|
||||
|
||||
private String username;
|
||||
|
||||
...
|
||||
}
|
||||
|
||||
...
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
@Entity
|
||||
@IdClass( PK.class )
|
||||
public class Login {
|
||||
|
||||
@Id
|
||||
private String system;
|
||||
|
||||
@Id
|
||||
private String username;
|
||||
|
||||
public static class PK implements Serializable {
|
||||
|
||||
private String system;
|
||||
|
||||
private String username;
|
||||
|
||||
...
|
||||
}
|
||||
|
||||
...
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
@Entity
|
||||
@IdClass( PK.class )
|
||||
public class Login {
|
||||
|
||||
@Id
|
||||
@ManyToOne
|
||||
private System system;
|
||||
|
||||
@Id
|
||||
private String username;
|
||||
|
||||
public static class PK implements Serializable {
|
||||
|
||||
private System system;
|
||||
|
||||
private String username;
|
||||
|
||||
...
|
||||
}
|
||||
|
||||
...
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
@Entity
|
||||
@IdClass( PK.class )
|
||||
public class LogFile {
|
||||
|
||||
@Id
|
||||
private String name;
|
||||
|
||||
@Id
|
||||
private LocalDate date;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer uniqueStamp;
|
||||
|
||||
public static class PK implements Serializable {
|
||||
|
||||
private String name;
|
||||
|
||||
private LocalDate date;
|
||||
|
||||
private Integer uniqueStamp;
|
||||
|
||||
...
|
||||
}
|
||||
|
||||
...
|
||||
}
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
@Entity
|
||||
public class MyEntity {
|
||||
|
||||
@Id
|
||||
public Integer id;
|
||||
|
||||
...
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
@Entity
|
||||
public class MyEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
public Integer id;
|
||||
|
||||
...
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
@Entity
|
||||
public class MyEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue( generator = "uuid" )
|
||||
@GenericGenerator(
|
||||
name = "uuid",
|
||||
strategy = "org.hibernate.id.UUIDGenerator",
|
||||
parameters = {
|
||||
@Parameter(
|
||||
name = "uuid_gen_strategy_class",
|
||||
value = "org.hibernate.id.uuid.CustomVersionOneStrategy"
|
||||
)
|
||||
}
|
||||
)
|
||||
public UUID id;
|
||||
|
||||
...
|
||||
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
@Entity
|
||||
public class MyEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
public UUID id;
|
||||
|
||||
...
|
||||
}
|
|
@ -51,11 +51,12 @@ Any types used for identifier attributes beyond this list will not be portable.
|
|||
Values for simple identifiers can be assigned, as we have seen in the examples above.
|
||||
The expectation for assigned identifier values is that the application assigns (sets them on the entity attribute) prior to calling save/persist.
|
||||
|
||||
.Simple assigned identifier
|
||||
[[identifiers-simple-assigned-mapping-example]]
|
||||
.Simple assigned entity identifier
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/SimpleAssigned.java[]
|
||||
include::{sourcedir}/AssignedIdentifierTest.java[tag=identifiers-simple-assigned-mapping-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
|
@ -64,11 +65,12 @@ include::{extrasdir}/id/SimpleAssigned.java[]
|
|||
|
||||
Values for simple identifiers can be generated. To denote that an identifier attribute is generated, it is annotated with `javax.persistence.GeneratedValue`
|
||||
|
||||
[[identifiers-simple-generated-mapping-example]]
|
||||
.Simple generated identifier
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/SimpleGenerated.java[]
|
||||
include::{sourcedir}/GeneratedIdentifierTest.java[tag=identifiers-simple-generated-mapping-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
|
@ -106,28 +108,31 @@ Note especially that collections and one-to-ones are never appropriate.
|
|||
Modeling a composite identifier using an EmbeddedId simply means defining an embeddable to be a composition for the one or more attributes making up the identifier,
|
||||
and then exposing an attribute of that embeddable type on the entity.
|
||||
|
||||
.Basic EmbeddedId
|
||||
[[identifiers-basic-embeddedid-mapping-example]]
|
||||
.Basic `@EmbeddedId`
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/EmbeddedId1.java[]
|
||||
include::{sourcedir}/EmbeddedIdTest.java[tag=identifiers-basic-embeddedid-mapping-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
As mentioned before, EmbeddedIds can even contain ManyToOne attributes.
|
||||
As mentioned before, EmbeddedIds can even contain `@ManyToOne` attributes.
|
||||
|
||||
.EmbeddedId with ManyToOne
|
||||
[[identifiers-basic-embeddedid-manytoone-mapping-example]]
|
||||
.`@EmbeddedId` with `@ManyToOne`
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/EmbeddedId2.java[]
|
||||
include::{sourcedir}/EmbeddedIdManyToOneTest.java[tag=identifiers-basic-embeddedid-manytoone-mapping-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
Hibernate supports directly modeling the ManyToOne in the PK class, whether EmbeddedId or IdClass.
|
||||
However that is not portably supported by the JPA specification.
|
||||
Hibernate supports directly modeling the ManyToOne in the PK class, whether `@EmbeddedId` or `@IdClass`.
|
||||
|
||||
However, that is not portably supported by the JPA specification.
|
||||
In JPA terms one would use "derived identifiers"; for details, see <<identifiers-derived>>.
|
||||
====
|
||||
|
||||
|
@ -137,37 +142,41 @@ In JPA terms one would use "derived identifiers"; for details, see <<identifiers
|
|||
Modeling a composite identifier using an IdClass differs from using an EmbeddedId in that the entity defines each individual attribute making up the composition.
|
||||
The IdClass simply acts as a "shadow".
|
||||
|
||||
.Basic IdClass
|
||||
[[identifiers-basic-idclass-mapping-example]]
|
||||
.Basic `@IdClass`
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/IdClass1.java[]
|
||||
include::{sourcedir}/IdClassTest.java[tag=identifiers-basic-idclass-mapping-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
Non-aggregated composite identifiers can also contain ManyToOne attributes as we saw with aggregated ones (still non-portably).
|
||||
|
||||
.IdClass with ManyToOne
|
||||
[[identifiers-basic-idclass-manytoone-mapping-example]]
|
||||
.IdClass with `@ManyToOne`
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/IdClass2.java[]
|
||||
include::{sourcedir}/IdClassManyToOneTest.java[tag=identifiers-basic-idclass-manytoone-mapping-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
With non-aggregated composite identifiers, Hibernate also supports "partial" generation of the composite values.
|
||||
|
||||
.IdClass with partial generation
|
||||
[[identifiers-basic-idclass-generatedvalue-mapping-example]]
|
||||
.`@IdClass` with partial identifier generation using `@GeneratedValue`
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/IdClass3.java[]
|
||||
include::{sourcedir}/IdClassGeneratedValueTest.java[tag=identifiers-basic-idclass-generatedvalue-mapping-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
[NOTE]
|
||||
====
|
||||
This feature exists because of a highly questionable interpretation of the JPA specification made by the SpecJ committee.
|
||||
|
||||
Hibernate does not feel that JPA defines support for this, but added the feature simply to be usable in SpecJ benchmarks.
|
||||
Use of this feature may or may not be portable from a JPA perspective.
|
||||
====
|
||||
|
@ -178,22 +187,24 @@ Use of this feature may or may not be portable from a JPA perspective.
|
|||
Hibernate allows defining a composite identifier out of entity associations.
|
||||
In the following example, the `PersonAddress` entity identifier is formed of two `@ManyToOne` associations.
|
||||
|
||||
[[identifiers-composite-id-mapping-example]]
|
||||
.Composite identifiers with associations
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/CompositeIdAssociations.java[]
|
||||
include::{sourcedir}/IdManyToOneTest.java[tag=identifiers-composite-id-mapping-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
Although the mapping is much simpler than using an `@EmbeddedId` or an `@IdClass`, there's no separation between the entity instance and the actual identifier.
|
||||
To query this entity, an instance of the entity itself must be supplied to the persistence context.
|
||||
|
||||
.Composite identifiers with associations query
|
||||
[[identifiers-composite-id-fetching-example]]
|
||||
.Fetchcing with composite identifiers
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/CompositeIdAssociationsQuery.java[]
|
||||
include::{sourcedir}/IdManyToOneTest.java[tag=identifiers-composite-id-fetching-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
|
@ -383,22 +394,24 @@ These strategies are defined by the `org.hibernate.id.UUIDGenerationStrategy` co
|
|||
The default strategy is a version 4 (random) strategy according to IETF RFC 4122.
|
||||
Hibernate does ship with an alternative strategy which is a RFC 4122 version 1 (time-based) strategy (using ip address rather than mac address).
|
||||
|
||||
[[identifiers-generators-uuid-mapping-example]]
|
||||
.Implicitly using the random UUID strategy
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/UUIDRandom.java[]
|
||||
include::{sourcedir}/UuidGeneratedValueTest.java[tag=identifiers-generators-uuid-mapping-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
To specify an alternative generation strategy, we'd have to define some configuration via `@GenericGenerator`.
|
||||
Here we choose the RFC 4122 version 1 compliant strategy named `org.hibernate.id.uuid.CustomVersionOneStrategy`.
|
||||
|
||||
[[identifiers-generators-custom-uuid-mapping-example]]
|
||||
.Implicitly using the random UUID strategy
|
||||
====
|
||||
[source,java]
|
||||
----
|
||||
include::{extrasdir}/id/UUIDCustomVersionOneStrategy.java[]
|
||||
include::{sourcedir}/UuidCustomGeneratedValueTest.java[tag=identifiers-generators-custom-uuid-mapping-example, indent=0]
|
||||
----
|
||||
====
|
||||
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.identifier;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class AssignedIdentifierTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Book.class
|
||||
};
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Book book = new Book();
|
||||
book.setId( 1L );
|
||||
book.setTitle( "High-Performance Java Persistence" );
|
||||
book.setAuthor( "Vlad Mihalcea" );
|
||||
|
||||
entityManager.persist( book );
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testIdentityScope() {
|
||||
|
||||
}
|
||||
|
||||
//tag::identifiers-simple-assigned-mapping-example[]
|
||||
@Entity(name = "Book")
|
||||
public static class Book {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String author;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-simple-assigned-mapping-example[]
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
//tag::identifiers-simple-assigned-mapping-example[]
|
||||
}
|
||||
//end::identifiers-simple-assigned-mapping-example[]
|
||||
}
|
|
@ -0,0 +1,174 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.identifier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.EmbeddedId;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class EmbeddedIdManyToOneTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
SystemUser.class,
|
||||
Subsystem.class
|
||||
};
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Subsystem subsystem = new Subsystem();
|
||||
subsystem.setId( "Hibernate Forum" );
|
||||
subsystem.setDescription( "Hibernate projects forum" );
|
||||
entityManager.persist( subsystem );
|
||||
|
||||
SystemUser systemUser = new SystemUser();
|
||||
systemUser.setPk( new PK(
|
||||
subsystem,
|
||||
"vlad"
|
||||
) );
|
||||
systemUser.setName( "Vlad Mihalcea" );
|
||||
|
||||
entityManager.persist( systemUser );
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Subsystem subsystem = entityManager.find(
|
||||
Subsystem.class,
|
||||
"Hibernate Forum"
|
||||
);
|
||||
SystemUser systemUser = entityManager.find(
|
||||
SystemUser.class,
|
||||
new PK(
|
||||
subsystem,
|
||||
"vlad"
|
||||
)
|
||||
);
|
||||
|
||||
assertEquals( "Vlad Mihalcea", systemUser.getName() );
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
//tag::identifiers-basic-embeddedid-manytoone-mapping-example[]
|
||||
@Entity(name = "SystemUser")
|
||||
public static class SystemUser {
|
||||
|
||||
@EmbeddedId
|
||||
private PK pk;
|
||||
|
||||
private String name;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-basic-embeddedid-manytoone-mapping-example[]
|
||||
|
||||
public PK getPk() {
|
||||
return pk;
|
||||
}
|
||||
|
||||
public void setPk(PK pk) {
|
||||
this.pk = pk;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
//tag::identifiers-basic-embeddedid-manytoone-mapping-example[]
|
||||
}
|
||||
|
||||
@Entity(name = "Subsystem")
|
||||
public static class Subsystem {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String description;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-basic-embeddedid-manytoone-mapping-example[]
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
//tag::identifiers-basic-embeddedid-manytoone-mapping-example[]
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
public static class PK implements Serializable {
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private Subsystem subsystem;
|
||||
|
||||
private String username;
|
||||
|
||||
public PK(Subsystem subsystem, String username) {
|
||||
this.subsystem = subsystem;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
private PK() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
PK pk = (PK) o;
|
||||
return Objects.equals( subsystem, pk.subsystem ) &&
|
||||
Objects.equals( username, pk.username );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( subsystem, username );
|
||||
}
|
||||
}
|
||||
//end::identifiers-basic-embeddedid-manytoone-mapping-example[]
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.identifier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.EmbeddedId;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class EmbeddedIdTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
SystemUser.class
|
||||
};
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
SystemUser systemUser = new SystemUser();
|
||||
systemUser.setPk( new PK(
|
||||
"Hibernate Forum",
|
||||
"vlad"
|
||||
) );
|
||||
systemUser.setName( "Vlad Mihalcea" );
|
||||
|
||||
entityManager.persist( systemUser );
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
SystemUser systemUser = entityManager.find(
|
||||
SystemUser.class,
|
||||
new PK(
|
||||
"Hibernate Forum",
|
||||
"vlad"
|
||||
)
|
||||
);
|
||||
|
||||
assertEquals( "Vlad Mihalcea", systemUser.getName() );
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
//tag::identifiers-basic-embeddedid-mapping-example[]
|
||||
@Entity(name = "SystemUser")
|
||||
public static class SystemUser {
|
||||
|
||||
@EmbeddedId
|
||||
private PK pk;
|
||||
|
||||
private String name;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-basic-embeddedid-mapping-example[]
|
||||
|
||||
public PK getPk() {
|
||||
return pk;
|
||||
}
|
||||
|
||||
public void setPk(PK pk) {
|
||||
this.pk = pk;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
//tag::identifiers-basic-embeddedid-mapping-example[]
|
||||
}
|
||||
|
||||
@Embeddable
|
||||
public static class PK implements Serializable {
|
||||
|
||||
private String subsystem;
|
||||
|
||||
private String username;
|
||||
|
||||
public PK(String subsystem, String username) {
|
||||
this.subsystem = subsystem;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
private PK() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
PK pk = (PK) o;
|
||||
return Objects.equals( subsystem, pk.subsystem ) &&
|
||||
Objects.equals( username, pk.username );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( subsystem, username );
|
||||
}
|
||||
}
|
||||
//end::identifiers-basic-embeddedid-mapping-example[]
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.identifier;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class GeneratedIdentifierTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Book.class
|
||||
};
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Book book = new Book();
|
||||
book.setTitle( "High-Performance Java Persistence" );
|
||||
book.setAuthor( "Vlad Mihalcea" );
|
||||
|
||||
entityManager.persist( book );
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
}
|
||||
|
||||
//tag::identifiers-simple-generated-mapping-example[]
|
||||
@Entity(name = "Book")
|
||||
public static class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String author;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-simple-generated-mapping-example[]
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
//tag::identifiers-simple-generated-mapping-example[]
|
||||
}
|
||||
//end::identifiers-simple-generated-mapping-example[]
|
||||
}
|
|
@ -0,0 +1,175 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.identifier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class IdClassGeneratedValueTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
SystemUser.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
SystemUser _systemUser = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
SystemUser systemUser = new SystemUser();
|
||||
systemUser.setId(
|
||||
new PK(
|
||||
"Hibernate Forum",
|
||||
"vlad"
|
||||
)
|
||||
);
|
||||
systemUser.setName( "Vlad Mihalcea" );
|
||||
|
||||
entityManager.persist( systemUser );
|
||||
|
||||
return systemUser;
|
||||
} );
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
SystemUser systemUser = entityManager.find(
|
||||
SystemUser.class,
|
||||
_systemUser.getId()
|
||||
);
|
||||
|
||||
assertEquals( "Vlad Mihalcea", systemUser.getName() );
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
//tag::identifiers-basic-idclass-generatedvalue-mapping-example[]
|
||||
@Entity(name = "SystemUser")
|
||||
@IdClass( PK.class )
|
||||
public static class SystemUser {
|
||||
|
||||
@Id
|
||||
private String subsystem;
|
||||
|
||||
@Id
|
||||
private String username;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer registrationId;
|
||||
|
||||
private String name;
|
||||
|
||||
public PK getId() {
|
||||
return new PK(
|
||||
subsystem,
|
||||
username,
|
||||
registrationId
|
||||
);
|
||||
}
|
||||
|
||||
public void setId(PK id) {
|
||||
this.subsystem = id.getSubsystem();
|
||||
this.username = id.getUsername();
|
||||
this.registrationId = id.getRegistrationId();
|
||||
}
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-basic-idclass-generatedvalue-mapping-example[]
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
//tag::identifiers-basic-idclass-generatedvalue-mapping-example[]
|
||||
}
|
||||
|
||||
public static class PK implements Serializable {
|
||||
|
||||
private String subsystem;
|
||||
|
||||
private String username;
|
||||
|
||||
private Integer registrationId;
|
||||
|
||||
public PK(String subsystem, String username) {
|
||||
this.subsystem = subsystem;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public PK(String subsystem, String username, Integer registrationId) {
|
||||
this.subsystem = subsystem;
|
||||
this.username = username;
|
||||
this.registrationId = registrationId;
|
||||
}
|
||||
|
||||
private PK() {
|
||||
}
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-basic-idclass-generatedvalue-mapping-example[]
|
||||
|
||||
public String getSubsystem() {
|
||||
return subsystem;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public Integer getRegistrationId() {
|
||||
return registrationId;
|
||||
}
|
||||
|
||||
//tag::identifiers-basic-idclass-generatedvalue-mapping-example[]
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
PK pk = (PK) o;
|
||||
return Objects.equals( subsystem, pk.subsystem ) &&
|
||||
Objects.equals( username, pk.username ) &&
|
||||
Objects.equals( registrationId, pk.registrationId );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( subsystem, username, registrationId );
|
||||
}
|
||||
}
|
||||
//end::identifiers-basic-idclass-generatedvalue-mapping-example[]
|
||||
}
|
|
@ -0,0 +1,201 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.identifier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.EmbeddedId;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class IdClassManyToOneTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
SystemUser.class,
|
||||
Subsystem.class
|
||||
};
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Subsystem subsystem = new Subsystem();
|
||||
subsystem.setId( "Hibernate Forum" );
|
||||
subsystem.setDescription( "Hibernate projects forum" );
|
||||
entityManager.persist( subsystem );
|
||||
|
||||
SystemUser systemUser = new SystemUser();
|
||||
systemUser.setId( new PK(
|
||||
subsystem,
|
||||
"vlad"
|
||||
) );
|
||||
systemUser.setName( "Vlad Mihalcea" );
|
||||
|
||||
entityManager.persist( systemUser );
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Subsystem subsystem = entityManager.find(
|
||||
Subsystem.class,
|
||||
"Hibernate Forum"
|
||||
);
|
||||
SystemUser systemUser = entityManager.find(
|
||||
SystemUser.class,
|
||||
new PK(
|
||||
subsystem,
|
||||
"vlad"
|
||||
)
|
||||
);
|
||||
|
||||
assertEquals( "Vlad Mihalcea", systemUser.getName() );
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
//tag::identifiers-basic-idclass-manytoone-mapping-example[]
|
||||
@Entity(name = "SystemUser")
|
||||
@IdClass( PK.class )
|
||||
public static class SystemUser {
|
||||
|
||||
@Id
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private Subsystem subsystem;
|
||||
|
||||
@Id
|
||||
private String username;
|
||||
|
||||
private String name;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-basic-idclass-manytoone-mapping-example[]
|
||||
|
||||
public PK getId() {
|
||||
return new PK(
|
||||
subsystem,
|
||||
username
|
||||
);
|
||||
}
|
||||
|
||||
public void setId(PK id) {
|
||||
this.subsystem = id.getSubsystem();
|
||||
this.username = id.getUsername();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
//tag::identifiers-basic-idclass-manytoone-mapping-example[]
|
||||
}
|
||||
|
||||
@Entity(name = "Subsystem")
|
||||
public static class Subsystem {
|
||||
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
private String description;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-basic-idclass-manytoone-mapping-example[]
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
//tag::identifiers-basic-idclass-manytoone-mapping-example[]
|
||||
}
|
||||
|
||||
public static class PK implements Serializable {
|
||||
|
||||
private Subsystem subsystem;
|
||||
|
||||
private String username;
|
||||
|
||||
public PK(Subsystem subsystem, String username) {
|
||||
this.subsystem = subsystem;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
private PK() {
|
||||
}
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-basic-idclass-manytoone-mapping-example[]
|
||||
|
||||
public Subsystem getSubsystem() {
|
||||
return subsystem;
|
||||
}
|
||||
|
||||
public void setSubsystem(Subsystem subsystem) {
|
||||
this.subsystem = subsystem;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
PK pk = (PK) o;
|
||||
return Objects.equals( subsystem, pk.subsystem ) &&
|
||||
Objects.equals( username, pk.username );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( subsystem, username );
|
||||
}
|
||||
//tag::identifiers-basic-idclass-manytoone-mapping-example[]
|
||||
}
|
||||
//end::identifiers-basic-idclass-manytoone-mapping-example[]
|
||||
}
|
|
@ -0,0 +1,168 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.identifier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class IdClassTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
SystemUser.class
|
||||
};
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
SystemUser systemUser = new SystemUser();
|
||||
systemUser.setId(
|
||||
new PK(
|
||||
"Hibernate Forum",
|
||||
"vlad"
|
||||
)
|
||||
);
|
||||
systemUser.setName( "Vlad Mihalcea" );
|
||||
|
||||
entityManager.persist( systemUser );
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
SystemUser systemUser = entityManager.find(
|
||||
SystemUser.class,
|
||||
new PK(
|
||||
"Hibernate Forum",
|
||||
"vlad"
|
||||
)
|
||||
);
|
||||
|
||||
assertEquals( "Vlad Mihalcea", systemUser.getName() );
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
//tag::identifiers-basic-idclass-mapping-example[]
|
||||
@Entity(name = "SystemUser")
|
||||
@IdClass( PK.class )
|
||||
public static class SystemUser {
|
||||
|
||||
@Id
|
||||
private String subsystem;
|
||||
|
||||
@Id
|
||||
private String username;
|
||||
|
||||
private String name;
|
||||
|
||||
public PK getId() {
|
||||
return new PK(
|
||||
subsystem,
|
||||
username
|
||||
);
|
||||
}
|
||||
|
||||
public void setId(PK id) {
|
||||
this.subsystem = id.getSubsystem();
|
||||
this.username = id.getUsername();
|
||||
}
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-basic-idclass-mapping-example[]
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
//tag::identifiers-basic-idclass-mapping-example[]
|
||||
}
|
||||
|
||||
public static class PK implements Serializable {
|
||||
|
||||
private String subsystem;
|
||||
|
||||
private String username;
|
||||
|
||||
public PK(String subsystem, String username) {
|
||||
this.subsystem = subsystem;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
private PK() {
|
||||
}
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-basic-idclass-mapping-example[]
|
||||
|
||||
public String getSubsystem() {
|
||||
return subsystem;
|
||||
}
|
||||
|
||||
public void setSubsystem(String subsystem) {
|
||||
this.subsystem = subsystem;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
//tag::identifiers-basic-idclass-mapping-example[]
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
PK pk = (PK) o;
|
||||
return Objects.equals( subsystem, pk.subsystem ) &&
|
||||
Objects.equals( username, pk.username );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( subsystem, username );
|
||||
}
|
||||
}
|
||||
//end::identifiers-basic-idclass-mapping-example[]
|
||||
}
|
|
@ -0,0 +1,219 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.identifier;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class IdManyToOneTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Book.class,
|
||||
Author.class,
|
||||
Publisher.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Author author = new Author();
|
||||
Publisher publisher = new Publisher();
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
author.setName( "Vlad Mihalcea" );
|
||||
entityManager.persist( author );
|
||||
|
||||
publisher.setName( "Amazon" );
|
||||
entityManager.persist( publisher );
|
||||
|
||||
Book book = new Book();
|
||||
book.setAuthor( author );
|
||||
book.setPublisher( publisher );
|
||||
book.setTitle( "High-Performance Java Persistence" );
|
||||
entityManager.persist( book );
|
||||
} );
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
//tag::identifiers-composite-id-fetching-example[]
|
||||
Book book = entityManager.find( Book.class, new Book(
|
||||
author,
|
||||
publisher,
|
||||
"High-Performance Java Persistence"
|
||||
) );
|
||||
|
||||
assertEquals( "Vlad Mihalcea", book.getAuthor().getName() );
|
||||
//end::identifiers-composite-id-fetching-example[]
|
||||
} );
|
||||
|
||||
}
|
||||
|
||||
//tag::identifiers-composite-id-mapping-example[]
|
||||
@Entity(name = "Book")
|
||||
public static class Book implements Serializable {
|
||||
|
||||
@Id
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private Author author;
|
||||
|
||||
@Id
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private Publisher publisher;
|
||||
|
||||
@Id
|
||||
private String title;
|
||||
|
||||
public Book(Author author, Publisher publisher, String title) {
|
||||
this.author = author;
|
||||
this.publisher = publisher;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
private Book() {
|
||||
}
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-composite-id-mapping-example[]
|
||||
|
||||
public Author getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(Author author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public Publisher getPublisher() {
|
||||
return publisher;
|
||||
}
|
||||
|
||||
public void setPublisher(Publisher publisher) {
|
||||
this.publisher = publisher;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
//tag::identifiers-composite-id-mapping-example[]
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
Book book = (Book) o;
|
||||
return Objects.equals( author, book.author ) &&
|
||||
Objects.equals( publisher, book.publisher ) &&
|
||||
Objects.equals( title, book.title );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( author, publisher, title );
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "Author")
|
||||
public static class Author implements Serializable {
|
||||
|
||||
@Id
|
||||
private String name;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-composite-id-mapping-example[]
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
//tag::identifiers-composite-id-mapping-example[]
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
Author author = (Author) o;
|
||||
return Objects.equals( name, author.name );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( name );
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "Publisher")
|
||||
public static class Publisher implements Serializable {
|
||||
|
||||
@Id
|
||||
private String name;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-composite-id-mapping-example[]
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
//tag::identifiers-composite-id-mapping-example[]
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
Publisher publisher = (Publisher) o;
|
||||
return Objects.equals( name, publisher.name );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash( name );
|
||||
}
|
||||
}
|
||||
//end::identifiers-composite-id-mapping-example[]
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.identifier;
|
||||
|
||||
import java.util.UUID;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.Parameter;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class UuidCustomGeneratedValueTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Book.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Book book = new Book();
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
book.setTitle( "High-Performance Java Persistence" );
|
||||
book.setAuthor( "Vlad Mihalcea" );
|
||||
|
||||
entityManager.persist( book );
|
||||
} );
|
||||
|
||||
assertNotNull( book.getId() );
|
||||
}
|
||||
|
||||
//tag::identifiers-generators-custom-uuid-mapping-example[]
|
||||
@Entity(name = "Book")
|
||||
public static class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue( generator = "custom-uuid" )
|
||||
@GenericGenerator(
|
||||
name = "custom-uuid",
|
||||
strategy = "org.hibernate.id.UUIDGenerator",
|
||||
parameters = {
|
||||
@Parameter(
|
||||
name = "uuid_gen_strategy_class",
|
||||
value = "org.hibernate.id.uuid.CustomVersionOneStrategy"
|
||||
)
|
||||
}
|
||||
)
|
||||
private UUID id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String author;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-generators-custom-uuid-mapping-example[]
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
//tag::identifiers-generators-custom-uuid-mapping-example[]
|
||||
}
|
||||
//end::identifiers-generators-custom-uuid-mapping-example[]
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.userguide.mapping.identifier;
|
||||
|
||||
import java.util.UUID;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author Vlad Mihalcea
|
||||
*/
|
||||
public class UuidGeneratedValueTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
Book.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Book book = new Book();
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
book.setTitle( "High-Performance Java Persistence" );
|
||||
book.setAuthor( "Vlad Mihalcea" );
|
||||
|
||||
entityManager.persist( book );
|
||||
} );
|
||||
|
||||
assertNotNull( book.getId() );
|
||||
}
|
||||
|
||||
//tag::identifiers-generators-uuid-mapping-example[]
|
||||
@Entity(name = "Book")
|
||||
public static class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private UUID id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String author;
|
||||
|
||||
//Getters and setters are omitted for brevity
|
||||
//end::identifiers-generators-uuid-mapping-example[]
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
//tag::identifiers-generators-uuid-mapping-example[]
|
||||
}
|
||||
//end::identifiers-generators-uuid-mapping-example[]
|
||||
}
|
Loading…
Reference in New Issue