简体   繁体   中英

Creating OrderColumn for OneToMany list mapping on a join table in Hibernate

I have a productJob table and a quadrat table. Every quadrat can be installed on many productJob and every productJob can have between 1 and 4 quadrats of the same or different kinds! I'm interested to keep the order of quadrat installation on the product jobs. For example first, second, third or forth place! But the following mapping doesn't create the order column on the JoinTable telai_quadrati . What is the problem of my mapping? The orderColumn isn't created in anyway!

@Entity
@Table(name = "telai")
public class ProductJob implements IProductJob, IProductJobProcessing{
     @Embedded private QuadratGroup quadrateGroup = new QuadratGroup();
}

@Embeddable
public class QuadratGroup implements Serializable{
     @OneToMany(targetEntity = Quadrat.class, cascade = CascadeType.ALL)
     @JoinTable(
        name = "telai_quadrati", 
        joinColumns = {@JoinColumn(name = "dbId", table = "telai")}, 
        inverseJoinColumns = {@JoinColumn(name = "id", table = "quadrati")})
     //@OrderColumn(name = "order")
     @IndexColumn(name = "order")
     public List<Quadrat> getQuadratList(){ //return an ordered list of the quadrats with at most 4 elements}

And it is clear that for the quadrats there exists no order so I use set!

@Entity
@Table(name = "quadrati")
public class Quadrat implements IQuadrat, Cloneable, Serializable{
    @ManyToMany
    @JoinTable(
        name = "telai_quadrati",
        joinColumns = @JoinColumn(name = "id", table = "quadrati"),
        inverseJoinColumns = @JoinColumn(name = "dbId", table = "telai"))
    private Set<ProductJob> productJobs;

It works if I use property access inspite of method access! Like this:

@OneToMany(targetEntity = Quadrat.class, cascade = CascadeType.ALL)
//@MapKeyJoinColumn(name="indice" , table = "telai_quadrati")
@JoinTable(
        name = "telai_quadrati", 
        joinColumns = {@JoinColumn(name = "telaio_id", table = "telai")}, 
        inverseJoinColumns = {@JoinColumn(name = "quadrato_id", table = "quadrati")})
@OrderColumn(name = "indice")
private List<Quadrat> quadratList;

But I wonder why it doesn't work with method access that forces me a heavy refactor in my project! :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM