比如我们这里想要创建一张表,用于用户关注和粉丝。
relationship 两个字段,都是主键(即联合主键)
Spring Data JPA 实体如何设计才能自动创建这样的表呢?
仅仅使用两个 @Id 吗?
答案不是。
解决办法
Relationship.java 实体
RelationshipPK.java 辅助
启动项目,生成的数据表符合要求
RelationshipRepository.java
s
relationship 两个字段,都是主键(即联合主键)
Spring Data JPA 实体如何设计才能自动创建这样的表呢?
仅仅使用两个 @Id 吗?
答案不是。
解决办法
实体
Relationship.java 实体
- package com.liuyanzhao.forum.entity;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- import javax.persistence.IdClass;
- /**
- * @author 言曌
- * @date 2018/4/24 下午9:38
- */
- @Entity
- @IdClass(RelationshipPK.class)
- public class Relationship {
- private Long fromUserId;
- private Long toUserId;
- @Id
- @Column(name = "from_user_id", nullable = false)
- public Long getFromUserId() {
- return fromUserId;
- }
- public void setFromUserId(Long fromUserId) {
- this.fromUserId = fromUserId;
- }
- @Id
- @Column(name = "to_user_id", nullable = false)
- public Long getToUserId() {
- return toUserId;
- }
- public void setToUserId(Long toUserId) {
- this.toUserId = toUserId;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Relationship that = (Relationship) o;
- if (fromUserId != null ? !fromUserId.equals(that.fromUserId) : that.fromUserId != null) return false;
- if (toUserId != null ? !toUserId.equals(that.toUserId) : that.toUserId != null) return false;
- return true;
- }
- @Override
- public int hashCode() {
- int result = fromUserId != null ? fromUserId.hashCode() : 0;
- result = 31 * result + (toUserId != null ? toUserId.hashCode() : 0);
- return result;
- }
- }
RelationshipPK.java 辅助
- package com.liuyanzhao.forum.entity;
- import javax.persistence.Column;
- import javax.persistence.Id;
- import java.io.Serializable;
- /**
- * @author 言曌
- * @date 2018/4/24 下午9:38
- */
- public class RelationshipPK implements Serializable {
- private Long fromUserId;
- private Long toUserId;
- @Column(name = "from_user_id", nullable = false)
- @Id
- public Long getFromUserId() {
- return fromUserId;
- }
- public void setFromUserId(Long fromUserId) {
- this.fromUserId = fromUserId;
- }
- @Column(name = "to_user_id", nullable = false)
- @Id
- public Long getToUserId() {
- return toUserId;
- }
- public void setToUserId(Long toUserId) {
- this.toUserId = toUserId;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- RelationshipPK that = (RelationshipPK) o;
- if (fromUserId != null ? !fromUserId.equals(that.fromUserId) : that.fromUserId != null) return false;
- if (toUserId != null ? !toUserId.equals(that.toUserId) : that.toUserId != null) return false;
- return true;
- }
- @Override
- public int hashCode() {
- int result = fromUserId != null ? fromUserId.hashCode() : 0;
- result = 31 * result + (toUserId != null ? toUserId.hashCode() : 0);
- return result;
- }
- }
启动项目,生成的数据表符合要求
二、获取数据
RelationshipRepository.java
- package com.liuyanzhao.forum.repository;
- import com.liuyanzhao.forum.entity.Relationship;
- import com.liuyanzhao.forum.entity.RelationshipPK;
- import org.springframework.data.jpa.repository.JpaRepository;
- import java.util.List;
- /**
- * @author 言曌
- * @date 2018/4/24 下午9:47
- */
- public interface RelationshipRepository extends JpaRepository<Relationship, RelationshipPK> {
- /**
- * 根据关注者id查找所有记录(查找关注的人的id)
- *
- * @param fromUserId
- * @return
- */
- List<Relationship> findByFromUserId(Long fromUserId);
- /**
- * 根据被关注者查找所有记录(查找粉丝的id)
- *
- * @param toUserId
- * @return
- */
- List<Relationship> findByToUserId(Long toUserId);
- /**
- * 根据关注者和被关注者id查找某条记录
- * @param fromUserId
- * @param toUserId
- * @return
- */
- Relationship findByFromUserIdAndToUserId(Long fromUserId, Long toUserId);
- }
三、测试类
- package com.liuyanzhao.forum.repository;
- import com.liuyanzhao.forum.entity.Relationship;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.test.context.junit4.SpringRunner;
- import java.util.List;
- import static org.junit.Assert.*;
- /**
- * @author 言曌
- * @date 2018/4/24 下午9:56
- */
- @SpringBootTest
- @RunWith(SpringRunner.class)
- public class RelationshipRepositoryTest {
- @Autowired
- private RelationshipRepository relationshipRepository;
- @Test
- public void save() {
- relationshipRepository.save(new Relationship(1L,2L));
- relationshipRepository.save(new Relationship(1L,3L));
- relationshipRepository.save(new Relationship(1L,4L));
- relationshipRepository.save(new Relationship(2L,3L));
- relationshipRepository.save(new Relationship(2L,4L));
- relationshipRepository.save(new Relationship(3L,4L));
- }
- @Test
- public void findByFromUserId() throws Exception {
- List<Relationship> relationshipList = relationshipRepository.findByFromUserId(1L);
- System.out.println(relationshipList);
- }
- @Test
- public void findByToUserId() throws Exception {
- List<Relationship> relationshipList = relationshipRepository.findByToUserId(4L);
- System.out.println(relationshipList);
- }
- @Test
- public void findByFromUserIdAndToUserId() throws Exception {
- Relationship relationship = relationshipRepository.findByFromUserIdAndToUserId(1L,2L);
- System.out.println();
- }
- }
s
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏