diff --git a/persistence-modules/hibernate5-annotations/README.md b/persistence-modules/hibernate5-annotations/README.md new file mode 100644 index 0000000000..da3c5ad1fd --- /dev/null +++ b/persistence-modules/hibernate5-annotations/README.md @@ -0,0 +1,10 @@ +## Hibernate 5 + +This module contains articles about Hibernate 5. + +### Relevant Articles: +- [Custom Types in Hibernate and the @Type Annotation](https://www.baeldung.com/hibernate-custom-types) +- [@Immutable in Hibernate](http://www.baeldung.com/hibernate-immutable) +- [@JoinColumn Annotation Explained](https://www.baeldung.com/jpa-join-column) +- [Difference Between @JoinColumn and mappedBy](https://www.baeldung.com/jpa-joincolumn-vs-mappedby) + diff --git a/persistence-modules/hibernate5-annotations/pom.xml b/persistence-modules/hibernate5-annotations/pom.xml new file mode 100644 index 0000000000..ab242333fe --- /dev/null +++ b/persistence-modules/hibernate5-annotations/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + hibernate5-annotations + 0.1-SNAPSHOT + hibernate5-annotations + jar + Hibernate tutorial illustrating the use of named parameters + + + com.baeldung + persistence-modules + 1.0.0-SNAPSHOT + + + + + org.hibernate + hibernate-core + ${hibernate-core.version} + + + + com.h2database + h2 + ${h2.version} + + + + org.apache.commons + commons-lang3 + ${commons.lang3.version} + + + + org.hibernate + hibernate-testing + ${hibernate-core.version} + + + + org.hibernate + hibernate-spatial + ${hibernate-core.version} + + + org.opengeo + geodb + ${geodb.version} + + + + + + geodb-repo + GeoDB repository + http://repo.boundlessgeo.com/main/ + + + + + 5.4.7.Final + 1.4.200 + 3.8.1 + true + 2.1.7.RELEASE + 5.4.7.Final + 1.4.200 + 3.8.1 + 0.9 + + + diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/HibernateUtil.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/HibernateUtil.java new file mode 100644 index 0000000000..afe2aeac89 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/HibernateUtil.java @@ -0,0 +1,73 @@ +package com.baeldung.hibernate; + +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Properties; + +import org.apache.commons.lang3.StringUtils; +import org.hibernate.SessionFactory; +import org.hibernate.boot.Metadata; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.service.ServiceRegistry; + +import com.baeldung.hibernate.joincolumn.Email; +import com.baeldung.hibernate.joincolumn.Office; +import com.baeldung.hibernate.joincolumn.OfficeAddress; + +public class HibernateUtil { + private static String PROPERTY_FILE_NAME; + + public static SessionFactory getSessionFactory() throws IOException { + return getSessionFactory(null); + } + + public static SessionFactory getSessionFactory(String propertyFileName) throws IOException { + PROPERTY_FILE_NAME = propertyFileName; + ServiceRegistry serviceRegistry = configureServiceRegistry(); + return makeSessionFactory(serviceRegistry); + } + + public static SessionFactory getSessionFactoryByProperties(Properties properties) throws IOException { + ServiceRegistry serviceRegistry = configureServiceRegistry(properties); + return makeSessionFactory(serviceRegistry); + } + + private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry) { + MetadataSources metadataSources = new MetadataSources(serviceRegistry); + + metadataSources.addPackage("com.baeldung.hibernate.pojo"); + metadataSources.addAnnotatedClass(com.baeldung.hibernate.joincolumn.OfficialEmployee.class); + metadataSources.addAnnotatedClass(Email.class); + metadataSources.addAnnotatedClass(Office.class); + metadataSources.addAnnotatedClass(OfficeAddress.class); + + Metadata metadata = metadataSources.getMetadataBuilder() + .build(); + + return metadata.getSessionFactoryBuilder() + .build(); + + } + + private static ServiceRegistry configureServiceRegistry() throws IOException { + return configureServiceRegistry(getProperties()); + } + + private static ServiceRegistry configureServiceRegistry(Properties properties) throws IOException { + return new StandardServiceRegistryBuilder().applySettings(properties) + .build(); + } + + public static Properties getProperties() throws IOException { + Properties properties = new Properties(); + URL propertiesURL = Thread.currentThread() + .getContextClassLoader() + .getResource(StringUtils.defaultString(PROPERTY_FILE_NAME, "hibernate.properties")); + try (FileInputStream inputStream = new FileInputStream(propertiesURL.getFile())) { + properties.load(inputStream); + } + return properties; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/UnsupportedTenancyException.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/UnsupportedTenancyException.java new file mode 100644 index 0000000000..99d9505ea3 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/UnsupportedTenancyException.java @@ -0,0 +1,8 @@ +package com.baeldung.hibernate; + +public class UnsupportedTenancyException extends Exception { + public UnsupportedTenancyException (String message) { + super(message); + } + +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/Address.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/Address.java new file mode 100644 index 0000000000..d559e5a6c2 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/Address.java @@ -0,0 +1,69 @@ +package com.baeldung.hibernate.customtypes; + +import java.util.Objects; + +public class Address { + + private String addressLine1; + private String addressLine2; + private String city; + private String country; + private int zipCode; + + public String getAddressLine1() { + return addressLine1; + } + + public String getAddressLine2() { + return addressLine2; + } + + public String getCity() { + return city; + } + + public String getCountry() { + return country; + } + + public int getZipCode() { + return zipCode; + } + + public void setAddressLine1(String addressLine1) { + this.addressLine1 = addressLine1; + } + + public void setAddressLine2(String addressLine2) { + this.addressLine2 = addressLine2; + } + + public void setCity(String city) { + this.city = city; + } + + public void setCountry(String country) { + this.country = country; + } + + public void setZipCode(int zipCode) { + this.zipCode = zipCode; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Address address = (Address) o; + return zipCode == address.zipCode && + Objects.equals(addressLine1, address.addressLine1) && + Objects.equals(addressLine2, address.addressLine2) && + Objects.equals(city, address.city) && + Objects.equals(country, address.country); + } + + @Override + public int hashCode() { + return Objects.hash(addressLine1, addressLine2, city, country, zipCode); + } +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/AddressType.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/AddressType.java new file mode 100644 index 0000000000..c10c67df9a --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/AddressType.java @@ -0,0 +1,169 @@ +package com.baeldung.hibernate.customtypes; + +import org.hibernate.HibernateException; +import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.type.IntegerType; +import org.hibernate.type.StringType; +import org.hibernate.type.Type; +import org.hibernate.usertype.CompositeUserType; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import java.util.Objects; + +public class AddressType implements CompositeUserType { + + @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; + + switch (property) { + case 0: + return empAdd.getAddressLine1(); + case 1: + return empAdd.getAddressLine2(); + case 2: + return empAdd.getCity(); + case 3: + return empAdd.getCountry(); + case 4: + return Integer.valueOf(empAdd.getZipCode()); + } + + 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()); + + } + + @Override + public Class returnedClass() { + return Address.class; + } + + @Override + public boolean equals(Object x, Object y) throws HibernateException { + if (x == y) + return true; + + if (Objects.isNull(x) || Objects.isNull(y)) + return false; + + return x.equals(y); + } + + @Override + public int hashCode(Object x) throws HibernateException { + return x.hashCode(); + } + + @Override + public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { + + Address empAdd = new Address(); + empAdd.setAddressLine1(rs.getString(names[0])); + + 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])); + + return empAdd; + } + + @Override + public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, 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()); + } + } + + @Override + public Object deepCopy(Object value) throws HibernateException { + + 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()); + + return newEmpAdd; + } + + @Override + public boolean isMutable() { + return true; + } + + @Override + public Serializable disassemble(Object value, SharedSessionContractImplementor session) throws HibernateException { + return (Serializable) deepCopy(value); + } + + @Override + public Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) throws HibernateException { + return deepCopy(cached); + } + + @Override + public Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner) throws HibernateException { + return original; + } +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/LocalDateStringJavaDescriptor.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/LocalDateStringJavaDescriptor.java new file mode 100644 index 0000000000..56be9e693f --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/LocalDateStringJavaDescriptor.java @@ -0,0 +1,51 @@ +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; + +public class LocalDateStringJavaDescriptor extends AbstractTypeDescriptor { + + public static final LocalDateStringJavaDescriptor INSTANCE = new LocalDateStringJavaDescriptor(); + + public LocalDateStringJavaDescriptor() { + super(LocalDate.class, ImmutableMutabilityPlan.INSTANCE); + } + + @Override + public String toString(LocalDate value) { + return LocalDateType.FORMATTER.format(value); + } + + @Override + public LocalDate fromString(String string) { + return LocalDate.from(LocalDateType.FORMATTER.parse(string)); + } + + @Override + public X unwrap(LocalDate value, Class type, WrapperOptions options) { + + if (value == null) + return null; + + if (String.class.isAssignableFrom(type)) + return (X) LocalDateType.FORMATTER.format(value); + + throw unknownUnwrap(type); + } + + @Override + public LocalDate wrap(X value, WrapperOptions options) { + if (value == null) + return null; + + if(String.class.isInstance(value)) + return LocalDate.from(LocalDateType.FORMATTER.parse((CharSequence) value)); + + throw unknownWrap(value.getClass()); + } +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/LocalDateStringType.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/LocalDateStringType.java new file mode 100644 index 0000000000..c8d37073e8 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/LocalDateStringType.java @@ -0,0 +1,34 @@ +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 java.time.LocalDate; + +public class LocalDateStringType extends AbstractSingleColumnStandardBasicType implements DiscriminatorType { + + public static final LocalDateStringType INSTANCE = new LocalDateStringType(); + + public LocalDateStringType() { + super(VarcharTypeDescriptor.INSTANCE, LocalDateStringJavaDescriptor.INSTANCE); + } + + @Override + public String getName() { + return "LocalDateString"; + } + + @Override + public LocalDate stringToObject(String xml) throws Exception { + return fromString(xml); + } + + @Override + public String objectToSQLString(LocalDate value, Dialect dialect) throws Exception { + return '\'' + toString(value) + '\''; + } + +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/OfficeEmployee.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/OfficeEmployee.java new file mode 100644 index 0000000000..3ca06e4316 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/OfficeEmployee.java @@ -0,0 +1,88 @@ +package com.baeldung.hibernate.customtypes; + +import com.baeldung.hibernate.pojo.Phone; +import org.hibernate.annotations.Columns; +import org.hibernate.annotations.Parameter; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import java.time.LocalDate; + +@TypeDef(name = "PhoneNumber", + typeClass = PhoneNumberType.class, + defaultForType = PhoneNumber.class) +@Entity +@Table(name = "OfficeEmployee") +public class OfficeEmployee { + + @Id + @GeneratedValue + private long id; + + @Column + @Type(type = "LocalDateString") + private LocalDate dateOfJoining; + + @Columns(columns = {@Column(name = "country_code"), + @Column(name = "city_code"), + @Column(name = "number")}) + 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") + private Address empAddress; + + @Type(type = "com.baeldung.hibernate.customtypes.SalaryType", + parameters = {@Parameter(name = "currency", value = "USD")}) + @Columns(columns = {@Column(name = "amount"), + @Column(name = "currency")}) + private Salary salary; + + public Salary getSalary() { + return salary; + } + + public void setSalary(Salary salary) { + this.salary = salary; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public LocalDate getDateOfJoining() { + return dateOfJoining; + } + + public void setDateOfJoining(LocalDate dateOfJoining) { + this.dateOfJoining = dateOfJoining; + } + + public PhoneNumber getEmployeeNumber() { + return employeeNumber; + } + + public void setEmployeeNumber(PhoneNumber employeeNumber) { + this.employeeNumber = employeeNumber; + } + + public Address getEmpAddress() { + return empAddress; + } + + public void setEmpAddress(Address empAddress) { + this.empAddress = empAddress; + } +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/PhoneNumber.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/PhoneNumber.java new file mode 100644 index 0000000000..0be6cbc910 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/PhoneNumber.java @@ -0,0 +1,43 @@ +package com.baeldung.hibernate.customtypes; + +import java.util.Objects; + +public final class PhoneNumber { + + private final int countryCode; + private final int cityCode; + private final int number; + + public PhoneNumber(int countryCode, int cityCode, int number) { + this.countryCode = countryCode; + this.cityCode = cityCode; + this.number = number; + } + + public int getCountryCode() { + return countryCode; + } + + public int getCityCode() { + return cityCode; + } + + public int getNumber() { + return number; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PhoneNumber that = (PhoneNumber) o; + return countryCode == that.countryCode && + cityCode == that.cityCode && + number == that.number; + } + + @Override + public int hashCode() { + return Objects.hash(countryCode, cityCode, number); + } +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/PhoneNumberType.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/PhoneNumberType.java new file mode 100644 index 0000000000..9f09352bec --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/PhoneNumberType.java @@ -0,0 +1,98 @@ +package com.baeldung.hibernate.customtypes; + +import org.hibernate.HibernateException; +import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.usertype.UserType; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import java.util.Objects; + + +public class PhoneNumberType implements UserType { + @Override + public int[] sqlTypes() { + return new int[]{Types.INTEGER, Types.INTEGER, Types.INTEGER}; + } + + @Override + public Class returnedClass() { + return PhoneNumber.class; + } + + @Override + public boolean equals(Object x, Object y) throws HibernateException { + if (x == y) + return true; + if (Objects.isNull(x) || Objects.isNull(y)) + return false; + + return x.equals(y); + } + + @Override + public int hashCode(Object x) throws HibernateException { + return x.hashCode(); + } + + @Override + public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { + int countryCode = rs.getInt(names[0]); + + if (rs.wasNull()) + return null; + + int cityCode = rs.getInt(names[1]); + int number = rs.getInt(names[2]); + PhoneNumber employeeNumber = new PhoneNumber(countryCode, cityCode, number); + + return employeeNumber; + } + + @Override + public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { + + if (Objects.isNull(value)) { + st.setNull(index, Types.INTEGER); + } else { + PhoneNumber employeeNumber = (PhoneNumber) value; + st.setInt(index,employeeNumber.getCountryCode()); + st.setInt(index+1,employeeNumber.getCityCode()); + st.setInt(index+2,employeeNumber.getNumber()); + } + } + + @Override + public Object deepCopy(Object value) throws HibernateException { + if (Objects.isNull(value)) + return null; + + PhoneNumber empNumber = (PhoneNumber) value; + PhoneNumber newEmpNumber = new PhoneNumber(empNumber.getCountryCode(),empNumber.getCityCode(),empNumber.getNumber()); + + return newEmpNumber; + } + + @Override + public boolean isMutable() { + return false; + } + + @Override + public Serializable disassemble(Object value) throws HibernateException { + return (Serializable) value; + } + + @Override + public Object assemble(Serializable cached, Object owner) throws HibernateException { + return cached; + } + + @Override + public Object replace(Object original, Object target, Object owner) throws HibernateException { + return original; + } +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/Salary.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/Salary.java new file mode 100644 index 0000000000..f9a7ac5902 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/Salary.java @@ -0,0 +1,39 @@ +package com.baeldung.hibernate.customtypes; + +import java.util.Objects; + +public class Salary { + + private Long amount; + private String currency; + + public Long getAmount() { + return amount; + } + + public void setAmount(Long amount) { + this.amount = amount; + } + + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Salary salary = (Salary) o; + return Objects.equals(amount, salary.amount) && + Objects.equals(currency, salary.currency); + } + + @Override + public int hashCode() { + return Objects.hash(amount, currency); + } +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/SalaryCurrencyConvertor.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/SalaryCurrencyConvertor.java new file mode 100644 index 0000000000..340c697c11 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/SalaryCurrencyConvertor.java @@ -0,0 +1,15 @@ +package com.baeldung.hibernate.customtypes; + +public class SalaryCurrencyConvertor { + + public static Long convert(Long amount, String oldCurr, String newCurr){ + if (newCurr.equalsIgnoreCase(oldCurr)) + return amount; + + return convertTo(); + } + + private static Long convertTo() { + return 10L; + } +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/SalaryType.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/SalaryType.java new file mode 100644 index 0000000000..266b85140b --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/customtypes/SalaryType.java @@ -0,0 +1,161 @@ +package com.baeldung.hibernate.customtypes; + +import org.hibernate.HibernateException; +import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.type.LongType; +import org.hibernate.type.StringType; +import org.hibernate.type.Type; +import org.hibernate.usertype.CompositeUserType; +import org.hibernate.usertype.DynamicParameterizedType; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import java.util.Objects; +import java.util.Properties; + +public class SalaryType implements CompositeUserType, 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; + + switch (property) { + case 0: + return salary.getAmount(); + case 1: + return salary.getCurrency(); + } + + 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() { + return Salary.class; + } + + @Override + public boolean equals(Object x, Object y) throws HibernateException { + + if (x == y) + return true; + + if (Objects.isNull(x) || Objects.isNull(y)) + return false; + + return x.equals(y); + + } + + @Override + public int hashCode(Object x) throws HibernateException { + return x.hashCode(); + } + + @Override + public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { + + Salary salary = new Salary(); + salary.setAmount(rs.getLong(names[0])); + + if (rs.wasNull()) + return null; + + salary.setCurrency(rs.getString(names[1])); + + return salary; + } + + @Override + public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, 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()); + } + } + + @Override + public Object deepCopy(Object value) throws HibernateException { + + if (Objects.isNull(value)) + return null; + + Salary oldSal = (Salary) value; + Salary newSal = new Salary(); + + newSal.setAmount(oldSal.getAmount()); + newSal.setCurrency(oldSal.getCurrency()); + + return newSal; + } + + @Override + public boolean isMutable() { + return true; + } + + @Override + public Serializable disassemble(Object value, SharedSessionContractImplementor session) throws HibernateException { + return (Serializable) deepCopy(value); + } + + @Override + public Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) throws HibernateException { + return deepCopy(cached); + } + + @Override + public Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner) throws HibernateException { + return original; + } + + @Override + public void setParameterValues(Properties parameters) { + this.localCurrency = parameters.getProperty("currency"); + } +} diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/Email.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/Email.java new file mode 100644 index 0000000000..df07c3cf69 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/Email.java @@ -0,0 +1,47 @@ +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; + +@Entity +public class Email { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String address; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "employee_id") + private OfficialEmployee employee; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public OfficialEmployee getEmployee() { + return employee; + } + + public void setEmployee(OfficialEmployee employee) { + this.employee = employee; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/Office.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/Office.java new file mode 100644 index 0000000000..9940577761 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/Office.java @@ -0,0 +1,41 @@ +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; + +@Entity +public class Office { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumns({ + @JoinColumn(name="ADDR_ID", referencedColumnName="ID"), + @JoinColumn(name="ADDR_ZIP", referencedColumnName="ZIP") + }) + private OfficeAddress address; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public OfficeAddress getAddress() { + return address; + } + + public void setAddress(OfficeAddress address) { + this.address = address; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/OfficeAddress.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/OfficeAddress.java new file mode 100644 index 0000000000..cc723db6a2 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/OfficeAddress.java @@ -0,0 +1,34 @@ +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; + +@Entity +public class OfficeAddress { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name = "ZIP") + private String zipCode; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getZipCode() { + return zipCode; + } + + public void setZipCode(String zipCode) { + this.zipCode = zipCode; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/OfficialEmployee.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/OfficialEmployee.java new file mode 100644 index 0000000000..49c63c7578 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/joincolumn/OfficialEmployee.java @@ -0,0 +1,36 @@ +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; + +@Entity +public class OfficialEmployee { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee") + private List emails; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public List getEmails() { + return emails; + } + + public void setEmails(List emails) { + this.emails = emails; + } +} \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/pojo/Phone.java b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/pojo/Phone.java new file mode 100644 index 0000000000..d923bda5de --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/java/com/baeldung/hibernate/pojo/Phone.java @@ -0,0 +1,50 @@ +package com.baeldung.hibernate.pojo; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.io.Serializable; + +@Entity +public class Phone implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + private boolean deleted; + + private String number; + + public Phone() { + } + + public Phone(String number) { + this.number = number; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public boolean isDeleted() { + return deleted; + } + + public void setDeleted(boolean deleted) { + this.deleted = deleted; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } +} diff --git a/persistence-modules/hibernate5-annotations/src/main/resources/META-INF/persistence.xml b/persistence-modules/hibernate5-annotations/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..474eeb7a44 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,18 @@ + + + + Hibernate EntityManager Demo + true + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/main/resources/logback.xml b/persistence-modules/hibernate5-annotations/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/test/java/com/baeldung/hibernate/customtypes/HibernateCustomTypesManualTest.java b/persistence-modules/hibernate5-annotations/src/test/java/com/baeldung/hibernate/customtypes/HibernateCustomTypesManualTest.java new file mode 100644 index 0000000000..63dc6330be --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/test/java/com/baeldung/hibernate/customtypes/HibernateCustomTypesManualTest.java @@ -0,0 +1,90 @@ +package com.baeldung.hibernate.customtypes; + +import com.baeldung.hibernate.HibernateUtil; +import org.hibernate.SessionFactory; +import org.hibernate.query.Query; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.time.LocalDate; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; + +public class HibernateCustomTypesManualTest { + + @Test + public void givenEmployee_whenSavedWithCustomTypes_thenEntityIsSaved() throws IOException { + + final OfficeEmployee e = new OfficeEmployee(); + e.setDateOfJoining(LocalDate.now()); + + PhoneNumber number = new PhoneNumber(1, 222, 8781902); + e.setEmployeeNumber(number); + + Address empAdd = new Address(); + empAdd.setAddressLine1("Street"); + empAdd.setAddressLine2("Area"); + empAdd.setCity("City"); + empAdd.setCountry("Country"); + empAdd.setZipCode(100); + + e.setEmpAddress(empAdd); + + Salary empSalary = new Salary(); + empSalary.setAmount(1000L); + empSalary.setCurrency("USD"); + e.setSalary(empSalary); + + doInHibernate(this::sessionFactory, session -> { + session.save(e); + boolean contains = session.contains(e); + Assert.assertTrue(contains); + }); + + } + + @Test + public void givenEmployee_whenCustomTypeInQuery_thenReturnEntity() throws IOException { + + final OfficeEmployee e = new OfficeEmployee(); + e.setDateOfJoining(LocalDate.now()); + + PhoneNumber number = new PhoneNumber(1, 222, 8781902); + e.setEmployeeNumber(number); + + Address empAdd = new Address(); + empAdd.setAddressLine1("Street"); + empAdd.setAddressLine2("Area"); + empAdd.setCity("City"); + empAdd.setCountry("Country"); + empAdd.setZipCode(100); + e.setEmpAddress(empAdd); + + Salary empSalary = new Salary(); + empSalary.setAmount(1000L); + empSalary.setCurrency("USD"); + e.setSalary(empSalary); + + doInHibernate(this::sessionFactory, session -> { + session.save(e); + + Query query = session.createQuery("FROM OfficeEmployee OE WHERE OE.empAddress.zipcode = :pinCode"); + query.setParameter("pinCode",100); + int size = query.list().size(); + + Assert.assertEquals(1, size); + }); + + } + + private SessionFactory sessionFactory() { + try { + return HibernateUtil.getSessionFactory("hibernate-customtypes.properties"); + } catch (IOException e) { + e.printStackTrace(); + } + + return null; + } +} diff --git a/persistence-modules/hibernate5-annotations/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest.java b/persistence-modules/hibernate5-annotations/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest.java new file mode 100644 index 0000000000..0998ff1d90 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/test/java/com/baeldung/hibernate/joincolumn/JoinColumnIntegrationTest.java @@ -0,0 +1,57 @@ +package com.baeldung.hibernate.joincolumn; + +import com.baeldung.hibernate.HibernateUtil; +import java.io.IOException; +import org.hibernate.Session; +import org.hibernate.Transaction; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class JoinColumnIntegrationTest { + + private Session session; + private Transaction transaction; + + @Before + public void setUp() throws IOException { + session = HibernateUtil.getSessionFactory("hibernate-spatial.properties") + .openSession(); + transaction = session.beginTransaction(); + } + + @After + public void tearDown() { + transaction.rollback(); + session.close(); + } + + @Test + public void givenOfficeEntity_setAddress_shouldPersist() { + Office office = new Office(); + + OfficeAddress address = new OfficeAddress(); + address.setZipCode("11-111"); + office.setAddress(address); + + session.save(office); + session.flush(); + session.clear(); + } + + @Test + public void givenEmployeeEntity_setEmails_shouldPersist() { + OfficialEmployee employee = new OfficialEmployee(); + + Email email = new Email(); + email.setAddress("example@email.com"); + email.setEmployee(employee); + + session.save(employee); + session.flush(); + session.clear(); + } + +} \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/test/resources/hibernate-spatial.properties b/persistence-modules/hibernate5-annotations/src/test/resources/hibernate-spatial.properties new file mode 100644 index 0000000000..1657c838e3 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/test/resources/hibernate-spatial.properties @@ -0,0 +1,14 @@ +hibernate.connection.driver_class=org.h2.Driver +hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1 +hibernate.connection.username=sa +hibernate.connection.autocommit=true +jdbc.password= + +hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop + +hibernate.c3p0.min_size=5 +hibernate.c3p0.max_size=20 +hibernate.c3p0.acquire_increment=5 +hibernate.c3p0.timeout=1800 diff --git a/persistence-modules/hibernate5-annotations/src/test/resources/log4j.xml b/persistence-modules/hibernate5-annotations/src/test/resources/log4j.xml new file mode 100644 index 0000000000..2d153af124 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/test/resources/log4j.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/test/resources/log4j2.xml b/persistence-modules/hibernate5-annotations/src/test/resources/log4j2.xml new file mode 100644 index 0000000000..c5d0f12462 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/test/resources/log4j2.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/test/resources/logback.xml b/persistence-modules/hibernate5-annotations/src/test/resources/logback.xml new file mode 100644 index 0000000000..9e591977d7 --- /dev/null +++ b/persistence-modules/hibernate5-annotations/src/test/resources/logback.xml @@ -0,0 +1,18 @@ + + + + + + %d{yyyy-MM-dd HH:mm:ss} | %-5p | [%thread] %logger{5}:%L - %msg%n + + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate5-annotations/src/test/resources/profile.png b/persistence-modules/hibernate5-annotations/src/test/resources/profile.png new file mode 100644 index 0000000000..1cd4e978b9 Binary files /dev/null and b/persistence-modules/hibernate5-annotations/src/test/resources/profile.png differ