在我的要求中,我必须有一个名为Account的实体和另一个名为Transaction的实体。交易将包含从一个账户转移到另一个账户的资金。
例如:我可以将钱从John的账户转到Mary'account。我可以将钱从约翰的某个帐户转移到另一个约翰的帐户。
我正在尝试使用@JoinTable来解决这个经典场景,但不知怎的,我失去了一些观点,我基本上得到了“错误:关系xxx不存在”
完整日志
2018-01-27 20:51:55.601 INFO 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
Hibernate: alter table account_sources drop constraint FK_d6jfyffrk9ydsf4keiu1yhkco
2018-01-27 20:51:55.606 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table account_sources drop constraint FK_d6jfyffrk9ydsf4keiu1yhkco
2018-01-27 20:51:55.607 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "account_sources" does not exist
Hibernate: alter table account_sources drop constraint FK_s8l8nq2u3yjvjd9rpwqj8u1x5
2018-01-27 20:51:55.607 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table account_sources drop constraint FK_s8l8nq2u3yjvjd9rpwqj8u1x5
2018-01-27 20:51:55.607 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "account_sources" does not exist
Hibernate: alter table account_targets drop constraint FK_j0xd4pvy0biudmtycc9fsja57
2018-01-27 20:51:55.608 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table account_targets drop constraint FK_j0xd4pvy0biudmtycc9fsja57
2018-01-27 20:51:55.608 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "account_targets" does not exist
Hibernate: alter table account_targets drop constraint FK_hjcwotogjscfgt6jovlm1kau7
2018-01-27 20:51:55.609 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table account_targets drop constraint FK_hjcwotogjscfgt6jovlm1kau7
2018-01-27 20:51:55.609 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "account_targets" does not exist
Hibernate: alter table accounts drop constraint FK_e4w4av1wrhanry7t6mxt42nou
2018-01-27 20:51:55.610 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table accounts drop constraint FK_e4w4av1wrhanry7t6mxt42nou
2018-01-27 20:51:55.610 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: constraint "fk_e4w4av1wrhanry7t6mxt42nou" of relation "accounts" does not exist
Hibernate: drop table if exists account_sources cascade
Hibernate: drop table if exists account_targets cascade
Hibernate: drop table if exists accounts cascade
Hibernate: drop table if exists transactions cascade
Hibernate: drop table if exists users cascade
Hibernate: drop sequence hibernate_sequence
2018-01-27 20:51:55.627 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: drop sequence hibernate_sequence
2018-01-27 20:51:55.627 ERROR 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: sequence "hibernate_sequence" does not exist
Hibernate: create table account_sources (transactions_id int8 not null, sources_id int8 not null)
Hibernate: create table account_targets (transactions_id int8 not null, targets_id int8 not null)
Hibernate: create table accounts (id int8 not null, name varchar(255), user_id int8 not null, primary key (id))
Hibernate: create table transactions (id int8 not null, amount numeric(19, 2), primary key (id))
Hibernate: create table users (id int8 not null, address varchar(255), name varchar(255), primary key (id))
Hibernate: alter table account_sources add constraint FK_d6jfyffrk9ydsf4keiu1yhkco foreign key (sources_id) references accounts
Hibernate: alter table account_sources add constraint FK_s8l8nq2u3yjvjd9rpwqj8u1x5 foreign key (transactions_id) references transactions
Hibernate: alter table account_targets add constraint FK_j0xd4pvy0biudmtycc9fsja57 foreign key (targets_id) references accounts
Hibernate: alter table account_targets add constraint FK_hjcwotogjscfgt6jovlm1kau7 foreign key (transactions_id) references transactions
Hibernate: alter table accounts add constraint FK_e4w4av1wrhanry7t6mxt42nou foreign key (user_id) references users
Hibernate: create sequence hibernate_sequence
2018-01-27 20:51:55.690 INFO 7472 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
用户
@Entity
@Table(name = "USERS")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String address;
gets/sets
}
帐户
@Entity
@Table(name = "ACCOUNTS")
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@NotNull
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
//gets/sets
}
交易
@Entity
@Table(name = "TRANSACTIONS")
public class Transaction {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private BigDecimal amount;
@ManyToMany(cascade = { CascadeType.ALL })
@JoinTable(name = "account_sources")
private List<Account> sources;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "account_targets")
private List<Account> targets;
//gets/sets
}
架构postgres.sql
DROP TABLE IF EXISTS transactions;
DROP TABLE IF EXISTS accounts;
DROP TABLE IF EXISTS users;
CREATE TABLE users(id serial PRIMARY KEY, name VARCHAR(100), address VARCHAR(100));
CREATE TABLE accounts(id serial PRIMARY KEY, name VARCHAR(100), user_id bigint not null references users (id));
CREATE TABLE transactions(
id serial PRIMARY KEY,
name VARCHAR(100),
account_sources bigint not null references accounts (id),
account_targets bigint not null references accounts (id)
);