SHARE
TWEET

Untitled

a guest Jan 12th, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Data
  2. @Entity
  3. @Table(name = "transaction_line_item_value")
  4. @Builder
  5. @AllArgsConstructor
  6. @NoArgsConstructor
  7. @TypeDefs({
  8.     @TypeDef(name = "rupee-custom",  defaultForType = Rupee.class,  typeClass = RupeeType.class)
  9. })
  10. public class TransactionLineItemValue {
  11.  
  12.     @Id
  13.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  14.     @PodamExclude
  15.     private int id;
  16.  
  17.     //@Column(name = "amount", nullable = false) Works! But makes amount immutable.
  18.     @Column(name = "amount")
  19.     private Rupee amount;
  20.  
  21. }
  22.    
  23. public class RupeeType extends AbstractSingleColumnStandardBasicType<BigDecimal> {
  24.  
  25.     public RupeeType() {
  26.     super(new RupeeTypeDescriptor(), BigDecimalTypeDescriptor.INSTANCE);
  27.     }
  28.  
  29.     @Override
  30.     public String getName() {
  31.     return "rupee-custom";
  32.     }
  33.  
  34.     public static class RupeeTypeDescriptor implements SqlTypeDescriptor {
  35.  
  36.  
  37.     @Override
  38.     public int getSqlType() {
  39.         return Types.DOUBLE;
  40.     }
  41.  
  42.     @Override
  43.     public boolean canBeRemapped() {
  44.         return false;
  45.     }
  46.  
  47.     @Override
  48.     public <X> ValueBinder<X> getBinder(JavaTypeDescriptor<X> javaTypeDescriptor) {
  49.         return new BasicBinder<X>(javaTypeDescriptor, (SqlTypeDescriptor) this) {
  50.  
  51.             @Override
  52.             protected void doBind(PreparedStatement preparedStatement, X x, int i, WrapperOptions wrapperOptions) throws SQLException {
  53.                 preparedStatement.setBigDecimal(i, ((Rupee) x).getAmount());
  54.             }
  55.  
  56.             @Override
  57.             protected void doBind(CallableStatement callableStatement, X x, String s, WrapperOptions wrapperOptions) throws SQLException {
  58.                 callableStatement.setBigDecimal(s, ((Rupee) x).getAmount());
  59.             }
  60.         };
  61.     }
  62.  
  63.     @Override
  64.     public <X> ValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) {
  65.         return new BasicExtractor<X>(javaTypeDescriptor, (SqlTypeDescriptor) this) {
  66.  
  67.             @Override
  68.             protected X doExtract(ResultSet resultSet, String s, WrapperOptions wrapperOptions) throws SQLException {
  69.                 return (X) Rupee.builder().amount(resultSet.getBigDecimal(s)).build();
  70.             }
  71.  
  72.             @Override
  73.             protected X doExtract(CallableStatement callableStatement, int i, WrapperOptions wrapperOptions) throws SQLException {
  74.                 return (X) Rupee.builder().amount(callableStatement.getBigDecimal(i)).build();
  75.             }
  76.  
  77.             @Override
  78.             protected X doExtract(CallableStatement callableStatement, String s, WrapperOptions wrapperOptions) throws SQLException {
  79.                 return (X) Rupee.builder().amount(callableStatement.getBigDecimal(s)).build();
  80.             }
  81.         };
  82.     }
  83.  }
  84.  
  85. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top