gpt4 book ai didi

java - @IndexColumn 的使用导致 seq_num 为 0

转载 作者:行者123 更新时间:2023-11-30 07:36:00 30 4
gpt4 key购买 nike

我想使用@IndexColumn 来设置用户输入的一些数据的序列号。我正在使用 Spring 2.5.6、JBoss 5.1 (JPA 1.0)。

对于我的父类

@Entity
@Table(name="material")
public class Material implements Serializable {
.
.
/**
* List of material attributes associated with the given material
*/
@OneToMany(mappedBy = "material", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@IndexColumn(name="seq_number", base=0, nullable = false)
private List<MaterialAttribute> materialAttributes;

public void addMaterialAttribute(List<MaterialAttribute> attribs)
{
if(CollectionUtils.isNotEmpty(attribs))
{
for(MaterialAttribute attrib : attribs)
{
attrib.setMaterial(this);
}

this.setMaterialAttributes(attribs);
}
}

}

我的 child 类(class)

@Entity
@Table(name="material_attribute")
public class MaterialAttribute implements Serializable
{

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "material_id", referencedColumnName = "id", updatable=false, nullable = true, unique = false)
private Material material;

@Column(name = "seq_number", insertable=false, updatable=false, nullable = false)
private int seqNumber;
}

对于服务类

public void save(MaterialCommand pCmd)
{
Material material = new Material(pCmd.getName());

//convert from command object to entity object
List<MaterialAttribute> attribs = new ArrayList<MaterialAttribute>();

if(CollectionUtils.isNotEmpty(pCmd.getAttribs()))
{
Iterator<MaterialAttributeCommand> iter = pCmd.getAttribs().iterator();
while(iter.hasNext())
{
MaterialAttributeCommand attribCmd = (MaterialAttributeCommand) iter.next();

MaterialAttribute attrib = new MaterialAttribute();
attrib.setDisplayName(attribCmd.getDisplayName());
attrib.setValidationType(attribCmd.getValidationType());

attribs.add(attrib);
}
}

material.addMaterialAttribute(attribs);

this.getMaterialDAO().saveMaterial(material);
}

我正在获取数据库中的条目,但对于集合中的每个项目,seq_number 始终为零。

我不得不假设这是我保存数据的方式,但我只是没有看到它。


我已经能够通过执行以下操作解决问题(删除了 mappedBy):

@Entity
@Table(name="material")
public class Material implements Serializable {

/**
*
*/
private static final long serialVersionUID = 5083931681636496023L;

@Column(name="name", length=50, nullable=false)
private String mName;

/**
* List of material attributes associated with the given material
*/
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@IndexColumn(name="seq_number", base=0)
@JoinColumn(name="material_id",nullable=false)
private List<MaterialAttribute> materialAttributes;



@Entity
@Table(name="material_attribute")
public class MaterialAttribute implements Serializable
{

/**
*
*/
private static final long serialVersionUID = -196083650806575093L;

/**
* identifies the material that these attributes are associated with
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "material_id", insertable=false, updatable=false, nullable = true, unique = false)
private Material material;

@Column(name = "seq_number", insertable=false, updatable=false)
private int seqNumber;

最佳答案

使用 Hibernate 映射双向索引列表有点棘手,但在 2.4.6.2.1. Bidirectional association with indexed collections 部分中有所介绍文档(粗体是我的):

2.4.6.2.1. Bidirectional association with indexed collections

A bidirectional association where one end is an indexed collection (ie. represented as a @OrderColumn, or as a Map) requires special consideration. If a property on the associated class explicitly maps the indexed value, the use of mappedBy is permitted:

@Entity
public class Parent {
@OneToMany(mappedBy="parent")
@OrderColumn(name="order")
private List<Child> children;
...
}

@Entity
public class Child {
...
//the index column is mapped as a property in the associated entity
@Column(name="order")
private int order;

@ManyToOne
@JoinColumn(name="parent_id", nullable=false)
private Parent parent;
...
}

But, if there is no such property on the child class, we can't think of the association as truly bidirectional (there is information available at one end of the association that is not available at the other end: the index). In this case, we can't map the collection as mappedBy. Instead, we could use the following mapping:

@Entity
public class Parent {
@OneToMany
@OrderColumn(name="order")
@JoinColumn(name="parent_id", nullable=false)
private List<Child> children;
...
}

@Entity
public class Child {
...
@ManyToOne
@JoinColumn(name="parent_id", insertable=false, updatable=false, nullable=false)
private Parent parent;
...
}

Note that in this mapping, the collection-valued end of the association is responsible for updating the foreign key.

其实第二个映射就是如何以一对多的一方为拥有方映射一个双向的一对多。虽然这是可能的,但您需要注意这种映射将在优化 SQL 下生成,如关于 2.2.5.3.1.1. Bidirectional 的部分所述。 [一对多]关系:

To map a bidirectional one to many, with the one-to-many side as the owning side, you have to remove the mappedBy element and set the many to one @JoinColumn as insertable and updatable to false. This solution is not optimized and will produce some additional UPDATE statements.

总而言之,如果将索引列映射为目标实体的属性不是问题,这将是我的建议(即第一个映射)。

引用资料

关于java - @IndexColumn 的使用导致 seq_num 为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4054570/

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