gpt4 book ai didi

java - hibernate 将多列映射到 arraylist

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:23:22 24 4
gpt4 key购买 nike

我想知道 hibernate 是否允许使用 arrayList 来表示多列。

例如:我有一个名为“tableA”的表,其中包含以下列:表A:|id|c1|c2|c3|c4|

我想创建一个 java 对象,使用 arrayList 映射到列 c1、c2、c3、c4。

    @Entity
@Table(name = "tableA")
public class tableA{

@Column(name = "id", unique = true, nullable = false)
public int id;

//QUESTION: How do I annotate here to map columns c1,c2,c3,c4 to below arraylist ? ----
public List<String> columns = new ArrayList<>();


//I tried this, but it doesn't work :
@Column(name = "c1", length=10)
public String getC1(){
return columns.get(0);
}

public String setC1(String c1){
columns.add(c1);
}

}

如何在此处注释以将列 c1、c2、c3、c4 映射到数组列表下方?

最佳答案

我似乎没有回答你的问题......但是......有一个功能像你要求的那样:

  • 但注释不支持它 - 只是 xml映射
  • 但这与List<>无关- 但是 Map 非通用

首先,为什么只有 xml?因为它是一个与类无关的动态组件,参见:

In the Component class, there is an attribute called dynamic which allows for support of the dynamic-component property type in xml configuration. Annotations do not currently have support for dynamic-components...

这引出了我们正在谈论的内容:

9.5。动态组件<dynamic-component>

让我们总结一下文档:

You can also map a property of type Map:

<dynamic-component name="userAttributes">
<property name="foo" column="FOO" type="string"/>
<property name="bar" column="BAR" type="integer"/>
<many-to-one name="baz" class="Baz" column="BAZ_ID"/>
</dynamic-component>

The semantics of a <dynamic-component> mapping are identical to <component>. The advantage of this kind of mapping is the ability to determine the actual properties of the bean at deployment time just by editing the mapping document. Runtime manipulation of the mapping document is also possible, using a DOM parser. You can also access, and change, Hibernate's configuration-time metamodel via the Configuration object.

如文档中所述,通过这种方式,我们可以使用一些代表列的键和代表内容的值的字典。

也许,也检查这个(NHibernate 但基本含义是一样的)

最后:我经常使用此功能 - 对于其他字段 - 由业务用户驱动。这些确实只需要一些元数据管理和更新 xml映射到工作。没有其他的。更重要的是,因为这些是列(不是动态行),我们也可以按它们排序...

关于java - hibernate 将多列映射到 arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25676103/

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