gpt4 book ai didi

java - 如何获取依赖对象

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:43 24 4
gpt4 key购买 nike

我正在尝试获取银行中存在的帐户,但列表为空,即使数据库中存在两者之间的链接。知道数据库和我的程序之间的交互工作正常,只是bank.getAccounts() 返回一个空列表。这是我的代码。我想做的是:

public class Main {

public static void main(String[] args) {
Bank bank = new Bank();
bank.setName("XXX");
bank.setStreet("XXX");
bank.setPc("XXX");
bank.save();

Account c = new account();
c.setBalance(10);
c.setBank(bank);
c.save();

account c2 = new account();
c2.setBalance(20);
c2.setBank(bank);
c2.save();

for(account c : bank.getAccounts())
System.out.println("account n°: " + c.getId() + ", balance: " + c.getBalance());



}

}

银行类是:

@Entity
@Table(name = "BANK")
public class Bank implements Serializable {


/**
*
*/
private static final long serialVersionUID = 8199961750065782869L;

static private BankDAO dao;

private long id;
private String name;
private String street;
private String pc;

private Set<Account> accounts;

public Bank() {
super();
this.accounts = new HashSet<>();
}


@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getPc() {
return pc;
}

public void setPc(String pc) {
this.pc = pc;
}



@OneToMany(cascade = CascadeType.ALL, mappedBy="bank")
public Set<Account> getAccounts() {
return accounts;
}

public void setAccounts(Set<Account> accounts) {
this.accounts = accounts;
}

// Equals and hashCode implementations deleted

@Transient
private BankDAO getDao() {
if(Bank.dao == null)
dao = BankDAO.getDAO();
return Bank.dao;
}


public long save() {
Bank.dao = this.getDao();
return dao.save(this);
}


public int delete() {
Bank.dao = this.getDao();
return dao.delete(this);
}
}

Accounts 类代码:

@Entity
@Table(name = "Account")
public class Account implements Serializable{

private static final long serialVersionUID = 7224745445343369682L;

static private AccountDAO dao;

public long id;
public int balance;
public Bank bank;

public Account() {
super();
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public int getBalance() {
return balance;
}

public void setBalance(int balance) {
this.balance = balance;
}


@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="bank_id", nullable=false)
@Id
public Bank getBank() {
return bank;
}

public void setBank(Bank bank) {
this.bank = bank;
}

@Transient
private AccountDAO getDao() {
if(Account.dao == null)
dao = AccountDAO.getDAO();
return Account.dao;
}

/*
* save the state of the object: save or update
* @return: the id of the object
*/
public long save() {
Account.dao = this.getDao();
return dao.save(this);
}


public int delete() {
Account.dao = this.getDao();
return dao.delete(this);
}
}

日志是:

Feb 13, 2018 11:38:54 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.12.Final}
Feb 13, 2018 11:38:54 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 13, 2018 11:38:54 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
Feb 13, 2018 11:38:54 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Feb 13, 2018 11:38:55 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Feb 13, 2018 11:38:55 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/hibernateDataBase]
Feb 13, 2018 11:38:55 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=postgres, password=****}
Feb 13, 2018 11:38:55 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Feb 13, 2018 11:38:55 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Feb 13, 2018 11:38:55 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
Feb 13, 2018 11:38:55 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Feb 13, 2018 11:38:55 PM org.hibernate.type.BasicTypeRegistry register
INFO: HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@67080771
Feb 13, 2018 11:38:55 PM org.hibernate.mapping.RootClass checkCompositeIdentifier
WARN: HHH000038: Composite-id class does not override equals(): business.Account
Feb 13, 2018 11:38:55 PM org.hibernate.mapping.RootClass checkCompositeIdentifier
WARN: HHH000039: Composite-id class does not override hashCode(): business.Account
Hibernate: alter table ACCOUNT drop constraint FKc0nxjvf82u84r0b01m367tss3Feb 13, 2018 11:38:56 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@10650953] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.

Hibernate: drop table if exists BANK cascade
Hibernate: drop table if exists ACCOUNT cascade
Hibernate: drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start 1 increment 1
Feb 13, 2018 11:38:56 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@7db82169] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: create table BANK (id int8 not null, pc varchar(255), name varchar(255), street varchar(255), primary key (id))
Hibernate: create table ACCOUNT (id int8 not null, balance int4 not null, bank_id int8 not null, primary key (id, bank_id))
Hibernate: alter table ACCOUNT add constraint FKc0nxjvf82u84r0b01m367tss3 foreign key (bank_id) references BANK
Feb 13, 2018 11:38:56 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@3276732'
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into BANK (pc, name, street, id) values (?, ?, ?, ?)
Hibernate: select account_.id, account_.bank_id, account_.balance as balance2_1_ from ACCOUNT account_ where account_.id=? and account_.bank_id=?
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into ACCOUNT (balance, id, bank_id) values (?, ?, ?)
Hibernate: select account_.id, account_.bank_id, account_.balance as balance2_1_ from ACCOUNT account_ where account_.id=? and account_.bank_id=?
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into ACCOUNT (balance, id, bank_id) values (?, ?, ?)

最佳答案

Bank bank = new Bank();
bank.setName("XXX");
bank.setStreet("XXX");
bank.setPc("XXX");
bank.save(); -- checkpoint 1

Account c = new account();
c.setBalance(10);
c.setBank(bank); -- checkpoint 2
c.save();

account c2 = new account();
c2.setBalance(20);
c2.setBank(bank);
c2.save(); -- checkpoint 3

它不会以这种方式工作,正如您所看到的 @ checkpioint 1 您拥有 Bank 对象,并且不会改变其状态。我猜你想通过检查点 2 和 3 进行银行->帐户填充,但这不会发生。

您要做的就是简单地手动放置该关系

bank.getAccounts().add(c1); 

但是,为了使关系像您期望的那样工作(因为银行已经初始化,因此不会发生延迟获取),您需要通过例如查询再次实际获取您的银行。然后,新返回的银行将具有统一的集合代理,并且确实会执行额外的选择(如果未指定急切获取)以获取与该银行关联的帐户集合。

关于java - 如何获取依赖对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48776676/

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