gpt4 book ai didi

java - jpa 多对多与附加列

转载 作者:太空宇宙 更新时间:2023-11-04 14:20:28 24 4
gpt4 key购买 nike

我与两个实体之间的附加列建立了多对多关系。在所有者端实体上,我已将级联类型设置为持久。

    @OneToMany(cascade=CascadeType.PERSIST, fetch = FetchType.LAZY, mappedBy = "definitionType")
private List<DefinitionProperty> definitionProperties = new ArrayList<DefinitionProperty>();

这是我代表表的新实体:

@EmbeddedId
protected DefinitionPropertyPK pk;

@JoinColumn(name = "dtid", referencedColumnName = "id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private DefinitionType definitionType;
@JoinColumn(name = "prid", referencedColumnName = "id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Property property;
@Column(name = "initial_value", insertable = false, updatable = false)
@Basic(optional = false)
private String initialValue;

问题是,当我保留所有者对象时,它将被插入到数据库中,但在连接表中不会创建任何内容,并且出现“无法将 NULL 值插入列“initial_value””异常。我不知道为什么它是空的,因为我已经在服务代码中设置了它。

Call: INSERT INTO definition_property (initial_value, dtid, prid) VALUES (?, ?, ?)
bind => [3 parameters bound]

这是我的服务代码以及如何创建新对象并设置它们的值。

DefinitionType definitionType = new DefinitionType();
String propertyIds[] = idProperty.split(",");
String propertyVals[] = propertyValues.split(",");
for (int i = 0; i < propertyIds.length; i++) {
Property property = propertyDao.find(Integer.parseInt(propertyIds[i]));
DefinitionProperty dp = new DefinitionProperty();

if (propertyVals[i] != "" || propertyVals[i]!=null) {
dp.setInitialValue(propertyVals[i]);
} else {
dp.setInitialValue(property.getInitialValue());
}
dp.setDefinitionType(definitionType);
dp.setProperty(property);
definitionType.getDefinitionProperties().add(dp);
}
definitionTypeDao.persist(definitionType);

您可以找到所有代码here

最佳答案

您确定这个说法吗

dp.setInitialValue(propertyVals[i]);

propertyVals[i] is not null

'optional' talks about property and field values and suggests that this feature should be evaluated within the runtime.

关于java - jpa 多对多与附加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27248673/

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