我有2個實體有一對多的關係。由於外鍵爲空,相關子條目無法插入。 雖然在測試中工作。春季數據百里香 - 無法插入相關記錄,外鍵空
看上去很高,大部分都被解決了,因爲提問者並沒有把孩子添加到父母身上,這不是我的情況。任何幫助感謝!
錯誤
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("AIWA"."CAT_DT"."CAT_ID")
編輯1:添加詳細的錯誤日誌如下。
2017-03-01 09:45:04 DEBUG JpaTransactionManager:759 - Initiating transaction commit 2017-03-01 09:45:04 DEBUG JpaTransactionManager:512 - Committing JPA transaction on EntityManager [[email protected]] 2017-03-01 09:45:04 DEBUG SQL:109 - insert into CAT (CATTYPE, ..., ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?) 2017-03-01 09:45:04 TRACE BasicBinder:81 - binding parameter [1] as [NUMERIC] - [200] 2017-03-01 09:45:04 TRACE BasicBinder:69 - binding parameter [2] as [VARCHAR] - [null] 2017-03-01 09:45:04 TRACE BasicBinder:81 - binding parameter [3] as [VARCHAR] - [1] 2017-03-01 09:45:04 TRACE BasicBinder:69 - binding parameter [4] as [VARCHAR] - [null] 2017-03-01 09:45:04 TRACE BasicBinder:69 - binding parameter [5] as [VARCHAR] - [null] 2017-03-01 09:45:04 TRACE BasicBinder:69 - binding parameter [6] as [VARCHAR] - [null] 2017-03-01 09:45:04 TRACE BasicBinder:81 - binding parameter [7] as [NUMERIC] - [1] 2017-03-01 09:45:04 TRACE BasicBinder:69 - binding parameter [8] as [VARCHAR] - [null] 2017-03-01 09:45:04 TRACE BasicBinder:81 - binding parameter [9] as [NUMERIC] - [182] 2017-03-01 09:45:04 DEBUG SQL:109 - insert into CAT_DT (CAT_ID, DESCR, LANGUAGE, NAME, SORTNAME, ID) values (?, ?, ?, ?, ?, ?) 2017-03-01 09:45:04 TRACE BasicBinder:69 - binding parameter [1] as [NUMERIC] - [null] 2017-03-01 09:45:04 TRACE BasicBinder:81 - binding parameter [2] as [VARCHAR] - [a] 2017-03-01 09:45:04 TRACE BasicBinder:81 - binding parameter [3] as [NUMERIC] - [1] 2017-03-01 09:45:04 TRACE BasicBinder:81 - binding parameter [4] as [VARCHAR] - [a] 2017-03-01 09:45:04 TRACE BasicBinder:69 - binding parameter [5] as [VARCHAR] - [null] 2017-03-01 09:45:04 TRACE BasicBinder:81 - binding parameter [6] as [NUMERIC] - [264] 2017-03-01 09:45:04 WARN SqlExceptionHelper:144 - SQL Error: 1400, SQLState: 23000 2017-03-01 09:45:04 ERROR SqlExceptionHelper:146 - ORA-01400: cannot insert NULL into ("CAT_DT"."CAT_ID")
2017-03-01 09:45:04 INFO AbstractBatchImpl:208 - HHH000010: On release of batch it still contained JDBC statements 2017-03-01 09:45:04 DEBUG JpaTransactionManager:898 - Initiating transaction rollback after commit exception
實體
public class Cat{
private List<CatDt> catDts = new ArrayList<CatDt>();
@OneToMany(fetch = FetchType.EAGER, mappedBy = "cat", cascade=CascadeType.ALL)
public List<CatDt> getCatDts() {
if(this.catDts==null){
this.catDts = new ArrayList<CatDt>();
}
return this.catDts;
}
public void setCatDts(List<CatDt> catDts) {
this.catDts = catDts;
}
}
public class CatDt{
private Cat cat;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
@JoinColumn(name = "CAT_ID", nullable = false)
public Cat getCat() {
return this.cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
}
Thymeleaf形式
<ul th:each="item, stat: *{catDts}">
<li>
<span th:text="#{front.cat.CatDt.no.label}">No</span>
<label th:text="${stat.count}">1</label>
</li>
<li>
<span th:text="#{front.cat.CatDt.name.label}">Name</span>
<input type="text" th:field="*{catDts[__${stat.index}__].name}" th:id="${'name-'+stat.index}">
</li>
<li>
<span th:text="#{front.cat.CatDt.descr.label}">Description</span>
<input type="text" th:field="*{catDts[__${stat.index}__].descr}" th:id="${'descr-'+stat.index}">
</li>
<li>
<span th:text="#{front.cat.CatDt.lang.label}">Language</span>
<input type="text" th:field="*{catDts[__${stat.index}__].language}" th:id="${'language-'+stat.index}">
</li>
</ul>
產生的HTML表格
<form action="/aiwaweb/cat/create" method="post">
<!-- Main info -->
<ul>
<li>
<span>Code</span>
<input id="code" name="code" value="" type="text">
</li>
<li>
<span>Type</span>
<select id="catType" name="catType">
<option...>...</option>
</select>
</li>
<li>
<span>Status</span>
<select id="status" name="status">
<option value="0">INACTIVE</option>
<option value="1">ACTIVE</option>
</select>
</li>
</ul>
<!-- Details -->
<ul>
<li>
<span>No.</span>
<label>1</label>
</li>
<li>
<span>Name</span>
<input id="name-0" name="catDts[0].name" value="" type="text">
</li>
<li>
<span>Description</span>
<input id="descr-0" name="catDts[0].descr" value="" type="text">
</li>
<li>
<span>Language</span>
<input id="language-0" name="catDts[0].language" value="" type="text">
</li>
</ul>
</form>
編輯2更新後的控制器create()將Cat添加到CatDt(早些時候它只是將CatDt添加到Cat);希望這種2種方式的關係可以解決,但沒有。
控制器
@GetMapping({"/create"})
public String create(ModelMap model){
Cat cat = new Cat();
List<CatDt> catDts = new ArrayList<CatDt>();
CatDt catDt = new CatDt();
catDt.setCat(cat); //this is the extra line added
catDts.add(catDt);
cat.setCatDts(catDts);
model.addAttribute("cat", cat);
return "cat/create";
}
@PostMapping(value="/create", params={"save"})
public String save(final Cat cat
, final BindingResult bindingResult, final ModelMap model){
if(bindingResult.hasErrors()){
return "cat/create";
}
Cat updatedCat = catService.save(cat);
model.clear();
return "redirect:/cat/create";
}
我做了什麼至今
- 當調試我控制器保存()上,貓有孩子CatDt在列表中,但貓財產CatDt的值爲null;但是想一想,也許它應該是空的,因爲貓還沒有保存,所以沒有身份證。
單元測試
@Test
public void testCatSave(){
Cat cat = new Cat();
cat.setCode("11111");
...
//add cat details
List<CatDt> catDts = new ArrayList<CatDt>();
CatDt catDt = new CatDt();
catDt.setCat(cat);
catDt.setName("Test");
...
catDts.add(catDt);
cat.setCatDts(catDts);
Cat catUpdated = catService.save(cat);
}
持久性配置
public class PersistenceConfig {
@Autowired
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
{
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(dataSource());
factory.setJpaVendorAdapter(vendorAdapter);
factory.setJpaProperties(hibernateProperties());
factory.setPackagesToScan("ae.tbits.atn.aiwacore.common.model");
return factory;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf)
{
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(emf);
return txManager;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator(){
return new HibernateExceptionTranslator();
}
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.username"));
dataSource.setPassword(env.getProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
env.getRequiredProperty("hibernate.type"));
return properties;
}
}
在此之後,以一個T,但不保存子記錄http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#the-controller
添加編輯1顯示更多的日誌。可以看到父母和孩子的2組插入語句。不知何故_child沒有獲得新插入的父id_。我是否需要手動執行任何操作,例如_commit_或_flush_?如果是的話,因爲我嘗試了,我的mvc代碼拋出錯誤(_no transaction in progress_)。我的服務只是調用repo的Cat.save();測試運行良好。還是需要修改實體或配置? –