问题是当我尝试保留WordPair并将其链接到词典实体时,hibernate保留新的Dictionary实体,然后保留新的WordPair实体并附加新的词典。如何附加现有的我用form:select项目选择的字典实体?如果我改变

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


WordPair中的“合并”选项或其他功能

java.lang.IllegalStateException:org.hibernate.TransientPropertyValueException:对象引用了一个未保存的临时实例-在刷新之前保存该临时实例:

我的看法如下:

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


和单词对实体:

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

10-06 11:01