4

I have a jpa entity like this:

@Entity
public class RectangleEntity
{

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer id;

   @Column
   private Integer x;

   @Column
   private Integer y;

   @Column
   @Convert(converter = ColorConverter.class)
   private Color color;

}

And my ColorConverter is like this:

@Converter
public class ColorConverter implements AttributeConverter<Color, String[]> {

 @Override
 public String[] convertToDatabaseColumn(Color color) {
  switch(color) {
      case REDISH : return {"red","pink"};
      case GREENISH: return {"green","cyan"};
  }
  return sb.toString();
 }

 @Override
 public Color convertToEntityAttribute(String... colorStrings) {

  if(colorStrings == null || colorStrings.length != 1) {
     return null;
  }
  if(colorStrings[0].equals("red") || colorStrings[0].equals("pink")) {
     return REDISH; 
  } else if(colorStrings[0].equals("green") || colorStrings[0].equals("cyan")) {
     return GREENISH; 
  }
 }

}

The thing is that I want to use my column value in a query like this:

Expression<String> colorPath = root.get("color");
Predicate predicate = requestStatePath.in(Color.REDISH);
Predicate[] predicatesArr = predicates.toArray(new Predicate[predicates.size()]);
criteriaQuery.where(predicatesArr);
CriteriaQuery<RectangleEntity> criteriaQuery = criteriaBuilder.createQuery(RectangleEntity.class);
TypedQuery<RectangleEntity> query = this.em.createQuery(criteriaQuery);
query.getResultList();

I want this query select all my records which their color field is pink or red as they are REDISH.

Is it possible?

A.v
  • 734
  • 5
  • 26

1 Answers1

1

I don't think your converter is doing what you want it to do. First, realize that would not even be a valid AttributeConverter according to JPA. JPA only defines support for AttributeConverters on basic types and does not consider a String[] a basic type. And while Hibernate allows the String[], it will only see it as a Serializable.

Are you expecting to store to store these String[]s into a DB ARRAY? If so, Hibernate does not support DB ARRAY types (yet, we have been talking about adding support for them in 6.0).

But to answer your exact question... yes, JPA says that the AttributeConverter ought to be automatically applied in the query within certain guidelines as outlined in section 3.8:

... The persistence provider must apply any conversion methods to instances of attribute values in path expressions used within Java Persistence query language queries or criteria queries (such as in comparisons, bulk updates, etc.) before sending them to the database for the query execution. When such converted attributes are used in comparison operations with literals or parameters, the value of the literal or parameter to which they are compared must also be converted. ...

But you'd end up with a predicate like <BINARY> in <BINARY>

Steve Ebersole
  • 9,339
  • 2
  • 48
  • 46
  • 1
    Tanx for your reply. You are right about String[]. That was my mistake. My problem is not about storing value, due to my use of oracle view. I want to query my view with parameter of a type which will convert to a set of values which will be used as an `in` value. I didn't get your last statement. – A.v Aug 31 '16 at 05:02