2016-10-05 5 views
0

としてカスタム型を使用することはできません、私は例外を取得しています:春、JPA、PostgreSQLは - ID

org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

私はJPAエンティティも

@Entity(name = "products") 
class JpaProductEntity { 

    @Id 
    private ProductId id; 

    private String payload; 

    private Integer version; 

    public JpaProductEntity() { 
    } 

    public void setId(ProductId id) { 
     this.id = id; 
    } 

    public String getPayload() { 
     return payload; 
    } 

    public void setPayload(String payload) { 
     this.payload = payload; 
    } 

    public Integer getVersion() { 
     return version; 
    } 

    public void setVersion(Integer version) { 
     this.version = version; 
    } 
} 

IDクラス

public class ProductId implements Serializable { 

    private static final long serialVersionUID = -6110163899364126142L; 

    private String identifier; 

    public ProductId(String identifier) { 
     this.identifier = identifier; 
    } 

    public static ProductId from(String identifier) { 
     Assert.notNull(identifier, "Identifier cannot be null"); 
     return new ProductId(identifier); 
    } 

    public static ProductId generate() { 
     return new ProductId(Id.generateIdentifier()); 
    } 
} 

を持っています、コンバータ:

@Converter(autoApply = true) 
public class ProductIdPostgresUuidConverter implements AttributeConverter<ProductId, UUID> { 

    @Override 
    public UUID convertToDatabaseColumn(ProductId attribute) { 
     return UUID.fromString(attribute.toString()); 
    } 

    @Override 
    public ProductId convertToEntityAttribute(UUID dbData) { 
     return ProductId.from(dbData.toString()); 
    } 
} 

コンバータはまったく動作しないようです(私はコンバータのメソッドにブレークポイントを入れましたが、何も起こらず、デバッガはそこに入りません)。

@Entity(name = "products") 
class JpaProductEntity { 

    @Type(type = "pg-uuid") // tells what type will be internally used by DB 
    // Definitely need to instruct JPA to use your converter 
    @Convert(converter = ProductIdPostgresUuidConverter.class) 
    @Id 
    private ProductId id; 

} 

ヒント:あなたはあなたの裁量で使用されるように、同じタイプの複数のコンバータを有していてもよく

+0

JPAプロバイダはJPA 2.1をサポートしていますか?あなたは非PKフィールドのコンバータを使えますか?どのJPAプロバイダですか?あなたは明示的にコンバータをpersistence.xmlに含めましたか? –

+0

'ProductId'クラスに' @ Embeddable'、 'JpaProductEntity'にidを' @埋め込み 'としてアノテーションを付けることができます。変換は必要ありません。もちろん、 'ProductId'は' identifier'と引数のないコンストラクタのためのgetterとsetterを必要とします。 – coladict

答えて

0

はこれを試してみてください。 JPAに明示的に指示する必要があります。

+0

@Convert(converter = ProductIdPostgresUuidConverter.class) - 私はこれを試しましたが、私のために働いていません。 – Teimuraz

+0

@moreoエラーは何でしたか? –

+0

私は、ユーザがHibernateを使用していると言っている場所が見当たらず、結局のところ '@ Type'を使うことができる唯一の場所です –