Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 将旧数据库(JPA)中的空列映射为0_Database_Jpa_Null_Legacy_Playframework - Fatal编程技术网

Database 将旧数据库(JPA)中的空列映射为0

Database 将旧数据库(JPA)中的空列映射为0,database,jpa,null,legacy,playframework,Database,Jpa,Null,Legacy,Playframework,使用游戏!框架和它的JPASupport类,我遇到了遗留数据库的问题 我有以下课程: @Entity @Table(name="product_catalog") public class ProductCatalog extends JPASupport { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Integer product_catalog; @OneToOne

使用游戏!框架和它的JPASupport类,我遇到了遗留数据库的问题

我有以下课程:

@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;
}
某些产品目录没有上层目录,在旧数据库中被引用为0。如果我将上面的目录作为NULL提供,那么JPA会向该数据库列插入一个NULL值。
在写入数据库时,如何将空值强制为0,在从数据库读取时,如何将空值强制为0?

我看不到任何简单的方法可以直接使用JPA实现您想要的功能(而且,即使您找到了一种适用于保存或加载等基本操作的方法,也很有可能无法适用于更复杂的用例,如复杂的条件/hql、非标准获取模式等)

所以我会这样做:

@Entity
@Table(name="product_catalog")
public class ProductCatalog extends JPASupport {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer product_catalog;

    @Column(name="upper_catalog")
    public Long upper_catalog_id;

    public String name;

    public ProductCatalog getUpperCatalog() {
       if (upper_catalog_id == 0)
         return null;

       return ProductCatalog.findById(upper_catalog_id);
    }

    public void setUpperCatalog(ProductCatalog pc) {
       if (pc == null) {
         upper_catalog_id = 0;
       }
       else {
         if (pc.id == null) {
            // option 1. a bit like a cascade
            pc.save();
            // option 2. if you consider passing a transient entity is not valid
            throw new RuntimeException("transient entity " + pc.toString());
         }
         upper_catalog_id = pc.id;
       }
    }
}
我看到两种选择:

  • 将基本数据类型用作
    Id
    (即
    int
    而不是
    Integer
  • 如果您使用Hibernate作为JPA提供程序,请使用
    CustomType
    进行转换

谢谢,它工作得很好!只需将Long改为整数(一些Play!配置?)。在这种情况下,@Transient的含义是什么?我没有添加它,而且我对这个类的所有测试似乎都通过了。JPA提供商将Transient理解为:“不要管理这个字段(不要将它保存到数据库))”。我玩的时候忘了@瞬态仅在现场需要。我用了很长时间,因为这是默认播放!键入id,但使用您刚刚重新阅读代码后希望使用的任何内容,并且您应该小心地仅使用持久化ProductCatalog(带有id)调用setter,否则它将无法工作。(也许您应该在方法的开头添加此测试:如果pc.id为null,则持久化pc)。