Expanding model
This commit is contained in:
parent
1d1c40fe30
commit
7fb19685df
4 changed files with 123 additions and 25 deletions
21
src/main/java/org/acme/Address.java
Normal file
21
src/main/java/org/acme/Address.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package org.acme;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="addresses")
|
||||
public class Address extends PanacheEntity {
|
||||
|
||||
@ManyToOne
|
||||
@JsonIgnore
|
||||
public User user;
|
||||
public String street;
|
||||
public String zip_code;
|
||||
public String city;
|
||||
public String country;
|
||||
}
|
||||
10
src/main/java/org/acme/Phone.java
Normal file
10
src/main/java/org/acme/Phone.java
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package org.acme;
|
||||
|
||||
import jakarta.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class Phone {
|
||||
|
||||
public String type;
|
||||
public String number;
|
||||
}
|
||||
|
|
@ -1,19 +1,30 @@
|
|||
package org.acme;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
||||
import jakarta.persistence.CollectionTable;
|
||||
import jakarta.persistence.ElementCollection;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name="users")
|
||||
public class User extends PanacheEntity {
|
||||
|
||||
|
||||
public String first_name;
|
||||
public String last_name;
|
||||
public String street;
|
||||
public String zip_code;
|
||||
public String city;
|
||||
public String country;
|
||||
public String phone;
|
||||
public String birthday;
|
||||
public String email;
|
||||
public String birthday;
|
||||
|
||||
@OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
|
||||
public List<Address> addresses = new ArrayList<>();
|
||||
|
||||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
@CollectionTable(name ="phone_numbers", joinColumns = @JoinColumn(name="user_id"))
|
||||
public List<Phone> phone_numbers = new ArrayList<>();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,21 +51,42 @@ public class UserResource {
|
|||
public void dropAndCreateTable() throws Exception {
|
||||
try (Connection connection = ds.getConnection()) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
statement.setQueryTimeout(30);
|
||||
statement.executeUpdate("DROP INDEX IF EXISTS addresses_user_id");
|
||||
statement.executeUpdate("DROP INDEX IF EXISTS phone_numbers_user_id");
|
||||
statement.executeUpdate("DROP TABLE IF EXISTS addresses");
|
||||
statement.executeUpdate("DROP TABLE IF EXISTS phone_numbers");
|
||||
statement.executeUpdate("DROP TABLE IF EXISTS users");
|
||||
statement.executeUpdate("""
|
||||
CREATE TABLE users(
|
||||
id INTEGER PRIMARY KEY,
|
||||
first_name TEXT,
|
||||
last_name TEXT,
|
||||
email TEXT,
|
||||
birthday TEXT
|
||||
)
|
||||
""");
|
||||
statement.executeUpdate("""
|
||||
CREATE TABLE addresses(
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id INTEGER,
|
||||
street TEXT,
|
||||
zip_code TEXT,
|
||||
city TEXT,
|
||||
country TEXT,
|
||||
phone TEXT,
|
||||
birthday TEXT
|
||||
FOREIGN KEY(user_id) REFERENCES users(id)
|
||||
)
|
||||
""");
|
||||
statement.executeUpdate("CREATE INDEX addresses_user_id ON addresses(user_id)");
|
||||
statement.executeUpdate("""
|
||||
CREATE TABLE phone_numbers(
|
||||
user_id INTEGER,
|
||||
type TEXT,
|
||||
number TEXT,
|
||||
PRIMARY KEY(user_id, type)
|
||||
FOREIGN KEY(user_id) REFERENCES users(id)
|
||||
)
|
||||
""");
|
||||
statement.executeUpdate("CREATE INDEX phone_numbers_user_id ON phone_numbers(user_id)");
|
||||
userCount = 0;
|
||||
}
|
||||
}
|
||||
|
|
@ -81,26 +102,54 @@ public class UserResource {
|
|||
try (Connection connection = ds.getConnection()) {
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
try (PreparedStatement ps = connection.prepareStatement("""
|
||||
try (PreparedStatement insertUser = connection.prepareStatement("""
|
||||
INSERT INTO users
|
||||
(first_name, last_name, street, zip_code, city, country, phone, birthday)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""")) {
|
||||
(first_name, last_name, email, birthday)
|
||||
VALUES(?, ?, ?, ?)
|
||||
RETURNING id
|
||||
""", Statement.RETURN_GENERATED_KEYS);
|
||||
PreparedStatement insertAddress = connection.prepareStatement("""
|
||||
INSERT INTO addresses
|
||||
(user_id, street, zip_code, city, country)
|
||||
VALUES(?, ?, ?, ?, ?)
|
||||
""");
|
||||
PreparedStatement insertPhoneNo = connection.prepareStatement("""
|
||||
INSERT INTO phone_numbers
|
||||
(user_id, type, number)
|
||||
VALUES(?, ?, ?)
|
||||
""")) {
|
||||
for (int n = 1; n <= numberOfRecords; n++) {
|
||||
Address address = faker.address();
|
||||
|
||||
ps.setString(1, faker.name().firstName());
|
||||
ps.setString(2, faker.name().lastName());
|
||||
ps.setString(3, address.streetAddress());
|
||||
ps.setString(4, address.zipCode());
|
||||
ps.setString(5, address.city());
|
||||
ps.setString(6, address.country());
|
||||
ps.setString(7, faker.phoneNumber().cellPhone());
|
||||
ps.setString(8,
|
||||
insertUser.setString(1, faker.name().firstName());
|
||||
insertUser.setString(2, faker.name().lastName());
|
||||
insertUser.setString(3, faker.internet().emailAddress());
|
||||
insertUser.setString(4,
|
||||
LocalDate.ofInstant(faker.date().birthday().toInstant(), ZoneId.of("UTC")).toString());
|
||||
ps.execute();
|
||||
insertUser.execute();
|
||||
long userId = getGeneratedId(insertUser);
|
||||
|
||||
long addressCount = ThreadLocalRandom.current().nextInt(1, 5);
|
||||
for (int a = 0; a < addressCount; a++) {
|
||||
Address address = faker.address();
|
||||
insertAddress.setLong(1, userId);
|
||||
insertAddress.setString(2, address.streetAddress());
|
||||
insertAddress.setString(3, address.zipCode());
|
||||
insertAddress.setString(4, address.city());
|
||||
insertAddress.setString(5, address.country());
|
||||
insertAddress.execute();
|
||||
}
|
||||
|
||||
insertPhoneNo.setLong(1, userId);
|
||||
insertPhoneNo.setString(2, "cell");
|
||||
insertPhoneNo.setString(3, faker.phoneNumber().cellPhone());
|
||||
insertPhoneNo.execute();
|
||||
|
||||
insertPhoneNo.setLong(1, userId);
|
||||
insertPhoneNo.setString(2, "home");
|
||||
insertPhoneNo.setString(3, faker.phoneNumber().phoneNumber());
|
||||
insertPhoneNo.execute();
|
||||
}
|
||||
}
|
||||
|
||||
connection.commit();
|
||||
userCount = getUserCount();
|
||||
|
||||
|
|
@ -109,6 +158,13 @@ public class UserResource {
|
|||
}
|
||||
}
|
||||
|
||||
private long getGeneratedId(PreparedStatement insertUser) throws SQLException {
|
||||
try(ResultSet res = insertUser.getResultSet()) {
|
||||
res.next();
|
||||
return res.getLong(1);
|
||||
}
|
||||
}
|
||||
|
||||
private int getUserCount() throws SQLException {
|
||||
try (Connection connection = ds.getConnection()) {
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue