我在我的应用程序中使用spring-data-jpa 1.9.2,mysql-connector 5.1和hibernate 4.3.11 Final。
我在Marketvalue和value之间有一个ManyToOne关联:
@Entity
@Table(name = "MarketValues")
public class MarketValue implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(targetEntity = Value.class)
@JoinColumn(nullable = false, name = "value_isin", referencedColumnName = "isin")
private Value value;
private Date date;
private double cours;
public MarketValue() {
}
}
@Entity
public class Value implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String isin;
private String nature;
@OneToMany(mappedBy = "value")
private Collection<MarketValue> marketvalues;
@ManyToOne(targetEntity = Entreprise.class, fetch = FetchType.EAGER)
@JoinColumn(name = "entreprise_name", referencedColumnName = "name")
private Entreprise entreprise;
public Value() {
super();
}
当我尝试保存具有相同值的两个市值时:
Entreprise e = new Entreprise("ename",3232123123.3,"edesc");
Value v = new Value("vname","visin","vnature", e);
MarketValue mv = new MarketValue(v,22.3,date);
MarketValue mv2 = new MarketValue(v,20.0,date2);
eRepository.save(e);
vRepository.save(v);
mvRepository.save(mv);
mvRepository.save(mv2); //exception here
我在保存第二个市值时得到了这个。
[...]
caused by : com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'visin' for key 'UK_n4350d6kihgn8ls1p4508pwut'
at sun.reflect.GeneratedConstructorAccessor85.newInstance(Unknown Source) ~[na:na]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_91]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_91]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.Util.getInstance(Util.java:387) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:932) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994) ~[mysql-connector-java-5.1.38.jar:5.1.38]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
^解决
我有另一个表引用了列&#34; value_isin&#34; MarketValue
使列#34; value_isin&#34; UNIQUE因此ManyToOne
关联的行为类似于OneToOne
关联。