Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.martinyuk.travelagency.domain;
- import java.io.Serializable;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import org.hibernate.mapping.PersistentClass;
- import org.hibernate.tuple.Instantiator;
- import org.hibernate.tuple.entity.EntityMetamodel;
- import org.hibernate.tuple.entity.PojoEntityTuplizer;
- public class EnumTuplizer extends PojoEntityTuplizer {
- public EnumTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
- super(entityMetamodel, mappedEntity);
- }
- @Override
- @SuppressWarnings(value = { "unchecked", "rawtypes"})
- protected Instantiator buildInstantiator(EntityMetamodel entityMetamodel, PersistentClass persistentClass) {
- return new Instantiator() {
- @Override
- public Object instantiate(Serializable id) {
- try {
- Class enumClass = getMappedClass();
- String identifierMethodName = EnumUserType.DEFAULT_CODE_VALUE_METHOD_NAME;
- Method codeValueMethod = enumClass.getMethod(identifierMethodName, new Class[0]);
- Class<?> codeValueType = codeValueMethod.getReturnType();
- String valueOfMethodName = EnumUserType.DEFAULT_FROM_VALUE_METHOD_NAME;
- Method fromValueMethod = enumClass.getMethod(valueOfMethodName, new Class[] { codeValueType });
- return fromValueMethod.invoke(enumClass, id);
- } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
- throw new AssertionError(e);
- }
- }
- @Override
- public Object instantiate() {
- throw new UnsupportedOperationException();
- }
- @Override
- public boolean isInstance(Object object) {
- throw new UnsupportedOperationException();
- }
- };
- }
- }
- package com.martinyuk.travelagency.domain;
- import java.io.Serializable;
- import java.lang.reflect.Method;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Types;
- import java.util.Properties;
- import org.apache.commons.lang.ObjectUtils;
- import org.hibernate.HibernateException;
- import org.hibernate.engine.spi.SharedSessionContractImplementor;
- import org.hibernate.type.IntegerType;
- import org.hibernate.usertype.ParameterizedType;
- import org.hibernate.usertype.UserType;
- public class EnumUserType<E> implements UserType, ParameterizedType {
- public static final String DEFAULT_CODE_VALUE_METHOD_NAME = "getId";
- public static final String DEFAULT_FROM_VALUE_METHOD_NAME = "fromValue";
- private Method codeValueMethod;
- private Method fromValueMethod;
- private Class<?> codeValueType;
- private Class<E> enumClass;
- public EnumUserType(){}
- @Override
- @SuppressWarnings("unchecked")
- public void setParameterValues(Properties parameters) {
- String enumClassName = parameters.getProperty("enumClass");
- try {
- enumClass = (Class<E>) Class.forName(enumClassName).asSubclass(Enum.class);
- } catch (ClassNotFoundException e) {
- throw new HibernateException("Enum class not found", e);
- }
- String identifierMethodName = parameters.getProperty("codeValueMethod", DEFAULT_CODE_VALUE_METHOD_NAME);
- try {
- codeValueMethod = enumClass.getMethod(identifierMethodName, new Class[0]);
- codeValueType = codeValueMethod.getReturnType();
- } catch (Exception e) {
- throw new HibernateException("Failed to obtain codeValue method. Setting default method name", e);
- }
- String valueOfMethodName = parameters.getProperty("fromValueMethod", DEFAULT_FROM_VALUE_METHOD_NAME);
- try {
- fromValueMethod = enumClass.getMethod(valueOfMethodName, new Class[] { codeValueType });
- } catch (Exception e) {
- throw new HibernateException("Failed to obtain fromValue method. Setting default method name", e);
- }
- }
- @Override
- public Class<E> returnedClass() {
- return enumClass;
- }
- @Override
- public int[] sqlTypes() {
- return new int[] { IntegerType.INSTANCE.sqlType() };
- }
- @Override
- public boolean isMutable() {
- return false;
- }
- @Override
- public boolean equals(Object x, Object y) throws HibernateException {
- return ObjectUtils.equals(x, y);
- }
- @Override
- public int hashCode(Object x) throws HibernateException {
- assert (x != null);
- return x.hashCode();
- }
- @Override
- public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
- try {
- String propertyValue = rs.getString(names[0]);
- Integer propertyNumericValue;
- try {
- propertyNumericValue = Integer.valueOf(propertyValue);
- } catch (NumberFormatException e) {
- return null;
- }
- return fromValueMethod.invoke(enumClass, propertyNumericValue);
- } catch (Exception e) {
- e.printStackTrace();
- throw new HibernateException("Exception while invoking valueOf method '" + fromValueMethod.getName() + "' of " + "enumeration class '" + enumClass + "'", e);
- }
- }
- @Override
- public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
- try {
- if (null == value) {
- st.setNull(index, Types.INTEGER);
- } else {
- Object identifier = codeValueMethod.invoke(value, new Object[0]);
- st.setInt(index, (Integer) identifier);
- }
- } catch (Exception e) {
- e.printStackTrace();
- throw new HibernateException("Exception while invoking identifier method '" + codeValueMethod.getName() + "' of " + "enumeration class '" + enumClass + "'", e);
- }
- }
- @Override
- public Object deepCopy(Object value) throws HibernateException {
- return value;
- }
- @Override
- public Object replace(Object original, Object target, Object owner) throws HibernateException {
- return original;
- }
- @Override
- public Serializable disassemble(Object value) throws HibernateException {
- return (Serializable) value;
- }
- @Override
- public Object assemble(Serializable cached, Object owner) throws HibernateException {
- return cached;
- }
- }
- package com.martinyuk.travelagency.domain;
- import lombok.AccessLevel;
- import lombok.NoArgsConstructor;
- import org.hibernate.annotations.Tuplizer;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- import javax.persistence.Table;
- @Entity
- @Table(name = "tour_type")
- @Tuplizer(impl = EnumTuplizer.class)
- @NoArgsConstructor(access = AccessLevel.PRIVATE)
- public enum TourType {
- VACATION(1, "VACATION"),
- TRIP(2, "TRIP"),
- SHOPPING(3, "SHOPPING");
- @Id
- @Column(name = "id_tour_type")
- private long id;
- @Column(name = "type")
- private String name;
- TourType(long id, String name) {
- this.id = id;
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public long getId() {
- return id;
- }
- public static TourType fromValue(long value) {
- switch ((int)value) {
- case 1: return VACATION;
- case 2: return TRIP;
- case 3: return SHOPPING;
- default: return null;
- }
- }
- }
- package com.martinyuk.travelagency.domain;
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import org.hibernate.annotations.Parameter;
- import org.hibernate.annotations.Type;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.EnumType;
- import javax.persistence.Enumerated;
- import javax.persistence.FetchType;
- import javax.persistence.ForeignKey;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.JoinTable;
- import javax.persistence.ManyToMany;
- import javax.persistence.ManyToOne;
- import javax.persistence.NamedQuery;
- import javax.persistence.OneToMany;
- import javax.persistence.OneToOne;
- import javax.persistence.PrimaryKeyJoinColumn;
- import javax.persistence.SecondaryTable;
- import javax.persistence.Table;
- import java.math.BigDecimal;
- import java.time.LocalDate;
- @Data
- @Entity
- @Table(name = "tour")
- @NoArgsConstructor
- @AllArgsConstructor
- @NamedQuery(name = "Tour.findAll", query = "FROM Tour")
- public class Tour {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "id_tour", nullable = false, insertable = false, updatable = false)
- private long id;
- @Column(name = "photo", nullable = false)
- private byte[] photo;
- @Column(name = "date", nullable = false)
- private LocalDate date;
- @Column(name = "duration", nullable = false)
- private int duration;
- @ManyToOne(optional = false, cascade = CascadeType.MERGE)
- @JoinColumn(name = "hotel_id_fk", nullable = false)
- private Hotel hotel;
- @ManyToOne(optional = false, cascade = CascadeType.MERGE)
- @JoinColumn(name = "tour_type_id_fk", nullable = false)
- @Type(type = "com.martinyuk.travelagency.domain.EnumUserType", parameters = @Parameter(name = "type", value = "com.martinyuk.travelagency.domain.TourType"))
- private TourType tourType;
- @Column(name = "description", nullable = false)
- private String description;
- @Column(name = "cost", nullable = false)
- private BigDecimal cost;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement