eclipselink manytoone不保存

时间:2016-09-05 08:46:49

标签: java jpa eclipselink

我有一个关于elcipselink的问题

这是我的模特:

@Entity
@Table(name = "TBL_ASSOC_LANGUE_CODE_BE_GARE")
public class AssocLangueCodeBeGare {

@AttributeOverrides({ @AttributeOverride(name = "id_langue", column = @Column(name = "ID_LANGUE") ),
    @AttributeOverride(name = "id_gare", column = @Column(name = "ID_GARE") ) })
@EmbeddedId
private AssocLangueCodeBeGareFK key;

@Column(name = "CODE_BE", length = 4)
private String codeBe;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, name = "ID_GARE", referencedColumnName = "ID_GARE", insertable = false, updatable = false)
private StopPoint stopPoint;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_LANGUE", referencedColumnName = "ID_LANGUE", insertable = false, updatable = false)
private Langue langue;

在StopPoint我没有参考表AssocLangueCodeBeGare,我不需要它。

然后我这样做:

this.serviceStopPoint.save(currentStopPoint);
            for (AssocLangueCodeBeGare assoc : listAssocLangueCodeBeGare) {
                assoc.setStopPoint(currentStopPoint);
                this.serviceAssocLangueCodeBeGare.save(assoc);
            }

保存

@Override
public void save(T entityToSave) {
    this.getEntityManager().persist(entityToSave);
}

我使用批量插入进行写入,有时当我保存另一个实体时,已完成刷新,我得到:

Caused by: org.h2.jdbc.JdbcBatchUpdateException: NULL not allowed for column "ID_GARE"; SQL statement:
INSERT INTO TBL_ASSOC_LANGUE_CODE_BE_GARE (CODE_BE, ID_GARE, ID_LANGUE) VALUES (?, ?, ?) [23502-192]
at org.h2.jdbc.JdbcPreparedStatement.executeBatch(JdbcPreparedStatement.java:1208)
at org.eclipse.persistence.internal.databaseaccess.DatabasePlatform.executeBatch(DatabasePlatform.java:2134)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeJDK12BatchStatement(DatabaseAccessor.java:871)

我是否错过了映射中的某些内容?

我是否需要在stoppoint实体中添加这样的内容:

@OneToMany(mappedBy = "stopPoint", cascade = CascadeType.ALL)
List<AssocLangueCodeBeGare> assocLangueCodeBeGares;

@ EDIT1

我认为它是由我的EmbeddedId引起的,因为它是空的!!映射没有在可嵌入对象中设置正确的值?

这是可嵌入的对象:

@Embeddable
public class AssocLangueCodeBeGareFK implements Serializable {

private String id_langue;
private Long id_gare;

@Override
public int hashCode() {

非常感谢!

2 个答案:

答案 0 :(得分:0)

最终我找到了解决方案:

我在AssocLangueCodeBeGare上使用 NSString *lastUpdatedTime = [JSONResultDict valueForKey:@"lastUpdatedTime"]; NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm a"; NSDate *yourDate = [dateFormatter dateFromString:lastUpdatedTime]; dateFormatter.dateFormat = @"dd MMM yyyy hh:mm a"; [dateFormatter setAMSymbol:@"am"]; [dateFormatter setPMSymbol:@"pm"]; NSString *newDate = [dateFormatter stringFromDate: yourDate]; NSLog( @"Date : %@", newDate ); Output : Date : 03 Sep 2016 12:01 am

我把@id放在

@IdClass(AssocLangueCodeBeGareFK.class

我将我的属性的AssocLangueCodeBeGareFK类名称重命名为与AssocLangueCodeBeGare类相同:

@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(nullable = false, name = "ID_GARE", referencedColumnName = "ID_GARE")
private StopPoint stopPoint;

@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID_LANGUE", referencedColumnName = "ID_LANGUE")
private Langue langue;

现在所有的工作都像魅力一样。

如果它有帮助......

答案 1 :(得分:0)

insertable = false和updatable = false可防止映射中的值写入数据库。您需要使用值设置到该数据库列的另一个映射,否则它将不会进入数据库。在使您的OneToMany ID工作时,当前代码中的MapsId注释也将如此,因为JPA将在您的可嵌入ID中设置值。或者您可以手动将引用类中的值添加到相应的AssocLangueCodeBeGare.key字段中。