날짜: 2022년 10월 13일
social의 pk = user와 이어주는 fk
원래 다음과 같이 코드를 작성했었다.
@Entity
@Getter
@Setter
public class social {
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "userId")
private user userId;
private String twitter;
private String facebook;
private String discord;
private String link;
}
public interface SocialRepository extends JpaRepository<social, user>{
}
이렇게 작성하니 composite key는 serializable해야한다는 오류가 나왔다.
내 pk는 복합키가 아닌데 복합키 오류가 나는 것을 보고
Id와 fk가 겹치면 class로 받는게 아니라 뭔가 다르게 해야한다고 생각했다.
그리고 다음 방법으로 해결했다.
@Entity
@Getter
@Setter
public class social {
@Id
private String userId;
@OneToOne
@PrimaryKeyJoinColumn
private user user;
private String twitter;
private String facebook;
private String discord;
private String link;
}
public interface SocialRepository extends JpaRepository<social, String>{
}
여기서 중요한건 PrimaryKeyJoinColumn!
- JoinColumn
외래 미 매핑할 때 사용
primarykeyjoincolumn은 primary 키가 fk일 때 사용할 수 있다.
Java Persistence/Identity and Sequencing - Wikibooks, open books for an open world
PrimaryKeyJoinColumn (hibernate-jpa-2.1-api 1.0.0.Final API)
@RunWith(SpringRunner.class)
@SpringBootTest
public class SocialRepositoryTest {
@Autowired
private SocialRepository socialRepository;
@Test
@Transactional
public void read() {
List<social> socialList = socialRepository.findAll();
System.out.println(socialList);
Optional<social> social = socialRepository.findById("gkrry2723");
social.ifPresent(selectedWT -> {
System.out.println(social);
});
}
}
@Entity
@Getter @Setter
public class walletType {
@Id
private String name;
private String icon;
private Integer index;
@OneToMany(mappedBy = "walletType")
private List<wallet> wallets = new ArrayList<>();
}
public interface WalletRepository extends JpaRepository<wallet, String> {
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class WalletTest {
@Autowired
private WalletRepository walletRepository;
@Test
@Transactional
public void testWallet(){
wallet wallet = new wallet();
walletType walletType = new walletType();
walletType.setName("Metamask");
wallet.setAddress("testWallet");
wallet.setWalletType(walletType);
wallet savedWallet = walletRepository.save(wallet);
Optional<wallet> findWallet = walletRepository.findById("testWallet");
Assertions.assertThat(findWallet.get().getAddress()).isEqualTo(savedWallet.getAddress());
Assertions.assertThat(savedWallet).isEqualTo(findWallet.get());
}
}
@Entity
@Getter
@Setter
public class wallet {
@Id
private String address;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "walletType")
private walletType walletType;
@OneToMany(mappedBy = "wallet")
private List<userWallet> userWallets = new ArrayList<>();
}
public interface WalletRepository extends JpaRepository<wallet, String> {
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class WalletTest {
@Autowired
private WalletRepository walletRepository;
@Test
@Transactional
public void testWallet(){
wallet wallet = new wallet();
walletType walletType = new walletType();
walletType.setName("Metamask");
wallet.setAddress("testWallet");
wallet.setWalletType(walletType);
wallet savedWallet = walletRepository.save(wallet);
Optional<wallet> findWallet = walletRepository.findById("testWallet");
Assertions.assertThat(findWallet.get().getAddress()).isEqualTo(savedWallet.getAddress());
Assertions.assertThat(savedWallet).isEqualTo(findWallet.get());
}
}
@Entity
@Getter
@Setter
@DynamicInsert
@DynamicUpdate
public class userWallet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer index;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user")
private user user;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="walletAddress")
private wallet wallet;
@ColumnDefault("default")
private String chain;
@Column(updatable = false, nullable = false)
@CreationTimestamp
private Timestamp createdAt;
}
public interface UserWalletRepository extends JpaRepository<userWallet, Integer> {
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserWalletTest {
@Autowired
private UserWalletRepository userWalletRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private WalletRepository walletRepository;
@Test
@Transactional
public void testUserWallet(){
userWallet userWallet = new userWallet();
Optional<wallet> wallet = walletRepository.findById("0xf9F3Ea76C7Be559B4D4C9B3Ee2c3E05484A84420");
user user = userRepository.find("gkrry2723");
userWallet.setWallet(wallet.get());
userWallet.setUser(user);
userWallet savedUserWallet = userWalletRepository.save(userWallet);
}
}
오늘 여러가지 해보면서 table을 jpa로 옮기는 것에 감을 잡은 것 같다. erd cloud에 db 스키마 옮기면서 다음주까지 완성 할 수 있을 것 같다.
jpa 다 옮기면 repository에 대해 좀 더 깊이 공부해봐야겠다.