简体   繁体   English

如何扩展@Entity

[英]How to extend an @Entity

This is my superclass 这是我的超人

@Entity
@Table(name = "utente")
@Component
@Inheritance
public class Utente implements Serializable{

    private static final long serialVersionUID = -7124540331184173742L;

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "nome")
    @Size(min = 1, max = 20)
    @Pattern(regexp="^[A-Za-z 'òàùèéì]*$")      
    @NotBlank
    private String nome;

    @Column(name = "cognome")
    @Size(min = 1, max = 20)
    @Pattern(regexp="^[A-Za-z 'òàùèéì]*$")
    @NotBlank
    private String cognome;

    @Column(name = "email")
    @Email
    @Size(min = 1, max = 40)
    private String email;

    @OneToOne(mappedBy = "utente", cascade = CascadeType.ALL)
    @Valid
    private Autenticazione autenticazione;  

    @OneToMany(mappedBy = "utente", fetch = FetchType.EAGER,  orphanRemoval=true, cascade=CascadeType.ALL)  
    private List<Autorizzazioni> autorizzazioniLista;
}

This is the class that extend the one above: 这是扩展以上内容的类:

@Entity
public class UtenteSubClass extends Utente{ 

@Transient
private String newField;
}

When I try to retrieve an object UtenteSubClass I get this error "org.hibernate.util.JDBCExceptionReporter - Unknown column 'this_.DTYPE' in 'where clause'" . 当我尝试检索对象UtenteSubClass时,出现此错误"org.hibernate.util.JDBCExceptionReporter - Unknown column 'this_.DTYPE' in 'where clause'"

Probably my "mapping system" is wrong. 我的“映射系统”可能是错误的。 Where is my error? 我的错误在哪里?

Thank you in advance. 先感谢您。

If you choose to use SINGLE_TABLE inheritance (which is default for @Inheritance annotation without arguments in your code) then you should add @DiscriminatorColumn annotation to your superclass (and @DiscriminatorValue to extending classes). 如果选择使用SINGLE_TABLE继承( @Inheritance批注的默认值,而代码中没有参数),则应将@DiscriminatorColumn批注添加到您的超类(并将@DiscriminatorValue到扩展类)。 Otherwise via documentation : 否则通过文档

If @DiscriminatorColumn is not specified on the root of the entity hierarchy and a discriminator column is required, the Persistence provider assumes a default column name of DTYPE and column type of DiscriminatorType.STRING. 如果未在实体层次结构的根目录上指定@DiscriminatorColumn,并且需要一个discriminator列,则Persistence提供程序将假定默认列名称为DTYPE,而列类型为DiscriminatorType.STRING。

hibernate just can't find this default column in your table. 休眠只是无法在表中找到此默认列。 You can introduce your own descriminator column, or generate default one 您可以引入自己的说明符列,或生成默认的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM