Map null column as 0 in a legacy database (JPA)
        Posted  
        
            by Indrek
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Indrek
        
        
        
        Published on 2010-06-07T07:56:58Z
        Indexed on 
            2010/06/07
            8:02 UTC
        
        
        Read the original article
        Hit count: 273
        
Using Play! framework and it's JPASupport class I have run into a problem with a legacy database.
I have the following class:
@Entity
@Table(name="product_catalog")
public class ProductCatalog extends JPASupport {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer product_catalog;
    @OneToOne
    @JoinColumn(name="upper_catalog")
    public ProductCatalog upper_catalog;
    public String name;
}
Some product catalogs don't have an upper catalog, and this is referenced as 0 in a legacy database. If I supply the upper_catalog as NULL, then expectedly JPA inserts a NULL value to that database column. How could I force the null values to be 0 when writing to the database and the other way around when reading from the database?
© Stack Overflow or respective owner