Hibernate,@ ManyToOne无需更新

时间:2014-12-20 00:44:42

标签: spring hibernate

Crypto.java

@Entity
@Table
public class Crypto extends SessionDAO {

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "my_entity_seq_gen")
@SequenceGenerator(name = "my_entity_seq_gen", sequenceName = "catalog_seq")
private int id;

private String currency1;
private String currency2;
private double value;

@Enumerated(EnumType.STRING)
private MainCurrency mainCurrency;

@Enumerated(EnumType.STRING)
private Exchanger exchanger;
getters, setters, etc....

Values.java

@Entity
@Table
public class Values extends SessionDAO {

@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
@Column(unique = true, nullable = false)
private int valuesId;
private double amount;
private Date date;

@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "cryptoId")
private Crypto crypto;

getters, setters, etc....

main.jsp中

<form:form method="post" commandName="valuesToCreate" action="?action=addValues">
    <tr>
    <td></td>
    <td><form:input path="amount" /></td>
    <td><fmt:formatDate value="<%=new java.util.Date()%>" var="dateString" pattern="yyyy/MM/dd" /><form:input path="date" value="${dateString}"/></td>
    <td><form:select path="crypto.id"><form:options items="${listCrypto}" itemValue="id" itemLabel="id"/></form:select></td>
    <td></td>
    <td><input type="submit" value="Add"></td>
    </tr>
    </form:form>

控制器

...
@ModelAttribute Values valuesToCreate
...
session.save(valuesToCreate);
...

一切正常,但在我添加&#34;值&#34;桌上的条目&#34;加密&#34;自动更新并设置为空的所有参数,不带id:

Hibernate: insert into "Values" ("amount", cryptoId, "date", "valuesId") values (?, ?, ?, ?)
Hibernate: update "Crypto" set "currency1"=?, "currency2"=?, "exchanger"=?, "mainCurrency"=?, "value"=? where "id"=?

我能为&#34;更新&#34;做些什么?不会打电话?

1 个答案:

答案 0 :(得分:0)

删除 cascade = CascadeType.ALL 后一切正确!