我尝试通过
SpringMVC和JPA持久保持多个关系.
我的观点如下:
添加WordPair
<form:form modelAttribute="wordpair" action="addWordPair" method="post">
<label for="semanticUnitOne">Enter word one: </label>
<form:input path="semanticUnitOne"/>
<br/>
<label for="semanticUnitTwo">Enter word two: </label>
<form:input path="semanticUnitTwo"/>
<br/>
<div class="form-group">
<label for="dictionary">Dictionary</label>
<form:select path="dictionary" cssClass="selectpicker" items="${dictionaries}" itemLabel="name" itemValue="id">
<%--<form:options items="${dictionaries}" itemValue="id" itemLabel="name"/>--%>
</form:select>
</div>
<input type="submit" class="btn" value="Add word pair"/>
</form:form>
<div class="control-group">
</div>
</div>
控制器如下:
@Controller
public class WordPairController {
@Autowired
WordPairService wordPairService;
@Autowired
DictionaryService dictionaryService;
@RequestMapping(value = "createWordPair", method = RequestMethod.GET)
public String createWordPair(Model model) {
model.addAttribute("wordpair", new WordPair());
model.addAttribute("dictionaries", this.dictionaryService.findAll());
return "addWordPair";
}
@RequestMapping(value = "addWordPair", method = RequestMethod.POST)
public String addWordPair(@ModelAttribute("wordpair") WordPair wordPair, BindingResult result, Dictionary dictionary) {
wordPair.setDictionary(dictionary);
wordPairService.save(wordPair);
return "addWordPair";
}
@RequestMapping(value = "wordPairGet", method = RequestMethod.GET)
public String wordPairGet(Model model) {
model.addAttribute("wordPair", wordPairService.findAllWordPairs());
return "wordPairGet";
}
}
词典的实体如下:
@Entity
public class Dictionary {
@Id
@GeneratedValue
private Long id;
private String name;
private String comment;
@OneToMany(mappedBy = "dictionary", cascade = CascadeType.All, fetch=FetchType.EAGER)
private List<WordPair> wordPairs;
和wordpair实体:
@Entity
@Table
public class WordPair {
@Id
@GeneratedValue
private Long id;
private String semanticUnitOne;
private String semanticUnitTwo;
private int rating;
@ManyToOne(fetch = FetchType.EAGER, cascade = javax.persistence.CascadeType.All)
private Dictionary dictionary;
还有一个服务和DAO,
@Repository("dictionaryDao")
public class DictionaryDaoImpl implements DictionaryDao {
@PersistenceContext
private EntityManager em;
public Dictionary save(Dictionary dictionary) {
em.persist(dictionary);
em.flush();
return dictionary;
}
public List<Dictionary> findAllDictionaries() {
List<Dictionary> result = em.createQuery("SELECT d FROM Dictionary d", Dictionary.class).getResultList();
return result;
}
}
@Repository("wordPairRepository")
public class WordPairDaoImpl implements WordPairDao {
@PersistenceContext
private EntityManager em;
public List<WordPair> findAllWordPairs() {
List<WordPair> result = em.createQuery("SELECT p FROM WordPair p", WordPair.class).getResultList();
return result;
}
public WordPair save(WordPair wordPair) {
em.persist(wordPair);
em.flush();
return wordPair;
}
问题是,当我尝试持久化WordPair并将Dictionary实体链接到它时,hibernate会持久保存新的Dictionary实体,然后通过附加新词典来保留新的WordPair实体.如何附加我用表单选择的现有字典实体:在视图中选择项目?
如果我改变
@ManyToOne(fetch = FetchType.EAGER, cascade = javax.persistence.CascadeType.All)
它提供了WordPair中的选项以合并或其他东西
java.lang.IllegalStateException:
org.hibernate.TransientPropertyValueException: object references an
unsaved transient instance – save the transient instance before
flushing:
最佳答案 我正在使用Spring JPA和Hibernate 5,我让hibernate创建表.
我有实体:
@Entity(name = "DICTIONARY")
public class Dictionary {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String comment;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="DICTIONARY_ID")
private List<WordPair> wordPairs;
和
@Entity(name = "WORDPAIR")
public class WordPair {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String semanticUnitOne;
private String semanticUnitTwo;
private int rating;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Dictionary dictionary;
我有JPA Repostories,注意@Transaction很重要
@Repository
@Transactional
public interface DictionaryRepository extends CrudRepository<Dictionary, Long> {
}
和
@Repository
@Transactional
public interface WordPairRepository extends CrudRepository<WordPair, Long> {
}
创建的表格如下所示:
DICTIONARY
ID
COMMENT
NAME
WORDPAIR
ID
RATING
SEMANTICUNITONE
SEMANTICUNITTWO
DICTIONARY_ID
以下测试表明我可以在两个方向上进行更新.
Dictionary dictionary = makeDictionary("comment", "dictionary 1");
dictionary.setWordPairs(new ArrayList<WordPair>());
dictionary.getWordPairs().add(makeWordPair("George", "Unit 3", "Unit 4", 65));
dictionary.getWordPairs().add(makeWordPair("Greg", "Unit 2", "Unit 1", 25));
dictionaryRepository.save(dictionary);
dictionary = dictionaryRepository.findOne(1l);
assertEquals("dictionary 1", dictionary.getName());
WordPair wordPair = wordPairRepository.findOne(dictionary.getWordPairs().get(0).getId());
assertEquals("dictionary 1", wordPair.getDictionary().getName());
wordPair.getDictionary().setName("dictionary 2");
wordPairRepository.save(wordPair);
wordPair = wordPairRepository.findOne(dictionary.getWordPairs().get(0).getId());
assertEquals("dictionary 2", wordPair.getDictionary().getName());
dictionary = dictionaryRepository.findOne(1l);
assertEquals("dictionary 2", dictionary.getName());