0

尝试在单个表的继承中使用描述符

@Entity
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "dtype")
//@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
  @Type(value = LdRnEnt.class, name = "ldRnEnt"),
  @Type(value = OtherRepo.class, name = "otherRepo")
})
//@DiscriminatorColumn(name="type", discriminatorType = DiscriminatorType.STRING, length=50) // use as cannot get access for toString
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class RepositoryType {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(updatable = false) private Integer id;
  
//  @Column(insertable = false, updatable = false, nullable = false) private String type; // using discriminator odd settings or error 
  @JsonIgnore
  @Column(insertable = false, updatable = false, nullable = false) private String dtype; // using discriminator odd settings or error 

  @OneToOne
  @JsonBackReference // prevents endless loop in Jackson
  @JoinColumn(name = "tst_ste_id", nullable = false, updatable = false, unique = true)
  private TstSte tstSte;

但是,当我尝试持久化一些数据时,值不在返回中(从持久化开始,但随着尝试不同的想法而增长:

  public void persist(TstSte tstSte) {
    em.persist(tstSte);
    em.flush();
    tstSte = em.find(TstSte.class, tstSte.getTsId());
    System.out.println(tstSte.toString());
  }

在所有情况下,我总是看到

TstSte [tsId=6, name=testSuiteName2, repositoryType.id=6, repositoryType.dtype=null]

但是,当我查看表格时,我确实看到了表格中的值

另外查看 SQL ic

: insert into repository_type (tst_ste_id, domain, project, dtype) values (?, ?, ?, 'LdRnEnt')

从名为 type 的列开始,但注意到了这一点,因此在两种情况下都尝试了默认值,即不返回鉴别器列。这是预期的操作吗?如果是,为什么?

此外,如果我将列作为字段放入:

  @JsonIgnore
  @Column(insertable = false, updatable = false, nullable = false) private String dtype; // using discriminator odd settings or error 

我做了一个简单的查询

  public List<TstSte> tstStes() {
    String sql = "from TstSte";
    return em.createQuery(sql, TstSte.class)
        .getResultList();
  }

然后将 dtype 值填充到对象中

4

0 回答 0