source 는 Github 에 있습니다.
Hibernate:
select
nogenerati0_.id as id1_5_0_,
nogenerati0_.name as name2_5_0_
from
no_generative_strategy nogenerati0_
where
nogenerati0_.id=?
@Test
public void ID_생성전략이_없을떄_동작방식_테스트() throws Exception {
// given
NoGenerativeStrategy noGenerativeStrategy =
NoGenerativeStrategy.builder()
.id(2L)
.name("test")
.build();
// when
noGenerativeStrategyRepository.save(noGenerativeStrategy);
Optional<NoGenerativeStrategy> result = noGenerativeStrategyRepository.findById(2L);
// then
Assert.assertThat(result.get().getName(), is("test"));
}
@Entity
@Getter
@NoArgsConstructor
public class NoGenerativeStrategy {
@Id
private Long id;
@Column
private String name;
@Builder
public NoGenerativeStrategy(Long id, String name) {
this.id = id;
this.name = name;
}
}
Hibernate:
insert
into
identity_strategy
(id, name)
values
(null, ?)
@RunWith(SpringRunner.class)
@DataJpaTest
public class IdentityStrategyRepositoryTest {
@Autowired
private IdentityStrategyRepository identityStrategyRepository;
@Test
public void IDENTITY_생성전략일_경우_동작방식_테스트() throws Exception {
// given
IdentityStrategy identityStrategy =
IdentityStrategy.builder()
.name("test")
.build();
// when
IdentityStrategy result = identityStrategyRepository.save(identityStrategy);
// then
Assert.assertThat(result.getId(), is(1L));
}
}
@Entity
@Getter
@NoArgsConstructor
public class IdentityStrategy {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@Builder
public IdentityStrategy(String name) {
this.name = name;
}
}
@RunWith(SpringRunner.class)
@DataJpaTest
public class SequenceStrategyRepositoryTest {
@Autowired
private SequenceStrategyRepository sequenceStrategyRepository;
@Test
public void 시퀀스_전략생성_방식_테스트() throws Exception {
// given
SequenceStrategy sequenceStrategy = SequenceStrategy.builder()
.name("test")
.build();
SequenceStrategy sequenceStrategy2 = SequenceStrategy.builder()
.name("test22")
.build();
// when
SequenceStrategy result1 = sequenceStrategyRepository.save(sequenceStrategy);
SequenceStrategy result2 = sequenceStrategyRepository.save(sequenceStrategy2);
// then
Assert.assertThat(result1.getId(), is(1L));
Assert.assertThat(result2.getId(), is(2L));
}
}
@Entity
@Getter
@NoArgsConstructor
@SequenceGenerator(
name="TEST_SEQUENCE_GENERATOR", // 제너레이터명
sequenceName="TEST_SEQUENCE", // 시퀀스명
initialValue= 1, // 시작 값
allocationSize= 1 // 할당할 범위 사이즈
)
public class SequenceStrategy {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TEST_SEQUENCE_GENERATOR")
private Long id;
@Column
private String name;
@Builder
public SequenceStrategy(String name) {
this.name = name;
}
}
CREATE SEQUENCE TEST_SEQ START WITH 1 INCREMENT BY 50;