更改抽象存储库

时间:2016-11-16 14:31:22

标签: java spring spring-data-jpa

我有一个像以下一样的超级课程:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected long id = "";

    protected String firstname = "";

    protected String lastname = "";

    protected Address address = null;

    // getter setter

正如你可以看到我正在使用TABLE_PER_CLASSInheritanceType,我想保持这样。

现在我有两个继承自Base

的子类
@Entity
public class Consulter extends Base {
    // other fields
} 

@Entity
public class Customer extends Base {
    // other fields
}

当我现在创建如下所示的存储库时:

public interface Base_Repository extends JpaRepository<Base, Long> {

}

当我提供具体类型的id时,jpa能否从数据库中返回正确的Object?那样的事情呢?

long id = 222;    

Customer customer = customerRepository.findeOne(id);
Base base = baseRepository.findeOne(id);

boolean equals = customer.equals(((Customer) base));

jpa也可以这样做吗?

Address address = new Address();
address.setStreet("space street 42");
address.setCountry("Trump Nation");

addressRepository.save(address);

Base base = baseRepository.findeOne(222);
base.setAddress(address);

baseRepository.save(base);

这会起作用还是不可能?

1 个答案:

答案 0 :(得分:1)

您无法保存抽象实体,因为抽象实体不会映射到数据库表。您需要保存子实体而不是抽象实体。