使用Vaadin的Spring Boot - 使用外键

问题描述 投票:0回答:2

我是Spring Boot和Vaadin的新手。我按照教程为电话簿应用程序创建了CRUD页面但是我在使用外键时遇到了问题。我有一个Contact表,它有电话类型(即cell或home)作为外键 - 即它引用了我的PhoneType表。我被困在如何从我的PhoneType表中填充的值的下拉列表中填充手机类型。现在我在我的Contact类中有以下成员变量

@ManyToOne
@JoinColumn(name="type")
private PhoneType phoneType;

在我的PhoneType课程中,我有

@Column(name = "type")
private String phoneType;

但是我收到一条错误,上面写着“通过JDBC语句执行DDL时出错”。

该应用程序的其余部分适用于CRUD页面。

spring vaadin
2个回答
0
投票

首先,在mySQL实现中,除非使用8.0+ JSON数据类型,否则无法存储实际对象。 SQL不知道PhoneType是什么,因为它是一个对象而不是一个有效的数据类型。 https://www.w3schools.com/sql/sql_datatypes.asp

如果你想存储实际的对象,你需要找到你喜欢的noSQL实现。

因此,您的“客户”类不会正确映射到表。您需要创建实例变量,例如

String hasCellPhone, hasHomePhone; //etc for the options in your dropdown menu

而不是试图把一个phonetype对象。

我问了几乎完全相同的问题,我建议你阅读整个帖子。 https://stackoverflow.com/a/50879597/5468597

create table item (
    barcode bigint not null auto_increment primary key,
    name varchar(20) not null,
    type varchar(20) not null,
    is_available boolean not null,
    is_late boolean null,
    notes varchar(255) null,
    check_out_date datetime null,
    due_date datetime null
    #create index idx_barcode (barcode));

create table patron (
    trinity_id bigint not null primary key,
    name varchar(30) not null,
    email varchar(20) not null,
    owes_fines boolean not null,
    fines_owed int null
    #create index idx_trinity_id (trinity_id));

create table checked_out_items (
    ref_id bigint primary key auto_increment not null,
    patron_id bigint not null,
    item_id bigint not null,
    item_available boolean not null,
    item_check_out_date datetime null,
    item_due_date datetime null);

alter table checked_out_items
    add constraint fk_patron_id
    foreign key (patron_id) references patron(trinity_id),
    add constraint fk_item_id
    foreign key (item_id) references item(barcode)
    #add constraint fk_item_available
    #add constraint fk_check_out_date
    #add constraint fk_due_date
    #foreign key (item_available references item(is_available)
    #foreign key (item_check_out_date) references item(check_out_date)
    #foreign key (item_due_date) references item(due_date)
    on update cascade
    on delete cascade;     


insert into patron values(0000000,'Test Erino','[email protected]',0,null);
insert into item values(1,'Chromebook','Laptop',0,null,null,null,null);

insert into  checked_out_items(patron_id,item_id,item_available,item_check_out_date,item_due_date)
select patron.trinity_id,item.barcode,item.is_available,item.check_out_date,item.due_date
from patron
inner join item;

最后:

select * from item;
select * from patron;
select * from checked_out_items;

Schema Setup

我不会在这里发布java逻辑。这是你在另一个线程中阅读。


0
投票

我解决了我的问题。

@ManyToOne (cascade = {CascadeType.ALL})
@JoinColumn(name="phoneType_typeId")
private PhoneType phoneType;

@Id
@GeneratedValue
@Column(name = "typeId")
private Long id;
© www.soinside.com 2019 - 2024. All rights reserved.