1、由于使用了JPA,默认使用的是hibernate,在启动时会删除所有的表并重新的建立表结构,所以data.sql中的语句并没有执行。
解决办法很简单,在application.properties文件中加入下面的配置:
spring.jpa.hibernate.ddl-auto=none
2、java.lang.NumberFormatException: For input string: “WRAP”
为Ingredient.java的Type字段加上@Enumerated(EnumType.STRING)注释
3、新的ingredient的string和实例转化
package tacos.web;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import tacos.Ingredient;
import tacos.data.IngredientRepository;
@Component
public class IngredientByIdConverter implements Converter<String, Ingredient> {
private IngredientRepository ingredientRepo;
@Autowired
public IngredientByIdConverter(IngredientRepository ingredientRepo) {
this.ingredientRepo = ingredientRepo;
}
@Override
public Ingredient convert(String id) {
Optional<Ingredient> optionalIngredient = ingredientRepo.findById(id);
return optionalIngredient.isPresent() ? optionalIngredient.get() : null;
}
}
4、org.h2.jdbc.JdbcSQLException: Sequence “HIBERNATE_SEQUENCE” not found
schema.sql加上一条语句
CREATE SEQUENCE HIBERNATE_SEQUENCE START WITH 1 INCREMENT BY 1;
5、org.h2.jdbc.JdbcSQLException: Column “CREATED_AT” not found;或者"XXXX_XX" not found
在3.1章的表字段的命名是java风格的,在执行hibernate的查询语句时会报错。
由于createdAt属性未加上@Column注解,那么你会认为它对应的字段会是createdAt,实际上再查询时,你会发现他映射的字段是created_at,即使你给他加上@Column(name=“createdAt”)也不起作用,但是你给他加上@Column(name=“createdat”)确实可以的。可能是spring强制你的字段必须符合sql的字段命名标准,会自动将java的驼峰命名的熟悉名转换成用下划线分隔的形式。
如果逐步的去按照@Column(name=“createdat”)这样的方式去处理实在太麻烦了,所以直接修改了表结构的ddl语句,如下:
drop table Taco_Ingredients if exists;
drop table Taco_Order_Tacos if exists;
create table if not exists Ingredient (
id varchar(4) not null,
name varchar(25) not null,
type varchar(10) not null
);
create table if not exists Taco (
id identity,
name varchar(50) not null,
created_at timestamp not null
);
create table if not exists Taco_Ingredients (
taco_id bigint not null,
ingredients_id varchar(4) not null
);
alter table Taco_Ingredients
add foreign key (taco_id) references Taco(id);
alter table Taco_Ingredients
add foreign key (ingredients_id) references Ingredient(id);
create table if not exists Taco_Order (
id identity,
name varchar(50) not null,
street varchar(50) not null,
city varchar(50) not null,
state varchar(2) not null,
zip varchar(10) not null,
cc_number varchar(16) not null,
cc_expiration varchar(5) not null,
ccCVV varchar(3) not null,
placed_at timestamp not null
);
create table if not exists Taco_Order_Tacos (
order_id bigint not null,
taco_id bigint not null
);
alter table Taco_Order_Tacos
add foreign key (order_id) references Taco_Order(id);
alter table Taco_Order_Tacos
add foreign key (taco_id) references Taco(id);
CREATE SEQUENCE HIBERNATE_SEQUENCE START WITH 1 INCREMENT BY 1;
这里删除了 Taco_Order表的所有delivery前缀