JPA双向映射取值问题解决办法
4人赞赏了该文章
273次浏览
编辑于2018年08月21日 19:05:43
1.JPA双向映射取值问题解决办法
1)两个class entity:InboundNodeEntity 和InboundNodeDetailEntity 省略get ,set方法,并且这里的常量是定义在接口中的
@Entity @Table(name=InboundNodeDbConstants.INBOUND_NODE_TABLE_NAME) public class InboundNodeEntity implements Serializable { /** * serialVersionUID: Serial version UUID for InboundNodeEntity * @see */ private static final long serialVersionUID = 8875248360699928223L; @Id @GeneratedValue(generator = InboundNodeDbConstants.INBOUND_NODE_UUID_GENERATOR_NAME) @Column(name = InboundNodeDbConstants.GENERATOR_UUID_NAME) private String guid; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name=InboundNodeDbConstants.INBOUND_NODE_TYPE_GUID, nullable = false) private InboundType inboundType; @Column(name=InboundNodeDbConstants.INBOUND_NODE_NODE_GUID) private String nodeCode; @Column(name=InboundNodeDbConstants.INBOUND_NODE_CARDINALITY) private String cardinality; @Column(name=InboundNodeDbConstants.INBOUND_NODE_LEVEL) private int level; @Column(name=InboundNodeDbConstants.INBOUND_NODE_TABLE) private String tableName; @Column(name=InboundNodeDbConstants.INBOUND_NODE_IS_RULE_RELEVANT) private boolean ruleRelevant; @Column(name=InboundNodeDbConstants.INBOUND_NODE_RULE_OBJECT_NAME) private String ruleObjectName; @Column(name=InboundNodeDbConstants.INBOUND_NODE_ENABLED) private boolean enable; @Column(name=InboundNodeDbConstants.INBOUND_NODE_DESCRIPTION) private String description; @OneToMany(cascade={CascadeType.ALL}, mappedBy="inboundNode") private Set inboundNodeDetails = new HashSet12345678910111213141516171819202122232425262728293031323334353637383940414243444546
@Entity@Table(name = InboundNodeDetailDbConstant.TABLE_INBOUND_NODE_DETAIL)public class InboundNodeDetailEntity implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(generator = "system-uuid") @Column(name = InboundNodeDetailDbConstant.GUID) private String guid; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = InboundNodeDetailDbConstant.NODE_GUID, nullable = false) private InboundNodeEntity inboundNode; @Column(name = InboundNodeDetailDbConstant.ELEMENT_CODE) private String elementCode; @Column(name = InboundNodeDetailDbConstant.DATA_TYPE_CODE) private String dataTypeCode; @Column(name = InboundNodeDetailDbConstant.IS_MANDATORY) private boolean isMandatory; @Column(name = InboundNodeDetailDbConstant.IS_RULERELEVANT) private boolean isRuleRelevant; @Column(name = InboundNodeDetailDbConstant.TABLE_NAME) private String tableName; @Column(name = InboundNodeDetailDbConstant.COLUM_NNAME) private String columnName; @Column(name = InboundNodeDetailDbConstant.IS_ENABLED) private boolean isEnabled; @Column(name = InboundNodeDetailDbConstant.IS_ALTERNETIVEKEY) private boolean isAlternativeKey; @Column(name = InboundNodeDetailDbConstant.IS_PARENT_KEY) private boolean isParentKey; @Column(name = InboundNodeDetailDbConstant.DESCRIPTION) private String description; @OneToMany(cascade = {CascadeType.ALL}, mappedBy = "inboundNodeDetailEntity") private Set<InboundContentEntity> inboundContentEntity; }12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
2)controller层去调用
@GetMapping(value = "/nodeDetail/all") public List<InboundNodeDetailEntityDto> getAllDetails() { return nodeDetailService.getAllDetails(); }12345
3)service层,这里要通过DTO去打断这个互相取值的关联,这样就不会出现一直在取值的问题了!
@Autowired
private InboundNodeDetailEntityRepository nodeDetailRepository;
@Override public List<InboundNodeDetailEntityDto> getAllDetails() { List<InboundNodeDetailEntityDto> inboundNodeDetailEntities = new ArrayList<>(); for (InboundNodeDetailEntity inboundNodeDetailEntity : nodeDetailRepository.findAll()) { InboundNodeDetailEntityDto inboundNodeDetailEntity2 = new InboundNodeDetailEntityDto(); List<InboundNodeDetailEntityParam> inboundNodeDetailEntityParams = new ArrayList<>(); InboundNodeDetailEntityParam inboundNodeDetailEntityParam = new InboundNodeDetailEntityParam(); inboundNodeDetailEntity2.setNodeGUID(inboundNodeDetailEntity.getInboundNode().getNodeCode()); inboundNodeDetailEntity2.setGuid(inboundNodeDetailEntity.getGuid()); inboundNodeDetailEntityParam.setAlternetiveKey(inboundNodeDetailEntity.getAlternativeKey()); inboundNodeDetailEntityParam.setColumnName(inboundNodeDetailEntity.getColumnName()); inboundNodeDetailEntityParam.setDataTypeCode(inboundNodeDetailEntity.getDataTypeCode()); inboundNodeDetailEntityParam.setDescription(inboundNodeDetailEntity.getDescription()); inboundNodeDetailEntityParam.setElementCode(inboundNodeDetailEntity.getElementCode()); inboundNodeDetailEntityParam.setEnable(inboundNodeDetailEntity.getEnabled()); inboundNodeDetailEntityParam.setMandatory(inboundNodeDetailEntity.getMandatory()); inboundNodeDetailEntityParam.setParentKey(inboundNodeDetailEntity.getParentKey()); inboundNodeDetailEntityParam.setRuleRelevant(inboundNodeDetailEntity.getRuleRelevant()); inboundNodeDetailEntityParam.setTableName(inboundNodeDetailEntity.getTableName()); inboundNodeDetailEntityParams.add(inboundNodeDetailEntityParam); //inboundNodeDetailEntity2.setInboundNodeDetailEntityParams(inboundNodeDetailEntityParams); inboundNodeDetailEntities.add(inboundNodeDetailEntity2); } return inboundNodeDetailEntities; }12345678910111213141516171819202122232425262728
4)Repository层
@Repositorypublic interface InboundNodeDetailEntityRepository extends JpaRepository<InboundNodeDetailEntity, String> { List<InboundNodeDetailEntity> findAll(); //List<InboundNodeDetailEntity> save(List<InboundNodeDetailEntity> inboundNodeDetailEntities); @SuppressWarnings("unchecked") InboundNodeDetailEntity save(InboundNodeDetailEntity inboundNodeDetailEntity); @Query(value = "select ine from InboundNodeDetailEntity ine where ine.inboundNode.nodeCode = ?1 and ine.elementCode = ?2") List<InboundNodeDetailEntity> findByNodeGuidAndElementCode(String nodeGuid, String elementCode); @Query(value = "select ine from InboundNodeDetailEntity ine join ine.inboundNode inNode where inNode.inboundType.typeCode=?1 and ine.inboundNode.nodeCode=?2") List<InboundNodeDetailEntity> findByTypeCodeAndNodeCode(String typeCode, String nodeCode); @Query(value = "select * from \"268073BE12C74139B231A938DA1AD6EE\".\"com.sap.ems.db.integration.data::InboundMapping.NodeElement\" element join \"268073BE12C74139B231A938DA1AD6EE\".\"com.sap.ems.db.integration.data::InboundMapping.Node\" node on element.\"NodeGUID\"=node.\"GUID\" WHERE ((\"NodeGUID\" = ?1) AND (\"IsAlternativeKey\" = true)) order by node.\"Level\" desc ",nativeQuery=true) List<InboundNodeDetailEntity> findAlternativeKeyByNodeGUID(String nodeGUID); }12345678910111213141516171819
文章标签: Java spring-data-jpa
来源:https://blog.csdn.net/u012874209/article/details/74295079
赞 4
4人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!
推荐阅读