Skip to content

Commit 8e031c4

Browse files
authored
[impr-manual-id] manually set id before save() (#18305)
1 parent 4132a6c commit 8e031c4

File tree

4 files changed

+103
-9
lines changed

4 files changed

+103
-9
lines changed

persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.hibernate.service.ServiceRegistry;
1414

1515
import com.baeldung.hibernate.entities.DeptEmployee;
16+
import com.baeldung.hibernate.pojo.BaseballPlayer;
1617
import com.baeldung.hibernate.pojo.Course;
1718
import com.baeldung.hibernate.pojo.Employee;
1819
import com.baeldung.hibernate.pojo.EntityDescription;
@@ -25,6 +26,7 @@
2526
import com.baeldung.hibernate.pojo.Post;
2627
import com.baeldung.hibernate.pojo.Product;
2728
import com.baeldung.hibernate.pojo.Student;
29+
import com.baeldung.hibernate.pojo.TennisPlayer;
2830
import com.baeldung.hibernate.pojo.TemporalValues;
2931
import com.baeldung.hibernate.pojo.User;
3032
import com.baeldung.hibernate.pojo.UserProfile;
@@ -86,6 +88,8 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
8688
metadataSources.addAnnotatedClass(DeptEmployee.class);
8789
metadataSources.addAnnotatedClass(com.baeldung.hibernate.entities.Department.class);
8890
metadataSources.addAnnotatedClass(Post.class);
91+
metadataSources.addAnnotatedClass(TennisPlayer.class);
92+
metadataSources.addAnnotatedClass(BaseballPlayer.class);
8993

9094
Metadata metadata = metadataSources.getMetadataBuilder()
9195
.build();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
@Entity
7+
public class BaseballPlayer {
8+
9+
@Id
10+
private Long playerId; // Long instead of long
11+
private String name;
12+
13+
public BaseballPlayer(String name) {
14+
this.name = name;
15+
}
16+
17+
public Long getPlayerId() {
18+
return playerId;
19+
}
20+
21+
public void setPlayerId(Long playerId) {
22+
this.playerId = playerId;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
@Entity
7+
public class TennisPlayer {
8+
9+
@Id
10+
private long playerId;
11+
private String name;
12+
13+
public TennisPlayer(String name) {
14+
this.name = name;
15+
}
16+
17+
public Long getPlayerId() {
18+
return playerId;
19+
}
20+
21+
public void setPlayerId(Long playerId) {
22+
this.playerId = playerId;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(String name) {
30+
this.name = name;
31+
}
32+
}

persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/IdentifiersIntegrationTest.java

+35-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
package com.baeldung.hibernate;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
6+
import java.io.IOException;
7+
8+
import org.hibernate.Session;
9+
import org.hibernate.Transaction;
10+
import org.hibernate.id.IdentifierGenerationException;
311
import org.junit.After;
412
import org.junit.Before;
513
import org.junit.Test;
6-
import org.hibernate.Transaction;
7-
import java.io.IOException;
8-
import org.hibernate.Session;
914

10-
import static org.assertj.core.api.Assertions.assertThat;
11-
12-
import com.baeldung.hibernate.pojo.Product;
15+
import com.baeldung.hibernate.pojo.BaseballPlayer;
1316
import com.baeldung.hibernate.pojo.Course;
1417
import com.baeldung.hibernate.pojo.OrderEntry;
1518
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
1619
import com.baeldung.hibernate.pojo.OrderEntryPK;
20+
import com.baeldung.hibernate.pojo.Product;
1721
import com.baeldung.hibernate.pojo.Student;
22+
import com.baeldung.hibernate.pojo.TennisPlayer;
1823
import com.baeldung.hibernate.pojo.User;
1924
import com.baeldung.hibernate.pojo.UserProfile;
2025

2126
public class IdentifiersIntegrationTest {
27+
2228
private Session session;
2329

2430
private Transaction transaction;
@@ -37,12 +43,32 @@ public void tearDown() {
3743
}
3844

3945
@Test
40-
public void whenSaveSimpleIdEntities_thenOk() {
46+
public void whenSavingTennisPlayerWithoutAnId_thenSavingEntityOk() {
47+
TennisPlayer tennisPlayer = new TennisPlayer("Tom");
48+
session.save(tennisPlayer);
49+
assertThat(tennisPlayer.getPlayerId()).isEqualTo(0L);
50+
}
51+
52+
@Test
53+
public void whenSavingBaseballPlayerWithoutAnId_thenSavingEntityFails() {
54+
BaseballPlayer baseballPlayer = new BaseballPlayer("Jerry");
55+
assertThatThrownBy(() -> session.save(baseballPlayer)).isInstanceOf(IdentifierGenerationException.class)
56+
.hasMessageContaining("ids for this class must be manually assigned before calling save()");
57+
}
58+
@Test
59+
public void whenSavingBaseballPlayerWithAManualId_thenSavingEntityOK() {
60+
BaseballPlayer baseballPlayer = new BaseballPlayer("Jerry");
61+
baseballPlayer.setPlayerId(42L);
62+
session.save(baseballPlayer);
63+
}
64+
65+
@Test
66+
public void whenSaveSequenceIdEntities_thenOk() {
4167
Student student = new Student();
4268
session.save(student);
4369
User user = new User();
4470
session.save(user);
45-
71+
4672
assertThat(student.getStudentId()).isEqualTo(1L);
4773
assertThat(user.getUserId()).isEqualTo(4L);
4874

@@ -103,4 +129,4 @@ public void whenSaveDerivedIdEntity_thenOk() {
103129
assertThat(profile.getProfileId()).isEqualTo(user.getUserId());
104130
}
105131

106-
}
132+
}

0 commit comments

Comments
 (0)