gpt4 book ai didi

java - 在使用 JPA 在 Spring 中保持多对一关系的同时,它添加了额外的不必要的父实体

转载 作者:行者123 更新时间:2023-11-30 02:50:39 25 4
gpt4 key购买 nike

问题是,当我尝试保留 WordPair 并将 Dictionary 实体链接到它时,hibernate 会保留新的 Dictionary 实体,然后保留新的 WordPair 实体并附加新的字典。如何附加我通过 form:select view item 选择的现有字典实体?如果我改变

@ManyToOne(fetch = FetchType.EAGER, cascade = javax.persistence.CascadeType.All) 

WordPair 中用于合并或其他内容的选项,它给出了

java.lang.IllegalStateException:org.hibernate.TransientPropertyValueException:对象引用未保存的 transient 实例 - 在刷新之前保存 transient 实例:

我的观点如下:

<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 如下:

@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;

和词对实体:

@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;
}

最佳答案

使用@joinColumn作为Wordpair表ID,如下WordPair类中的wordpairId

@Entity
@Table
public class WordPair {

@Id
@GeneratedValue
private Long id;

private String semanticUnitOne;
private String semanticUnitTwo;
private int rating;

@ManyToOne(fetch = FetchType.LAZY, cascade =javax.persistence.CascadeType.All)
@JoinColumn(name = "WordPairId")
private Dictionary dictionary;

参见下面的字典类 targetEntity 作为 WordPair.class

@Entity
public class Dictionary {

@Id
@GeneratedValue
private Long id;

private String name;
private String comment;

@OneToMany(mappedBy = "dictionary", cascade = CascadeType.All,fetch=FetchType.LAZY, targetEntity = WordPair.class)
private List<WordPair> wordPairs;

关于java - 在使用 JPA 在 Spring 中保持多对一关系的同时,它添加了额外的不必要的父实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38762524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com