如何创建子类@Entity?

时间:2017-12-25 02:29:59

标签: jpa spring-boot

我是春季靴子的新手。 我发现@Entity可以为db创建一个类,它可以工作。

const example = {test: "This is a test"};
const overwritten = {test: "This is a test"};
overwritten.toString = function(){
                           return '{test: "This is a test"}';
                       };
let all = {};
all[example] = 1;
all[overwritten] = 2;

// all == {"[object Object]": 1, '{test: "This is a test"}': 2}

但是当我扩展这个类时,只有当前字段存储到db。

//tTicker.java
@Entity
public class tTicker {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private  String currencyPair;
    private  BigDecimal open;
    private  BigDecimal last;

    ......
}

//tTickerRepository.java
public interface tTickerRepository extends CrudRepository<tTicker,Long> {
}    

//tService.java
tTicker tt = new tTicker();
        tt.setCurrencyPair(ticker.getCurrencyPair().toString());
        tt.setOpen(ticker.getOpen());
        tt.setLast(ticker.getLast());      

        ttickerRepository.save(tt);

我该怎么办?谢谢你的帮助。

https://github.com/xchao0213/aitrader

1 个答案:

答案 0 :(得分:0)

由于您的问题与JPA实体有关,因此您应该查看JPA文档(而不是Spring引导)。

您需要使用@MappedSupperClass注释您的超级/基类:

//tTicker.java
@MappedSupperClass
public class tTicker {    
    private  String currencyPair;
    private  BigDecimal open;
    private  BigDecimal last;

    ......
}


//aTicker.java
@Entity 
public class aTicker extends tTicker {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

}

看看@MappedSupperClass文档: https://docs.oracle.com/javaee/7/api/javax/persistence/MappedSuperclass.html