我有一个基本映射类,其中包含一些常用字段
@MappedSuperclass
@Data
public class NodeElement {
@Id
private String id;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private ElementPosition elementPosition;
}
从这个NodeElement
类开始,我只有几个类
@Data
@Entity(name = "node")
@Table(name = "NODE")
public class Node extends NodeElement {
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "PARENT_NODE_ID")
private Node parentNode;
@Column(name = "MESSAGE")
private String message;
@Column(name = "TITLE")
private String title;
@OneToMany(mappedBy = "node", cascade = CascadeType.ALL, orphanRemoval = true)
private List<NodeButton> nodeButtons;
@OneToMany(mappedBy = "node", cascade = CascadeType.ALL, orphanRemoval = true)
private List<NodeAsset> nodeAssets;
}
对于CRUD操作,我正在使用spring-data存储库:
public interface NodeElementRepository extends CrudRepository<NodeElement, String> {
}
对于保存实体,它工作得很好:
NodeElement nodeModel = elementConverter.convert(nodeElement);
this.nodeElementRepository.save(nodeModel);
但是当我尝试通过ID查找实体
Optional<NodeElement> node = this.nodeElementRepository.findById(nodeModel.getId());
我收到以下错误:
org.hibernate.UnknownEntityTypeException: Unable to locate persister: by.com.lifetech.lifeEmployeeBot.model.NodeElement
是否可以通过使用基本存储库获取实体?还是我必须使用特定的(NodeRepository
而不是NodeElementRepository
)?
答案 0 :(得分:0)
尝试将String更改为Long in
公共接口NodeElementRepository扩展了CrudRepository
如果这样不起作用,请尝试编写扩展JpaRepository的存储库
@Repository //remember about this!
public interface NodeElementRepository extends JpaRepository<NodeElement, Long> {
}
也可以尝试方法
.findOne(id)
答案 1 :(得分:0)
我更改了NodeElement
类的映射:
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Entity
@Data
public class NodeElement {
@Id
private String id;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private ElementPosition elementPosition;
}
然后,我可以使用NodeElementRepository
类通过id获取实体