@Entity
@Getter
@Setter
@DynamicInsert
@DynamicUpdate
public class profileImg {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer index;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="user")
private user user;
private String imgUrl;
@ColumnDefault("false")
private Boolean isHide;
@Column(updatable = false, nullable = false)
@CreationTimestamp
private Timestamp createdAt;
}
@Repository
public interface ProfileImgRepository extends JpaRepository<profileImg, Integer> {
List<profileImg> findByUser(user user);
}
user 객체로 user의 모든 profileImg를 찾는 함수도 추가해주었다. 위의 내용도 잘 하는지 테스트 해보고 싶었기 때문!@RunWith(SpringRunner.class)
@SpringBootTest
public class profileImgRepositoryTest {
@Autowired
private ProfileImgRepository profileImgRepository;
@Autowired
private UserRepository userRepository;
@Test
@Transactional
public void test(){
List<profileImg> profileImgList = profileImgRepository.findAll();
System.out.println(profileImgList);
user user = userRepository.find("gkrry2723");
List<profileImg> profileImgfind1 = profileImgRepository.findByUser(user);
System.out.println(profileImgfind1);
profileImg profileImg = new profileImg();
profileImg.setUser(user);
profileImg.setImgUrl("testUrl");
profileImg savedProfileImg = profileImgRepository.save(profileImg);
List<profileImg> profileImgfind2 = profileImgRepository.findByUser(user);
Assertions.assertThat(profileImgfind2.get(profileImgfind2.size()-1)).isEqualTo(savedProfileImg);
}
}
@Entity
@Getter
@Setter
@DynamicInsert
@DynamicUpdate
public class friend {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer index;
@ManyToOne
@JoinColumn(name="user")
private user user;
@ManyToOne
@JoinColumn(name="friend")
private user friend;
private String friendName;
@Column(updatable = false, nullable = false)
@CreationTimestamp
private Timestamp createdAt;
}
public interface FriendRepository extends JpaRepository<friend, Integer> {
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class FriendRepositoryTest {
@Autowired
private FriendRepository friendRepository;
@Autowired
private UserRepository userRepository;
@Test
@Transactional
public void test(){
List<friend> friendList = friendRepository.findAll();
for(friend friend: friendList){
System.out.println(friend.getIndex() + " " + friend.getFriendName());
}
user user1 = userRepository.find("gkrry2723");
user user2 = userRepository.find("goyangtwo");
friend friend = new friend();
friend.setUser(user1);
friend.setFriend(user2);
friend.setFriendName("test");
friend savedFriend = friendRepository.save(friend);
List<friend> friendList2 = friendRepository.findAll();
Assertions.assertThat(savedFriend).isEqualTo(friendList2.get(friendList2.size()-1));
}
}