JavaSpring数据存储库对具有不同Id的子类使用抽象超类
我有几个表有不同的@Id名称,但保存逻辑类似。我想为它们创建一个公共存储库
public interface MachineRepository extends JpaRepository<Machine, Long> {}
实体类如下所示:
抽象类Machine
@MappedSuperclass
public abstract class Machine{
}
实体类Computer
,除了id名称之外,它与server
具有相同的列字段
@Entity
@Table(name = "computer")
public class Computer extends Machine{
@Id
Long computerId;
String name;
// same getter & setter ...
}
实体类Server
@Entity
@Table(name = "server")
public class Server extends Machine{
@Id
Long serverId;
String name;
// getter & setter...
}
我想这样储蓄:
Machine computer = new Computer();
machineRepository.save(computer);
Machine server = new Server();
machineRepository.save(server);
还有什么我可以做的吗
共 (0) 个答案