-
Notifications
You must be signed in to change notification settings - Fork 40
/
Album.java
125 lines (105 loc) · 3.21 KB
/
Album.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright 2019-2023 Google LLC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package com.example.entities;
import com.example.entities.Album.AlbumId;
import com.google.cloud.spanner.hibernate.Interleaved;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;
import org.hibernate.annotations.JdbcTypeCode;
import org.hibernate.type.SqlTypes;
/** Sample Hibernate entity. */
@Entity
@Interleaved(parentEntity = Singer.class, cascadeDelete = true)
@IdClass(AlbumId.class)
public class Album {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JdbcTypeCode(SqlTypes.CHAR)
private UUID albumId;
@Id
@ManyToOne
@JoinColumn(name = "singerId")
@JdbcTypeCode(SqlTypes.CHAR)
private Singer singer;
private String title;
public Album(Singer singer, String title) {
this.singer = singer;
this.title = title;
}
public Album() {}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Singer getSinger() {
return singer;
}
public void setSinger(Singer singer) {
this.singer = singer;
}
@Override
public String toString() {
return "Album{"
+ "albumId="
+ albumId
+ ", singer="
+ singer.getName()
+ ", title='"
+ title
+ '\''
+ '}';
}
/** The embedded key for an entity using the {@link Interleaved} annotation. */
public static class AlbumId implements Serializable {
// The embedded key of an interleaved table contain all of the Primary Key fields
// of the parent interleaved entity and be named identically. In this case: singerId.
Singer singer;
@JdbcTypeCode(SqlTypes.CHAR)
UUID albumId;
public AlbumId(Singer singer, UUID albumId) {
this.singer = singer;
this.albumId = albumId;
}
public AlbumId() {}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AlbumId albumId1 = (AlbumId) o;
return Objects.equals(singer, albumId1.singer) && Objects.equals(albumId, albumId1.albumId);
}
@Override
public int hashCode() {
return Objects.hash(singer, albumId);
}
}
}