我有两个实体:category
oneToMany
products
。
那些是课程:
Product.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "NAME")
private Long NAME;
@ManyToOne
@JoinColumn(name="CATEGORY_ID", nullable = false)
private Category category;
//...getters, setters and toString
Category.java:
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "NAME")
private Long NAME;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "category", orphanRemoval = true)
private List<Product> products;
//...getters, setters and toString
在CategoryBean
我有与Category
实体相同的字段。在ProductBean
中,我与实体期望category
字段相同(在映射时避免无限循环)。
从前面(我使用角度),我发送与实体具有相同fileds的类别对象。换句话说,我发送到后面以保存它的类别对象包含:name
和array
products
。
要保存类别,这就是我正在做的事情:
//from bean to entity
Category category = CategoryMapper.toEntity(categoryBean);
// NOTE: the category is containing the list of its products
Category result = categoryRepository.save(category);
return CategoryMapper.toBean(result);
但是我得到了500 status
,这就是错误:
00:04:08.940 [http-nio-8080-exec-2] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 1747, SQLState: 42000
00:04:08.940 [http-nio-8080-exec-2] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ORA-01747: invalid user.table.column, table.column, or column specification
00:04:09.276 [http-nio-8080-exec-2] WARN ma.neoxia.exceptions.RestResponseEntityExceptionHandler - INTERNAL_SERVER_ERROR error
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
at
请注意,我使用的是oracle
和flyway
。这是PRODUCT SQL
脚本:
CREATE TABLE "PRODUCT"
( "ID" NUMBER(10,0),
"NAME" VARCHAR2(255 CHAR),
"CATEGORY_ID" NUMBER(10,0)
);
CREATE UNIQUE INDEX "PRODUCT_ID_IDX" ON "PRODUCT" ("ID");
ALTER TABLE "PRODUCT" MODIFY ("ID" NOT NULL ENABLE);
ALTER TABLE "PRODUCT" ADD PRIMARY KEY ("ID");
ALTER TABLE "PRODUCT" MODIFY ("CATEGORY_ID" NOT NULL ENABLE);
这是我的分类SQL
脚本:
CREATE TABLE "CATEGORY"
( "ID" NUMBER(19,0),
"NAME" VARCHAR2(255 CHAR)
);
CREATE UNIQUE INDEX "CAT_ID_IDX" ON "CATEGORY" ("ID");
ALTER TABLE "CATEGORY" MODIFY ("ID" NOT NULL ENABLE);
ALTER TABLE "CATEGORY" ADD PRIMARY KEY ("ID");
答案 0 :(得分:0)
在您的SQL文件中,您错过了在CATEGORY_ID
列上添加外键约束,其长度必须与ID
表中的CATEGORY
列相同
你可以这样做:
Alter TABLE PRODUCT
MODIFY "CATEGORY_ID" NUMBER(19,0);
ALTER TABLE PRODUCT
ADD CONSTRAINT FK_CategoryProduct
FOREIGN KEY (CATEGORY_ID) REFERENCES CATEGORY(ID);