Hibernate 休眠一对多列表,将列表索引作为键的一部分

Hibernate 休眠一对多列表,将列表索引作为键的一部分,hibernate,list,indexing,annotations,one-to-many,Hibernate,List,Indexing,Annotations,One To Many,我有两张桌子: CREATE TABLE product ( id serial NOT NULL, -- some other columns CONSTRAINT product_pkey PRIMARY KEY (id ) ); CREATE TABLE product_image ( product_id bigint NOT NULL, order integer NOT NULL, width integer NOT NULL, -- some othe

我有两张桌子:

CREATE TABLE product
(
  id serial NOT NULL,
  -- some other columns
  CONSTRAINT product_pkey PRIMARY KEY (id )
);

CREATE TABLE product_image
(
  product_id bigint NOT NULL,
  order integer NOT NULL,
  width integer NOT NULL,
  -- some other columns
  CONSTRAINT product_image_pk PRIMARY KEY (product_id , order ),
  CONSTRAINT product_image_product_fk FOREIGN KEY (product_id)
  REFERENCES product (id) 
);
我想这样映射:

public class Product {
  ...
  List<Image> images;
  ...
}

public class Image {
  ...
  int width;
  ...
}
公共类产品{
...
列出图像;
...
}
公众阶级形象{
...
整数宽度;
...
}
基本上,我希望Product类有一个Image对象列表,其中包含Image表中除order和Product id之外的所有字段(如果可能的话)。列表应按照顺序字段进行排序

理想情况下,我根本不想处理订单。我只想hibernate使用产品列表中的顺序。我是否需要image类中的product和order字段

有谁能给我指出正确的方向,我的注释应该是什么样的,或者最好的方法是什么来映射这类东西?我不能在数据库中做任何事情,但我对java模型持开放态度

谢谢

编辑:

我试过这个:

@Entity
@Table(name = "product_image")
public class Image implements Comparable<Image>{

    @Id
    @Column(name = "order")
    private Integer order;

    @Id
    @ManyToOne
    @JoinColumn(name="product_id")
    private Product product;
}

@Entity
@Table(name = "product")
public class Product {

    @OneToMany(mappedBy="product", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Sort(type = SortType.COMPARATOR, comparator = Image.class)
    @OrderColumn(name = "order")
    private List<Image> images;
}
@实体
@表(name=“product\u image”)
公共类映像实现了可比性{
@身份证
@列(name=“order”)
私有整数阶;
@身份证
@许多酮
@JoinColumn(name=“产品标识”)
私人产品;
}
@实体
@表(name=“产品”)
公共类产品{
@OneToMany(mappedBy=“product”,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@排序(type=SortType.COMPARATOR,COMPARATOR=Image.class)
@OrderColumn(name=“order”)
私有列表图像;
}
它可用于读取数据,但此代码失败:

List<Image> images = new ArrayList<Image>();
Image i = new Image();
i.setProduct(product);
images.add(i);      
product.setImages(images);

session.save(product);
List images=new ArrayList();
图像i=新图像();
i、 setProduct(产品);
增加(i);
产品。设置图像(图像);
保存(产品);

因为order仍然为空。

Hibernate支持排序的集合,如
java.util.SortedMap
java.util.SortedSet
。注释@Sort允许您设置用于排序的比较器

首先定义自己的Comparator类进行排序。重写
compare
方法,根据
Image
类中的
order
值对集合进行排序

class ImageComparator<Image> implements Comparator<Image> {

    @Override
    public int compare(Image o1, Image o2) {
        // implement compare method
    }
}
public class Product {
    ...
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "id")
    @Sort(type = SortType.COMPARATOR, comparator = ImageComparator.class)
    SortedSet<Image> images;
    ...
}

现在,您将对
图像
列表进行排序。

您是说我需要图像类中的一个显式订单字段,用于排序?我的理解是,您希望按照图像表中的
订单
字段进行排序。是的,但我并不希望图像知道其在产品图像列表中的索引。这可能吗?在Image类中将
顺序
声明为私有,并且不向
顺序
提供任何设置器。然后,代替一个单独的比较器类,实现
Comparator
并重写Image类本身中的
compare
方法。因此,
order
将无法从Image类中访问。然后,当我向列表中添加对象时,hibernate会根据列表索引设置字段吗?(很抱歉问这个问题,但我现在无法尝试)