Skip to content

Commit d1e4a3c

Browse files
committed
commit
1 parent 747330c commit d1e4a3c

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

hibernate/ifinances/src/main/java/com/rakeshv/ifinances/models/Account.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
import javax.persistence.Entity;
1212
import javax.persistence.EnumType;
1313
import javax.persistence.Enumerated;
14+
import javax.persistence.FetchType;
1415
import javax.persistence.GeneratedValue;
1516
import javax.persistence.GenerationType;
1617
import javax.persistence.Id;
1718
import javax.persistence.JoinColumn;
1819
import javax.persistence.JoinTable;
1920
import javax.persistence.ManyToMany;
2021
import javax.persistence.ManyToOne;
22+
import javax.persistence.NamedQueries;
23+
import javax.persistence.NamedQuery;
2124
import javax.persistence.OneToMany;
2225
import javax.persistence.SequenceGenerator;
2326
import javax.persistence.Table;
@@ -35,6 +38,10 @@
3538
@Builder
3639
@Entity
3740
@Table(name = "account")
41+
@NamedQueries({
42+
@NamedQuery(name = "Account.largeDeposits", query = "select distinct t.account from Transaction t " +
43+
"where t.amount > 500 and lower(t.transactionType) = 'deposit' ")
44+
})
3845
public class Account {
3946
@Id
4047
// AUTO is the default generation strategy
@@ -54,7 +61,7 @@ public class Account {
5461
inverseJoinColumns=@JoinColumn(name="USER_ID"))
5562
private Set<User> users = new HashSet<>();
5663

57-
@ManyToOne
64+
@ManyToOne(fetch = FetchType.LAZY)
5865
@JoinColumn(name="BANK_ID")
5966
private Bank bank;
6067

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.rakeshv.ifinances.models;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import javax.persistence.Column;
9+
import javax.persistence.Entity;
10+
import javax.persistence.Id;
11+
import javax.persistence.Table;
12+
13+
@Data
14+
@AllArgsConstructor
15+
@NoArgsConstructor
16+
@Builder
17+
@Entity
18+
@Table(name = "v_user_credentials")
19+
public class UserCredentialView {
20+
@Id
21+
@Column(name = "USER_ID")
22+
private Long userId;
23+
24+
@Column(name = "FIRST_NAME")
25+
private String firstName;
26+
27+
@Column(name = "LAST_NAME")
28+
private String lastName;
29+
30+
@Column(name = "USERNAME")
31+
private String userName;
32+
33+
@Column(name = "PASSWORD")
34+
private String password;
35+
}

hibernate/notes.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ more joins are needed. may lead to bad performance
131131
In our example, ticket_number, origin, price, destination are duplicated in both tables of subclass
132132
The deeper the hierarchy, the more deeper the joins needed
133133

134-
3. table per concrete entity. no table for super class.
134+
3. TABLE_PER_CLASS : table per concrete entity. no table for super class.
135135
subclass has separate table
136136
all properties of root class are mapped to columns of each subclass
137137
poor support for polymorphism
@@ -148,10 +148,19 @@ use AttributeConverter between state to db column
148148

149149
Entity lifecycle
150150

151+
152+
Transient
153+
Persistent
154+
Removed
155+
Detached
156+
157+
151158
entitymanager is an interface to interact with persistence context
152159
Entity states
153160
new, managed, removed, detached, transient
154161

162+
Transient - object created with new operator but is not yet associated with db row
163+
155164
persist(), remove(), merge(), find(), lock(), detach(), refresh()
156165
when entity is loaded from db then they are in Managed state
157166

0 commit comments

Comments
 (0)