JAVA-20163 Upgrade hibernate specific modules to JDK 11 (#13817)
* JAVA-20163 Migration hibernate-annotations * JAVA-20163 Migrate hibernate-queries * JAVA-20163 Migrating hibernate-mapping * JAVA-20163 rename reserved keywords, update inheritance example * JAVA-20163 Migrate hibernate-ogm module to the jdk 8 because hibernate-ogm doesn't support jakarta API * JAVA-20163 Migrate hibernate-enterprise module * JAVA-20163 Add update to HibernateExceptionUnitTest#whenQueryExecutedWithUnmappedEntity_thenMappingException * JAVA-20163 Set explicit version for hibernate 6.1.7.Final in the hibernate-queries module * JAVA-20163 Fix failed test with port that already exists (giving another port 8088) * JAVA-20163 Fix other location after changing the port * JAVA-20163 Remove duplicate Unit Test --------- Co-authored-by: n <noreplay@yahoo.com> Co-authored-by: Loredana Crusoveanu <lore.crusoveanu@gmail.com>
This commit is contained in:
@@ -76,13 +76,18 @@
|
||||
<version>${org.springframework.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.hypersistence</groupId>
|
||||
<artifactId>hypersistence-utils-hibernate-60</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
|
||||
<org.springframework.data.version>1.10.6.RELEASE</org.springframework.data.version>
|
||||
<hibernate-core.version>5.6.7.Final</hibernate-core.version>
|
||||
<org.springframework.version>6.0.6</org.springframework.version>
|
||||
<org.springframework.data.version>3.0.3</org.springframework.data.version>
|
||||
<hibernate-core.version>6.1.7.Final</hibernate-core.version>
|
||||
<maven.deploy.skip>true</maven.deploy.skip>
|
||||
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
||||
</properties>
|
||||
|
||||
@@ -2,9 +2,9 @@ package com.baeldung.hibernate.creationupdatetimestamp.model;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.type.IntegerType;
|
||||
import org.hibernate.type.StringType;
|
||||
import org.hibernate.type.Type;
|
||||
import org.hibernate.metamodel.spi.ValueAccess;
|
||||
import org.hibernate.usertype.CompositeUserType;
|
||||
import org.hibernate.usertype.UserType;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -14,74 +14,51 @@ import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Objects;
|
||||
|
||||
public class AddressType implements CompositeUserType {
|
||||
public class AddressType implements CompositeUserType<Address>, UserType<Address> {
|
||||
|
||||
@Override
|
||||
public String[] getPropertyNames() {
|
||||
return new String[]{"addressLine1", "addressLine2",
|
||||
"city", "country", "zipcode"};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type[] getPropertyTypes() {
|
||||
return new Type[]{StringType.INSTANCE, StringType.INSTANCE,
|
||||
StringType.INSTANCE, StringType.INSTANCE, IntegerType.INSTANCE};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getPropertyValue(Object component, int property) throws HibernateException {
|
||||
|
||||
Address empAdd = (Address) component;
|
||||
public Object getPropertyValue(Address component, int property) throws HibernateException {
|
||||
|
||||
switch (property) {
|
||||
case 0:
|
||||
return empAdd.getAddressLine1();
|
||||
return component.getAddressLine1();
|
||||
case 1:
|
||||
return empAdd.getAddressLine2();
|
||||
return component.getAddressLine2();
|
||||
case 2:
|
||||
return empAdd.getCity();
|
||||
return component.getCity();
|
||||
case 3:
|
||||
return empAdd.getCountry();
|
||||
return component.getCountry();
|
||||
case 4:
|
||||
return Integer.valueOf(empAdd.getZipCode());
|
||||
return component.getZipCode();
|
||||
default:
|
||||
throw new IllegalArgumentException(property +
|
||||
" is an invalid property index for class type " +
|
||||
component.getClass().getName());
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(property +
|
||||
" is an invalid property index for class type " +
|
||||
component.getClass().getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPropertyValue(Object component, int property, Object value) throws HibernateException {
|
||||
|
||||
Address empAdd = (Address) component;
|
||||
|
||||
switch (property) {
|
||||
case 0:
|
||||
empAdd.setAddressLine1((String) value);
|
||||
case 1:
|
||||
empAdd.setAddressLine2((String) value);
|
||||
case 2:
|
||||
empAdd.setCity((String) value);
|
||||
case 3:
|
||||
empAdd.setCountry((String) value);
|
||||
case 4:
|
||||
empAdd.setZipCode((Integer) value);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(property +
|
||||
" is an invalid property index for class type " +
|
||||
component.getClass().getName());
|
||||
|
||||
public Address instantiate(ValueAccess values, SessionFactoryImplementor sessionFactory) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class returnedClass() {
|
||||
public Class<?> embeddable() {
|
||||
return Address.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object x, Object y) throws HibernateException {
|
||||
public int getSqlType() {
|
||||
return Types.VARCHAR;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Address> returnedClass() {
|
||||
return Address.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Address x, Address y) {
|
||||
if (x == y)
|
||||
return true;
|
||||
|
||||
@@ -92,57 +69,52 @@ public class AddressType implements CompositeUserType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(Object x) throws HibernateException {
|
||||
public int hashCode(Address x) {
|
||||
return x.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
|
||||
|
||||
public Address nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
|
||||
Address empAdd = new Address();
|
||||
empAdd.setAddressLine1(rs.getString(names[0]));
|
||||
empAdd.setAddressLine1(rs.getString(position));
|
||||
|
||||
if (rs.wasNull())
|
||||
return null;
|
||||
|
||||
empAdd.setAddressLine2(rs.getString(names[1]));
|
||||
empAdd.setCity(rs.getString(names[2]));
|
||||
empAdd.setCountry(rs.getString(names[3]));
|
||||
empAdd.setZipCode(rs.getInt(names[4]));
|
||||
empAdd.setAddressLine2(rs.getString(position));
|
||||
empAdd.setCity(rs.getString(position));
|
||||
empAdd.setCountry(rs.getString(position));
|
||||
empAdd.setZipCode(rs.getInt(position));
|
||||
|
||||
return empAdd;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
|
||||
|
||||
public void nullSafeSet(PreparedStatement st, Address value, int index, SharedSessionContractImplementor session) throws SQLException {
|
||||
if (Objects.isNull(value))
|
||||
st.setNull(index, Types.VARCHAR);
|
||||
else {
|
||||
|
||||
Address empAdd = (Address) value;
|
||||
st.setString(index, empAdd.getAddressLine1());
|
||||
st.setString(index + 1, empAdd.getAddressLine2());
|
||||
st.setString(index + 2, empAdd.getCity());
|
||||
st.setString(index + 3, empAdd.getCountry());
|
||||
st.setInt(index + 4, empAdd.getZipCode());
|
||||
st.setString(index, value.getAddressLine1());
|
||||
st.setString(index + 1, value.getAddressLine2());
|
||||
st.setString(index + 2, value.getCity());
|
||||
st.setString(index + 3, value.getCountry());
|
||||
st.setInt(index + 4, value.getZipCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object deepCopy(Object value) throws HibernateException {
|
||||
|
||||
public Address deepCopy(Address value) {
|
||||
if (Objects.isNull(value))
|
||||
return null;
|
||||
|
||||
Address oldEmpAdd = (Address) value;
|
||||
Address newEmpAdd = new Address();
|
||||
|
||||
newEmpAdd.setAddressLine1(oldEmpAdd.getAddressLine1());
|
||||
newEmpAdd.setAddressLine2(oldEmpAdd.getAddressLine2());
|
||||
newEmpAdd.setCity(oldEmpAdd.getCity());
|
||||
newEmpAdd.setCountry(oldEmpAdd.getCountry());
|
||||
newEmpAdd.setZipCode(oldEmpAdd.getZipCode());
|
||||
newEmpAdd.setAddressLine1(value.getAddressLine1());
|
||||
newEmpAdd.setAddressLine2(value.getAddressLine2());
|
||||
newEmpAdd.setCity(value.getCity());
|
||||
newEmpAdd.setCountry(value.getCountry());
|
||||
newEmpAdd.setZipCode(value.getZipCode());
|
||||
|
||||
return newEmpAdd;
|
||||
}
|
||||
@@ -153,17 +125,27 @@ public class AddressType implements CompositeUserType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Serializable disassemble(Object value, SharedSessionContractImplementor session) throws HibernateException {
|
||||
public Serializable disassemble(Address value) {
|
||||
return (Serializable) deepCopy(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) throws HibernateException {
|
||||
return deepCopy(cached);
|
||||
public Address assemble(Serializable cached, Object owner) {
|
||||
return deepCopy((Address) cached);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner) throws HibernateException {
|
||||
return original;
|
||||
public Address replace(Address detached, Address managed, Object owner) {
|
||||
return detached;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInstance(Object object, SessionFactoryImplementor sessionFactory) {
|
||||
return CompositeUserType.super.isInstance(object, sessionFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSameClass(Object object, SessionFactoryImplementor sessionFactory) {
|
||||
return CompositeUserType.super.isSameClass(object, sessionFactory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import org.hibernate.type.LocalDateType;
|
||||
import org.hibernate.type.descriptor.WrapperOptions;
|
||||
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
|
||||
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
|
||||
import org.hibernate.type.descriptor.java.MutabilityPlan;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class LocalDateStringJavaDescriptor extends AbstractTypeDescriptor<LocalDate> {
|
||||
import io.hypersistence.utils.hibernate.type.array.internal.AbstractArrayTypeDescriptor;
|
||||
|
||||
public class LocalDateStringJavaDescriptor extends AbstractArrayTypeDescriptor<LocalDate> {
|
||||
|
||||
public static final LocalDateStringJavaDescriptor INSTANCE = new LocalDateStringJavaDescriptor();
|
||||
|
||||
@@ -18,12 +18,12 @@ public class LocalDateStringJavaDescriptor extends AbstractTypeDescriptor<LocalD
|
||||
|
||||
@Override
|
||||
public String toString(LocalDate value) {
|
||||
return LocalDateType.FORMATTER.format(value);
|
||||
return DateTimeFormatter.ISO_LOCAL_DATE.format(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate fromString(String string) {
|
||||
return LocalDate.from(LocalDateType.FORMATTER.parse(string));
|
||||
public LocalDate fromString(CharSequence string) {
|
||||
return LocalDate.from( DateTimeFormatter.ISO_LOCAL_DATE.parse(string));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -33,7 +33,7 @@ public class LocalDateStringJavaDescriptor extends AbstractTypeDescriptor<LocalD
|
||||
return null;
|
||||
|
||||
if (String.class.isAssignableFrom(type))
|
||||
return (X) LocalDateType.FORMATTER.format(value);
|
||||
return (X) DateTimeFormatter.ISO_LOCAL_DATE.format(value);
|
||||
|
||||
throw unknownUnwrap(type);
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public class LocalDateStringJavaDescriptor extends AbstractTypeDescriptor<LocalD
|
||||
return null;
|
||||
|
||||
if(String.class.isInstance(value))
|
||||
return LocalDate.from(LocalDateType.FORMATTER.parse((CharSequence) value));
|
||||
return LocalDate.from( DateTimeFormatter.ISO_LOCAL_DATE.parse((CharSequence) value));
|
||||
|
||||
throw unknownWrap(value.getClass());
|
||||
}
|
||||
|
||||
@@ -2,18 +2,16 @@ package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
|
||||
import org.hibernate.type.DiscriminatorType;
|
||||
import org.hibernate.type.descriptor.java.LocalDateJavaDescriptor;
|
||||
import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor;
|
||||
import org.hibernate.type.descriptor.jdbc.VarcharJdbcType;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class LocalDateStringType extends AbstractSingleColumnStandardBasicType<LocalDate> implements DiscriminatorType<LocalDate> {
|
||||
public class LocalDateStringType extends AbstractSingleColumnStandardBasicType<LocalDate> {
|
||||
|
||||
public static final LocalDateStringType INSTANCE = new LocalDateStringType();
|
||||
|
||||
public LocalDateStringType() {
|
||||
super(VarcharTypeDescriptor.INSTANCE, LocalDateStringJavaDescriptor.INSTANCE);
|
||||
super(VarcharJdbcType.INSTANCE, LocalDateStringJavaDescriptor.INSTANCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -21,14 +19,12 @@ public class LocalDateStringType extends AbstractSingleColumnStandardBasicType<L
|
||||
return "LocalDateString";
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate stringToObject(String xml) throws Exception {
|
||||
public LocalDate stringToObject(String xml) {
|
||||
return fromString(xml);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String objectToSQLString(LocalDate value, Dialect dialect) throws Exception {
|
||||
return '\'' + toString(value) + '\'';
|
||||
public String objectToSQLString(LocalDate value, Dialect dialect) {
|
||||
return '\'' + LocalDateStringJavaDescriptor.INSTANCE.toString(value) + '\'';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import org.hibernate.annotations.Columns;
|
||||
import org.hibernate.annotations.CompositeType;
|
||||
import org.hibernate.annotations.Parameter;
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.hibernate.annotations.TypeDef;
|
||||
import org.hibernate.usertype.UserTypeLegacyBridge;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.persistence.AttributeOverride;
|
||||
import jakarta.persistence.AttributeOverrides;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import java.time.LocalDate;
|
||||
|
||||
@TypeDef(name = "PhoneNumber",
|
||||
typeClass = PhoneNumberType.class,
|
||||
defaultForType = PhoneNumber.class)
|
||||
@Entity
|
||||
@Table(name = "OfficeEmployee")
|
||||
public class OfficeEmployee {
|
||||
@@ -24,25 +23,36 @@ public class OfficeEmployee {
|
||||
private long id;
|
||||
|
||||
@Column
|
||||
@Type(type = "LocalDateString")
|
||||
@Type(
|
||||
value = UserTypeLegacyBridge.class,
|
||||
parameters = @Parameter(name = UserTypeLegacyBridge.TYPE_NAME_PARAM_KEY, value = "LocalDateString")
|
||||
)
|
||||
private LocalDate dateOfJoining;
|
||||
|
||||
@Columns(columns = {@Column(name = "country_code"),
|
||||
@Column(name = "city_code"),
|
||||
@Column(name = "number")})
|
||||
@AttributeOverrides({
|
||||
@AttributeOverride(name = "countryCode", column = @Column(name = "country_code")),
|
||||
@AttributeOverride(name = "cityCode", column = @Column(name = "city_code")),
|
||||
@AttributeOverride(name = "number", column = @Column(name = "number"))
|
||||
})
|
||||
@Type(value = PhoneNumberType.class)
|
||||
private PhoneNumber employeeNumber;
|
||||
|
||||
@Columns(columns = {@Column(name = "address_line_1"),
|
||||
@Column(name = "address_line_2"),
|
||||
@Column(name = "city"), @Column(name = "country"),
|
||||
@Column(name = "zip_code")})
|
||||
@Type(type = "com.baeldung.hibernate.customtypes.AddressType")
|
||||
@CompositeType(value = com.baeldung.hibernate.customtypes.AddressType.class)
|
||||
@AttributeOverrides({
|
||||
@AttributeOverride(name = "addressLine1", column = @Column(name = "address_line_1")),
|
||||
@AttributeOverride(name = "addressLine2", column = @Column(name = "address_line_2")),
|
||||
@AttributeOverride(name = "city", column = @Column(name = "city")),
|
||||
@AttributeOverride(name = "country", column = @Column(name = "country")),
|
||||
@AttributeOverride(name = "zipCode", column = @Column(name = "zip_code"))
|
||||
})
|
||||
private Address empAddress;
|
||||
|
||||
@Type(type = "com.baeldung.hibernate.customtypes.SalaryType",
|
||||
@Type(value = com.baeldung.hibernate.customtypes.SalaryType.class,
|
||||
parameters = {@Parameter(name = "currency", value = "USD")})
|
||||
@Columns(columns = {@Column(name = "amount"),
|
||||
@Column(name = "currency")})
|
||||
@AttributeOverrides({
|
||||
@AttributeOverride(name = "amount", column = @Column(name = "amount")),
|
||||
@AttributeOverride(name = "currency", column = @Column(name = "currency"))
|
||||
})
|
||||
private Salary salary;
|
||||
|
||||
public Salary getSalary() {
|
||||
|
||||
@@ -2,11 +2,11 @@ package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public final class PhoneNumber {
|
||||
public class PhoneNumber {
|
||||
|
||||
private final int countryCode;
|
||||
private final int cityCode;
|
||||
private final int number;
|
||||
private int countryCode;
|
||||
private int cityCode;
|
||||
private int number;
|
||||
|
||||
public PhoneNumber(int countryCode, int cityCode, int number) {
|
||||
this.countryCode = countryCode;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.usertype.UserType;
|
||||
|
||||
@@ -11,11 +10,11 @@ import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PhoneNumberType implements UserType<PhoneNumber> {
|
||||
|
||||
public class PhoneNumberType implements UserType {
|
||||
@Override
|
||||
public int[] sqlTypes() {
|
||||
return new int[]{Types.INTEGER, Types.INTEGER, Types.INTEGER};
|
||||
public int getSqlType() {
|
||||
return Types.INTEGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -24,7 +23,7 @@ public class PhoneNumberType implements UserType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object x, Object y) throws HibernateException {
|
||||
public boolean equals(PhoneNumber x, PhoneNumber y) {
|
||||
if (x == y)
|
||||
return true;
|
||||
if (Objects.isNull(x) || Objects.isNull(y))
|
||||
@@ -34,48 +33,42 @@ public class PhoneNumberType implements UserType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(Object x) throws HibernateException {
|
||||
public int hashCode(PhoneNumber x) {
|
||||
return x.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
|
||||
int countryCode = rs.getInt(names[0]);
|
||||
public PhoneNumber nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
|
||||
int countryCode = rs.getInt(position);
|
||||
|
||||
if (rs.wasNull())
|
||||
return null;
|
||||
|
||||
int cityCode = rs.getInt(names[1]);
|
||||
int number = rs.getInt(names[2]);
|
||||
PhoneNumber employeeNumber = new PhoneNumber(countryCode, cityCode, number);
|
||||
int cityCode = rs.getInt(position);
|
||||
int number = rs.getInt(position);
|
||||
|
||||
return employeeNumber;
|
||||
return new PhoneNumber(countryCode, cityCode, number);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
|
||||
|
||||
public void nullSafeSet(PreparedStatement st, PhoneNumber value, int index, SharedSessionContractImplementor session) throws SQLException {
|
||||
if (Objects.isNull(value)) {
|
||||
st.setNull(index, Types.INTEGER);
|
||||
st.setNull(index+1, Types.INTEGER);
|
||||
st.setNull(index+2, Types.INTEGER);
|
||||
} else {
|
||||
PhoneNumber employeeNumber = (PhoneNumber) value;
|
||||
st.setInt(index,employeeNumber.getCountryCode());
|
||||
st.setInt(index+1,employeeNumber.getCityCode());
|
||||
st.setInt(index+2,employeeNumber.getNumber());
|
||||
st.setInt(index, value.getCountryCode());
|
||||
st.setInt(index+1, value.getCityCode());
|
||||
st.setInt(index+2, value.getNumber());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object deepCopy(Object value) throws HibernateException {
|
||||
public PhoneNumber deepCopy(PhoneNumber value) {
|
||||
if (Objects.isNull(value))
|
||||
return null;
|
||||
|
||||
PhoneNumber empNumber = (PhoneNumber) value;
|
||||
PhoneNumber newEmpNumber = new PhoneNumber(empNumber.getCountryCode(),empNumber.getCityCode(),empNumber.getNumber());
|
||||
|
||||
return newEmpNumber;
|
||||
return new PhoneNumber(value.getCountryCode(), value.getCityCode(), value.getNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,17 +77,17 @@ public class PhoneNumberType implements UserType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Serializable disassemble(Object value) throws HibernateException {
|
||||
public Serializable disassemble(PhoneNumber value) {
|
||||
return (Serializable) value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object assemble(Serializable cached, Object owner) throws HibernateException {
|
||||
return cached;
|
||||
public PhoneNumber assemble(Serializable cached, Object owner) {
|
||||
return (PhoneNumber) cached;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object replace(Object original, Object target, Object owner) throws HibernateException {
|
||||
return original;
|
||||
public PhoneNumber replace(PhoneNumber detached, PhoneNumber managed, Object owner) {
|
||||
return detached;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.type.LongType;
|
||||
import org.hibernate.type.StringType;
|
||||
import org.hibernate.type.Type;
|
||||
import org.hibernate.metamodel.spi.ValueAccess;
|
||||
import org.hibernate.usertype.CompositeUserType;
|
||||
import org.hibernate.usertype.DynamicParameterizedType;
|
||||
import org.hibernate.usertype.UserType;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -16,65 +16,47 @@ import java.sql.Types;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
|
||||
public class SalaryType implements CompositeUserType, DynamicParameterizedType {
|
||||
public class SalaryType implements UserType<Salary>, CompositeUserType<Salary>, DynamicParameterizedType {
|
||||
|
||||
private String localCurrency;
|
||||
|
||||
@Override
|
||||
public String[] getPropertyNames() {
|
||||
return new String[]{"amount", "currency"};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type[] getPropertyTypes() {
|
||||
return new Type[]{LongType.INSTANCE, StringType.INSTANCE};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getPropertyValue(Object component, int property) throws HibernateException {
|
||||
|
||||
Salary salary = (Salary) component;
|
||||
public Object getPropertyValue(Salary component, int property) throws HibernateException {
|
||||
|
||||
switch (property) {
|
||||
case 0:
|
||||
return salary.getAmount();
|
||||
return component.getAmount();
|
||||
case 1:
|
||||
return salary.getCurrency();
|
||||
return component.getCurrency();
|
||||
default:
|
||||
throw new IllegalArgumentException(property +
|
||||
" is an invalid property index for class type " +
|
||||
component.getClass().getName());
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(property +
|
||||
" is an invalid property index for class type " +
|
||||
component.getClass().getName());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setPropertyValue(Object component, int property, Object value) throws HibernateException {
|
||||
|
||||
Salary salary = (Salary) component;
|
||||
|
||||
switch (property) {
|
||||
case 0:
|
||||
salary.setAmount((Long) value);
|
||||
case 1:
|
||||
salary.setCurrency((String) value);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(property +
|
||||
" is an invalid property index for class type " +
|
||||
component.getClass().getName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class returnedClass() {
|
||||
public Salary instantiate(ValueAccess values, SessionFactoryImplementor sessionFactory) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> embeddable() {
|
||||
return Salary.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object x, Object y) throws HibernateException {
|
||||
public int getSqlType() {
|
||||
return Types.BIGINT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Salary> returnedClass() {
|
||||
return Salary.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Salary x, Salary y) {
|
||||
if (x == y)
|
||||
return true;
|
||||
|
||||
@@ -82,54 +64,48 @@ public class SalaryType implements CompositeUserType, DynamicParameterizedType {
|
||||
return false;
|
||||
|
||||
return x.equals(y);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode(Object x) throws HibernateException {
|
||||
public int hashCode(Salary x) {
|
||||
return x.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
|
||||
|
||||
public Salary nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
|
||||
Salary salary = new Salary();
|
||||
salary.setAmount(rs.getLong(names[0]));
|
||||
salary.setAmount(rs.getLong(position));
|
||||
|
||||
if (rs.wasNull())
|
||||
return null;
|
||||
|
||||
salary.setCurrency(rs.getString(names[1]));
|
||||
salary.setCurrency(rs.getString(position));
|
||||
|
||||
return salary;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
|
||||
|
||||
|
||||
public void nullSafeSet(PreparedStatement st, Salary value, int index, SharedSessionContractImplementor session) throws SQLException {
|
||||
if (Objects.isNull(value))
|
||||
st.setNull(index, Types.BIGINT);
|
||||
else {
|
||||
|
||||
Salary salary = (Salary) value;
|
||||
st.setLong(index, SalaryCurrencyConvertor.convert(salary.getAmount(),
|
||||
salary.getCurrency(), localCurrency));
|
||||
st.setString(index + 1, salary.getCurrency());
|
||||
st.setLong(index, SalaryCurrencyConvertor.convert(
|
||||
value.getAmount(),
|
||||
value.getCurrency(), localCurrency));
|
||||
st.setString(index + 1, value.getCurrency());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object deepCopy(Object value) throws HibernateException {
|
||||
|
||||
public Salary deepCopy(Salary value) {
|
||||
if (Objects.isNull(value))
|
||||
return null;
|
||||
|
||||
Salary oldSal = (Salary) value;
|
||||
Salary newSal = new Salary();
|
||||
|
||||
newSal.setAmount(oldSal.getAmount());
|
||||
newSal.setCurrency(oldSal.getCurrency());
|
||||
newSal.setAmount(value.getAmount());
|
||||
newSal.setCurrency(value.getCurrency());
|
||||
|
||||
return newSal;
|
||||
}
|
||||
@@ -140,18 +116,18 @@ public class SalaryType implements CompositeUserType, DynamicParameterizedType {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Serializable disassemble(Object value, SharedSessionContractImplementor session) throws HibernateException {
|
||||
public Serializable disassemble(Salary value) {
|
||||
return (Serializable) deepCopy(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) throws HibernateException {
|
||||
return deepCopy(cached);
|
||||
public Salary assemble(Serializable cached, Object owner) {
|
||||
return deepCopy((Salary) cached);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner) throws HibernateException {
|
||||
return original;
|
||||
public Salary replace(Salary detached, Salary managed, Object owner) {
|
||||
return detached;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -4,7 +4,7 @@ import org.hibernate.annotations.Cascade;
|
||||
import org.hibernate.annotations.CascadeType;
|
||||
import org.hibernate.annotations.Immutable;
|
||||
|
||||
import javax.persistence.*;
|
||||
import jakarta.persistence.*;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.baeldung.hibernate.immutable.entities;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
import org.hibernate.annotations.Immutable;
|
||||
|
||||
import javax.persistence.*;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Immutable
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.baeldung.hibernate.joincolumn;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Email {
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.baeldung.hibernate.joincolumn;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinColumns;
|
||||
import javax.persistence.ManyToOne;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.JoinColumns;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Office {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.baeldung.hibernate.joincolumn;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class OfficeAddress {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.baeldung.hibernate.joincolumn;
|
||||
|
||||
import java.util.List;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
|
||||
@Entity
|
||||
public class OfficialEmployee {
|
||||
|
||||
@@ -3,12 +3,12 @@ package com.baeldung.hibernate.lazycollection.model;
|
||||
import org.hibernate.annotations.LazyCollection;
|
||||
import org.hibernate.annotations.LazyCollectionOption;
|
||||
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OrderColumn;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OrderColumn;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Entity;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.baeldung.hibernate.lazycollection.model;
|
||||
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OrderColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class Employee {
|
||||
|
||||
@@ -43,8 +43,8 @@ public class HibernateAnnotationUtil {
|
||||
return metadata.buildSessionFactory();
|
||||
}
|
||||
|
||||
private static Map<String, String> dbSettings() {
|
||||
Map<String, String> dbSettings = new HashMap<>();
|
||||
private static Map<String, Object> dbSettings() {
|
||||
Map<String, Object> dbSettings = new HashMap<>();
|
||||
dbSettings.put(Environment.URL, "jdbc:h2:mem:spring_hibernate_one_to_many");
|
||||
dbSettings.put(Environment.USER, "sa");
|
||||
dbSettings.put(Environment.PASS, "");
|
||||
|
||||
@@ -2,13 +2,13 @@ package com.baeldung.hibernate.oneToMany.model;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "CART")
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.baeldung.hibernate.oneToMany.model;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "CARTOIO")
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.baeldung.hibernate.oneToMany.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "ITEMS")
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.baeldung.hibernate.oneToMany.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "ITEMSOIO")
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.baeldung.hibernate.pojo;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Entity
|
||||
|
||||
@@ -3,10 +3,10 @@ package com.baeldung.hibernate.wherejointable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
|
||||
@Entity(name = "e_group")
|
||||
public class Group {
|
||||
|
||||
@@ -3,12 +3,12 @@ package com.baeldung.hibernate.wherejointable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.JoinTable;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
|
||||
import org.hibernate.annotations.WhereJoinTable;
|
||||
|
||||
|
||||
@@ -2,11 +2,11 @@ package com.baeldung.hibernate.wherejointable;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity(name = "r_user_group")
|
||||
public class UserGroupRelation implements Serializable {
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
<description>Hibernate EntityManager Demo</description>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
|
||||
<property name="hibernate.hbm2ddl.auto" value="update"/>
|
||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/moviecatalog"/>
|
||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||
<property name="javax.persistence.jdbc.password" value="root"/>
|
||||
<property name="jakarta.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
|
||||
<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/moviecatalog"/>
|
||||
<property name="jakarta.persistence.jdbc.user" value="root"/>
|
||||
<property name="jakarta.persistence.jdbc.password" value="root"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.hibernate.creationupdatetimestamp;
|
||||
|
||||
import static org.hibernate.FlushMode.MANUAL;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
@@ -71,22 +72,22 @@ class HibernateCreationUpdateTimestampIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCreatingEntity_ThenCreatedOnAndLastUpdatedOnAreNotEqual() {
|
||||
void whenCreatingEntity_ThenCreatedOnAndLastUpdatedOnAreEqual() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
Book book = new Book();
|
||||
|
||||
session.save(book);
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
assertNotEquals(book.getCreatedOn(), book.getLastUpdatedOn());
|
||||
assertEquals(book.getCreatedOn(), book.getLastUpdatedOn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUpdatingEntity_ThenLastUpdatedOnIsUpdatedAndCreatedOnStaysTheSame() {
|
||||
session = sessionFactory.openSession();
|
||||
session.setHibernateFlushMode(MANUAL);
|
||||
session.beginTransaction();
|
||||
Book book = new Book();
|
||||
session.save(book);
|
||||
@@ -96,8 +97,9 @@ class HibernateCreationUpdateTimestampIntegrationTest {
|
||||
|
||||
String newName = "newName";
|
||||
book.setTitle(newName);
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
session.save(book);
|
||||
session.flush();
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
Instant createdOnAfterUpdate = book.getCreatedOn();
|
||||
Instant lastUpdatedOnAfterUpdate = book.getLastUpdatedOn();
|
||||
|
||||
@@ -6,10 +6,9 @@ import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.TypedQuery;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -76,7 +75,7 @@ public class HibernateCustomTypesIntegrationTest {
|
||||
doInHibernate(this::sessionFactory, session -> {
|
||||
session.save(e);
|
||||
|
||||
TypedQuery<OfficeEmployee> query = session.createQuery("FROM OfficeEmployee OE WHERE OE.empAddress.zipcode = :pinCode", OfficeEmployee.class);
|
||||
TypedQuery<OfficeEmployee> query = session.createQuery("FROM OfficeEmployee OE WHERE OE.empAddress.zipCode = :pinCode", OfficeEmployee.class);
|
||||
query.setParameter("pinCode",100);
|
||||
int size = query.getResultList().size();
|
||||
|
||||
@@ -100,8 +99,8 @@ public class HibernateCustomTypesIntegrationTest {
|
||||
return metadata.buildSessionFactory();
|
||||
}
|
||||
|
||||
private static Map<String, String> getProperties() {
|
||||
Map<String, String> dbSettings = new HashMap<>();
|
||||
private static Map<String, Object> getProperties() {
|
||||
Map<String, Object> dbSettings = new HashMap<>();
|
||||
dbSettings.put(Environment.URL, "jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1");
|
||||
dbSettings.put(Environment.USER, "sa");
|
||||
dbSettings.put(Environment.PASS, "");
|
||||
|
||||
@@ -8,7 +8,7 @@ import org.hibernate.Session;
|
||||
import org.junit.*;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import jakarta.persistence.PersistenceException;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
|
||||
@@ -78,8 +78,8 @@ public class JoinColumnIntegrationTest {
|
||||
return metadata.buildSessionFactory();
|
||||
}
|
||||
|
||||
private static Map<String, String> getProperties() {
|
||||
Map<String, String> dbSettings = new HashMap<>();
|
||||
private static Map<String, Object> getProperties() {
|
||||
Map<String, Object> dbSettings = new HashMap<>();
|
||||
dbSettings.put(Environment.URL, "jdbc:h2:mem:mydbJoinColumn;DB_CLOSE_DELAY=-1");
|
||||
dbSettings.put(Environment.USER, "sa");
|
||||
dbSettings.put(Environment.PASS, "");
|
||||
|
||||
Reference in New Issue
Block a user