I am trying to save a lot to one relation through SpringMVC and JPA.
My opinion is as follows:
Add 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>
The controller is as follows:
@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";
}
}
Entitiy for dictionary:
@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;
and wordpair object:
@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;
There is also a service and 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;
}
The problem is that I am trying to save an object WordPairand associate an object Dictionarywith it, hibernate saves a new Dictionary object and then saves a new WordPair object with a new dictionary attached to it. How to connect an existing dictionary that I selected using the form: select an item in the field of view? If i change
@ManyToOne(fetch = FetchType.EAGER, cascade = javax.persistence.CascadeType.All)
in WordPair for Merge or something, it gives
java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: - :