此处Customer_id
是加入列。我在oneToMany关系中有两个表客户和地址。对于两者都有主键是自动增量。
很少有要点:
在 Customer.java
中@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="customer_id", unique=true, nullable=false)
private long customerId;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name="address", joinColumns={@JoinColumn(name="customer_id", referencedColumnName="customer_id")}, inverseJoinColumns={@JoinColumn(name="account_id", referencedColumnName="account_id")})
private List<Address> addresses;
在 Address.java
中@Column(name="customer_id", unique=true, nullable=false)
private int customerId;
表格:
CREATE TABLE `customer` (
`customer_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`encrypt_key` varchar(200) NOT NULL,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`email` varchar(50) DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`create_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`self_description` varchar(2000) NOT NULL,
PRIMARY KEY (`customer_id`),
KEY `idx_last_name` (`last_name`)
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8;
CREATE TABLE `address` (
`account_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`customer_id` smallint(5) unsigned NOT NULL,
`address_type` varchar(15) NOT NULL, -- Office, Branch-1, Branch-2,
`door_num` varchar(50) NOT NULL,
`landmark` varchar(150) DEFAULT NULL,
`street` varchar(50) DEFAULT NULL,
`area_name` varchar(25) NOT NULL,
`district` varchar(25) NOT NULL,
`city` varchar(25) NOT NULL,
`postal_code` varchar(10) DEFAULT NULL,
`phone1` varchar(20) NOT NULL,
`phone2` varchar(20),
`create_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`account_id`),
KEY `idx_fk_city` (`city`),
CONSTRAINT `fk_customer_id` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8;
虽然我正在创建一个新客户,但我是这样的错误。
SqlExceptionHelper:144 - SQL错误:1452,SQLState:23000 2015-12-10 13:39:32错误SqlExceptionHelper:146 - 无法添加或更新子项 row:外键约束失败(
mobily
。address
,CONSTRAINTfk_customer_id
外键(customer_id
)参考customer
(customer_id
)ON UPDATE CASCADE)
因为:custmer_id
对象中的Address
来自&#39; 0&#39;。但那是
Customer
对象。是这样的:
customer Customer (id=90)
active 1
addresses ArrayList<E> (id=95)
elementData Object[10] (id=117)
[0] Address (id=119)
accountId 0
addressType "main office" (id=121)
areaName "area3" (id=122)
city "city3" (id=123)
customerId 0
district "district3" (id=124)
doorNum "89" (id=125)
landmark "landmark3" (id=126)
phone1 "646432365465" (id=127)
phone2 "4534542355675" (id=128)
postalCode "453245" (id=129)
street "street3" (id=130)
modCount 1
size 1
customerId 0
email "bbb@gmail.com" (id=104)
encryptKey "wwwwwfsad" (id=109)
firstName "xxx" (id=110)
lastName "yyy" (id=111)
password "user3" (id=112)
selfDescription "user3" (id=113)
theme Theme (id=114)
userName "user3" (id=116)
答案 0 :(得分:0)
从SQL表中删除自动增量。并以这种方式配置您的模型:
@Id
@Column(name = "primary_key_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "chat_seq_gen")
@SequenceGenerator(name = "chat_seq_gen", sequenceName = "chat_seq")
private Long chatid;
然后创建一个名为chat_seq的序列(对于这个类),应该是它。这是你想要的?如果没有,lemme知道,我删除了我的答案。
修改的
同样在您的Address.java中,您应该像这样引用客户:
@ManyToOne
@JoinColumn(name = "customerid", nullable = false)
private Customer customerAddresses;