gpt4 book ai didi

java - Hibernate:如何处理DAO中的Session以正确设置关联?

转载 作者:行者123 更新时间:2023-12-01 12:33:30 25 4
gpt4 key购买 nike

我找不到使用 DAO 设置关联的解决方案。需要解释一下。有 3 个实体:银行、客户和账户。银行和客户是双向多对多关联,客户和账户是一对多关联。每个实体的 DAO 都有一个 GenericDAO 接口(interface)来实现。由于应用程序不是多线程,我决定通过在 DAO 接口(interface)实现的每种方法中打开和关闭 session 来实现每个请求 session “模式”。请看一下代码。问题描述如下代码。

POJO 银行

@Entity
@Table(name = "bank")
public class Bank {

private int bankID;
private String bankName;
private Set<Client> setOfClients;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "bank_id")
public int getBankID() {
return bankID;
}

public void setBankID(int bankID) {
this.bankID = bankID;
}

@Column(name = "bank_name")
public String getBankName() {
return bankName;
}

public void setBankName(String bankName) {
this.bankName = bankName;
}

@ManyToMany(mappedBy = "setOfBanks", fetch = FetchType.EAGER)
public Set<Client> getSetOfClients() {
return setOfClients;
}

public void setSetOfClients(Set<Client> setOfClients) {
this.setOfClients = setOfClients;
}
}**

客户端 POJO

@Entity
@Table(name = "client")
public class Client {

private int clientID;
private String firstName;
private String secondName;
private Set<Account> setOfAccounts;
private Set<Bank> setOfBanks;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "client_id")
public int getClientID() {
return clientID;
}

public void setClientID(int clientID) {
this.clientID = clientID;
}

@Column(name = "first_name")
public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

@Column(name = "second_name")
public String getSecondName() {
return secondName;
}

public void setSecondName(String secondName) {
this.secondName = secondName;
}

@OneToMany(mappedBy = "clientID", cascade = {CascadeType.ALL})
public Set<Account> getSetOfAccounts() {
return setOfAccounts;
}

public void setSetOfAccounts(Set<Account> setOfAccounts) {
this.setOfAccounts = setOfAccounts;
}

@ManyToMany(targetEntity = Bank.class, cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
@JoinTable(
name = "bank_client",
joinColumns = {@JoinColumn(name = "client_id", nullable = false, referencedColumnName = "client_id")},
inverseJoinColumns = {@JoinColumn(name = "bank_id", nullable = false, referencedColumnName = "bank_id")}
)
public Set<Bank> getSetOfBanks() {
return setOfBanks;
}

public void setSetOfBanks(Set<Bank> setOfBanks) {
this.setOfBanks = setOfBanks;
}
}

ClientDAO

public class ClientDAO<Client> implements GenericDAO<Client>{

private Session session;

@Override
public void saveEntity(Client entity) {
try {
session = SessionFactoryUtil.getSessionFactory().openSession();
session.getTransaction().begin();
session.save(entity);
session.getTransaction().commit();
} catch (HibernateException e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
session.close();
}
}

@Override
public void deleteEntity(Client entity) {
try {
session = SessionFactoryUtil.getSessionFactory().openSession();
session.getTransaction().begin();
session.delete(entity);
session.getTransaction().commit();
} catch (HibernateException e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
session.close();
}

}

@Override
public void updateEntity(Object entity) {
try {
session = SessionFactoryUtil.getSessionFactory().openSession();
session.getTransaction().begin();
session.update(entity);
session.getTransaction().commit();
} catch (HibernateException e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
session.close();
}
}

@Override
public Client getEntityByID(int id) {
Client client = null;
try {
session = SessionFactoryUtil.getSessionFactory().openSession();
session.getTransaction().begin();
client = (Client) session.get(model.entity.Client.class, id);
} catch (HibernateException e) {
session.getTransaction().rollback();
e.printStackTrace();
} finally {
session.close();
}
return client;
}

@Override
public List<Client> getAllEntities() {
List<Client> result = null;
try {
session = SessionFactoryUtil.getSessionFactory().openSession();
session.getTransaction().begin();
result = session.createQuery("from Client").list();
session.getTransaction().commit();
} catch (HibernateException e) {
if (session != null) {
session.getTransaction().rollback();
}
e.printStackTrace();
} finally {
session.close();
}
return result;
}
}

BankDAO 有类似的方法实现,但与银行实体规范相关

主要方法

public class Main {
public static void main(final String[] args) {

ClientDAO<Client> clientDAO = new ClientDAO<Client>();
BankDAO<Bank> bankDAO = new BankDAO<Bank>();

Client client = clientDAO.getEntityByID(11);
Bank bank = bankDAO.getEntityByID(3);

Set<Bank> setOfBanks = client.getSetOfBanks();
setOfBanks.add(bank);

Set<Client> setOfClients = bank.getSetOfClients();
setOfClients.add(client);
}
}

我没有提到有一个中间表,其中包含客户表和银行表的外键值(它需要反射(reflect)它们的多对多关联)。主方法调用的结果应该是中间表中新添加的值(客户表和银行表的外键)。证明该协会已成立。但是当我调用 main-method 时什么也没有发生。据我了解,当我 setOfBanks.add(bank)setOfClients.add(client) 时,对象会添加到与持久对象不相关的集合中,因为 session 已关闭。我不知道如何正确打开和关闭 session 以便在 session 内建立关联。希望我能清楚地描述这个问题。我真的很感激任何回复。

最佳答案

您所实现的看起来更像 session-per-operation anti-pattern比海事组织的每个请求 session 模式。 “每个请求 session ”通常是指客户端-服务器或 Web 应用程序中的一个请求,其中一个请求通常涉及多个数据库操作。

将该模式应用于像您这样的命令行应用程序比较困难,但在您的情况下,最接近请求的可能是从命令行运行应用程序。因此,我同意 nachokk 的观点,您应该在 main 方法的开头打开 session ,并在最后关闭它。

关于java - Hibernate:如何处理DAO中的Session以正确设置关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25751993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com