Skip to content

Commit 03416a8

Browse files
fax4everjrenaat
authored andcommitted
HHH-14241 Test ImplicitNamingStrategyComponentPathImpl with IdClass
1 parent 3a88b1c commit 03416a8

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.id.idclass;
8+
9+
import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;
10+
import org.hibernate.cfg.Configuration;
11+
12+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
13+
import org.junit.Test;
14+
15+
public class IdClassNamingStrategyTest extends BaseCoreFunctionalTestCase {
16+
17+
@Override
18+
protected Class<?>[] getAnnotatedClasses() {
19+
return new Class<?>[] { MyEntity.class };
20+
}
21+
22+
@Override
23+
protected void configure(Configuration configuration) {
24+
/*
25+
* With this implicit naming strategy, we got the following mapping:
26+
*
27+
* create table MyEntity (
28+
* id_idA bigint not null,
29+
* id_idB bigint not null,
30+
* _identifierMapper_idA bigint not null, <-- ??
31+
* _identifierMapper_idB bigint not null, <-- ??
32+
* notes varchar(255),
33+
* primary key (id_idA, id_idB)
34+
* )
35+
*/
36+
configuration.setImplicitNamingStrategy( new ImplicitNamingStrategyComponentPathImpl() );
37+
}
38+
39+
@Test
40+
public void test() {
41+
inTransaction( ( session ) -> {
42+
MyEntity entity = new MyEntity();
43+
entity.setId( new MyEntityId( 739L, 777L ) );
44+
45+
session.persist( entity );
46+
} );
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.id.idclass;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.Id;
11+
import javax.persistence.IdClass;
12+
13+
@Entity
14+
@IdClass( MyEntityId.class )
15+
public class MyEntity {
16+
17+
@Id
18+
private Long idA;
19+
20+
@Id
21+
private Long idB;
22+
23+
private String notes;
24+
25+
public MyEntityId getId() {
26+
return new MyEntityId( idB, idA );
27+
}
28+
29+
public void setId(MyEntityId id) {
30+
this.idB = id.getIdA();
31+
this.idA = id.getIdB();
32+
}
33+
34+
public String getNotes() {
35+
return notes;
36+
}
37+
38+
public void setNotes(String notes) {
39+
this.notes = notes;
40+
}
41+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.id.idclass;
8+
9+
import java.io.Serializable;
10+
import java.util.Objects;
11+
12+
public class MyEntityId implements Serializable {
13+
14+
private Long idA;
15+
private Long idB;
16+
17+
public MyEntityId(Long generatedId, Long providedId) {
18+
this.idA = generatedId;
19+
this.idB = providedId;
20+
}
21+
22+
private MyEntityId() {
23+
}
24+
25+
public Long getIdA() {
26+
return idA;
27+
}
28+
29+
public void setIdA(Long idA) {
30+
this.idA = idA;
31+
}
32+
33+
public Long getIdB() {
34+
return idB;
35+
}
36+
37+
public void setIdB(Long idB) {
38+
this.idB = idB;
39+
}
40+
41+
@Override
42+
public boolean equals(Object o) {
43+
if ( this == o ) {
44+
return true;
45+
}
46+
if ( o == null || getClass() != o.getClass() ) {
47+
return false;
48+
}
49+
MyEntityId pk = (MyEntityId) o;
50+
return Objects.equals( idA, pk.idA ) &&
51+
Objects.equals( idB, pk.idB );
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hash( idA, idB );
57+
}
58+
}

0 commit comments

Comments
 (0)