[英]Difficulty creating relationship with abstract class and embedded attribute with JPA/Hibernate
I'm trying, but have not been successful so far, using the following classes with Hibernate. 我正在尝试,但到目前为止还没有成功,在Hibernate中使用以下类。
@MappedSuperclass
@Embeddable
public abstract class Foo {
// atributes...
}
@Embeddable
public class Poo extends Foo {
// atributes...
}
@Entity
@Table
public class None {
// atributes...
@Embedded
private Foo foo;
// constructor
public None(Foo foo) {
this.foo = foo;
}
}
// example of save
None none = new None(Poo poo);
save(none);
Hibernate returns: Cannot instantiate abstract class or interface Hibernate返回: 无法实例化抽象类或接口
Is it possible to perform this operation with JPA? 是否可以使用JPA执行此操作?
I ran into the same problem. 我遇到了同样的问题。
It seems like @embedable
does not work with @DiscriminatorColumn
. 似乎
@embedable
无法与@DiscriminatorColumn
。 The only way I could get this to work is to use @DiscriminatorColumn
, but treat the @embedable
like a separate entity on the same table. 我可以
@embedable
起作用的唯一方法是使用@DiscriminatorColumn
,但将@embedable
同一张表上的单独实体。 What this means is that the query will likely join the table to itself. 这意味着查询可能会将表连接到自身。
@Entity
@Table(name="tbComputers")
public class Computer{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public long id;
public String motherboard;
@OneToOne
@JoinColumn(name="id")
public CPU cpu;
}
@Entity
@DiscriminatorColumn(name="cpu_type")
@Table(name="tbComputers")
public abstract class CPU {
@Id
private Long id;
@Column(name = "cpu")
public String name;
public abstract String someProcessorSpecificMethod();
}
@Entity
@DiscriminatorValue("Intel")
public class Intel extends CPU {
@Override
public String someProcessorSpecificMethod() {
return "Intel";
}
}
@Entity
@DiscriminatorValue("AMD")
public class AMD extends CPU {
@Override
public String someProcessorSpecificMethod() {
return "AMD";
}
}
EDIT: After further testing I found that while this works for reading data, it does not for persisting. 编辑:经过进一步测试,我发现虽然这适用于读取数据,但不适用于持久化。 It will create a separate INSERT.
它将创建一个单独的INSERT。 It seems like it is not supported https://hibernate.atlassian.net/browse/HHH-1910 .
似乎不支持https://hibernate.atlassian.net/browse/HHH-1910 。 The alternative is to to split the table.
另一种方法是拆分表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.