Skip to content

Commit c2be113

Browse files
committed
HHH-8283 missed a few commits
1 parent 927a25e commit c2be113

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package org.hibernate.ejb.criteria.basic;
2+
3+
import java.io.Serializable;
4+
import java.sql.PreparedStatement;
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
import java.util.Calendar;
8+
import java.util.Date;
9+
import java.util.GregorianCalendar;
10+
11+
import org.hibernate.HibernateException;
12+
import org.hibernate.engine.spi.SessionImplementor;
13+
import org.hibernate.type.StandardBasicTypes;
14+
import org.hibernate.type.Type;
15+
import org.hibernate.usertype.CompositeUserType;
16+
17+
/**
18+
* @author Francois Gerodez
19+
*/
20+
public class Date3Type implements CompositeUserType {
21+
22+
@Override
23+
public String[] getPropertyNames() {
24+
return new String[] { "year", "month", "day" };
25+
}
26+
27+
@Override
28+
public Type[] getPropertyTypes() {
29+
return new Type[] { StandardBasicTypes.INTEGER, StandardBasicTypes.INTEGER, StandardBasicTypes.INTEGER };
30+
}
31+
32+
@Override
33+
public Object getPropertyValue(Object component, int property) throws HibernateException {
34+
Date date = (Date) component;
35+
Calendar c = GregorianCalendar.getInstance();
36+
c.setTime( date );
37+
38+
switch ( property ) {
39+
case 0:
40+
return c.get( Calendar.YEAR );
41+
case 1:
42+
return c.get( Calendar.MONTH );
43+
case 2:
44+
return c.get( Calendar.DAY_OF_MONTH );
45+
}
46+
47+
throw new HibernateException( "Invalid property provided" );
48+
}
49+
50+
@Override
51+
public void setPropertyValue(Object component, int property, Object value) throws HibernateException {
52+
Date date = (Date) component;
53+
Calendar c = GregorianCalendar.getInstance();
54+
c.setTime( date );
55+
56+
switch ( property ) {
57+
case 0:
58+
c.set( Calendar.YEAR, (Integer) value );
59+
case 1:
60+
c.set( Calendar.MONTH, (Integer) value );
61+
case 2:
62+
c.set( Calendar.DAY_OF_MONTH, (Integer) value );
63+
default:
64+
throw new HibernateException( "Invalid property provided" );
65+
}
66+
}
67+
68+
@Override
69+
public Class returnedClass() {
70+
return Date.class;
71+
}
72+
73+
@Override
74+
public boolean equals(Object x, Object y) throws HibernateException {
75+
if ( x == y )
76+
return true;
77+
if ( x == null || y == null )
78+
return false;
79+
Date dx = (Date) x;
80+
Date dy = (Date) y;
81+
82+
return dx.equals( dy );
83+
}
84+
85+
@Override
86+
public int hashCode(Object x) throws HibernateException {
87+
Date dx = (Date) x;
88+
return dx.hashCode();
89+
}
90+
91+
@Override
92+
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
93+
Date date = new Date();
94+
Calendar c = GregorianCalendar.getInstance();
95+
c.setTime( date );
96+
97+
Integer year = StandardBasicTypes.INTEGER.nullSafeGet( rs, names[0], session );
98+
Integer month = StandardBasicTypes.INTEGER.nullSafeGet( rs, names[1], session );
99+
Integer day = StandardBasicTypes.INTEGER.nullSafeGet( rs, names[2], session );
100+
101+
c.set( year, month, day );
102+
103+
return date;
104+
}
105+
106+
@Override
107+
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
108+
Date date = new Date();
109+
Calendar c = GregorianCalendar.getInstance();
110+
c.setTime( date );
111+
112+
StandardBasicTypes.INTEGER.nullSafeSet( st, c.get( Calendar.YEAR ), index, session );
113+
StandardBasicTypes.INTEGER.nullSafeSet( st, c.get( Calendar.MONTH ), index + 1, session );
114+
StandardBasicTypes.INTEGER.nullSafeSet( st, c.get( Calendar.DAY_OF_MONTH ), index + 2, session );
115+
}
116+
117+
@Override
118+
public Object deepCopy(Object value) throws HibernateException {
119+
if ( value == null )
120+
return null;
121+
122+
Date date = (Date) value;
123+
return date.clone();
124+
}
125+
126+
@Override
127+
public boolean isMutable() {
128+
return true;
129+
}
130+
131+
@Override
132+
public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException {
133+
return (Serializable) deepCopy( value );
134+
}
135+
136+
@Override
137+
public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException {
138+
return deepCopy( cached );
139+
}
140+
141+
@Override
142+
public Object replace(Object original, Object target, SessionImplementor session, Object owner) throws HibernateException {
143+
return deepCopy( original ); // TODO: improve
144+
}
145+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.hibernate.ejb.criteria.basic;
2+
3+
import java.math.BigDecimal;
4+
import java.util.Date;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
import javax.persistence.Table;
10+
import javax.persistence.Column;
11+
12+
import org.hibernate.annotations.Columns;
13+
import org.hibernate.annotations.Type;
14+
import org.hibernate.annotations.TypeDef;
15+
16+
/**
17+
* @author Francois Gerodez
18+
*/
19+
@Entity
20+
@Table(name = "crit_basic_payment")
21+
@TypeDef(name = "paymentDate", typeClass = Date3Type.class)
22+
public class Payment {
23+
24+
private Long id;
25+
private BigDecimal amount;
26+
private Date date;
27+
28+
@Id
29+
@GeneratedValue
30+
public Long getId() {
31+
return id;
32+
}
33+
34+
public void setId(Long id) {
35+
this.id = id;
36+
}
37+
38+
public BigDecimal getAmount() {
39+
return amount;
40+
}
41+
42+
public void setAmount(BigDecimal amount) {
43+
this.amount = amount;
44+
}
45+
46+
@Type(type = "paymentDate")
47+
@Columns(columns = { @Column(name = "YEARPAYMENT"), @Column(name = "MONTHPAYMENT"), @Column(name = "DAYPAYMENT") })
48+
public Date getDate() {
49+
return date;
50+
}
51+
52+
public void setDate(Date date) {
53+
this.date = date;
54+
}
55+
}

0 commit comments

Comments
 (0)