The following code shows how to mark the order column in a one to many mapping.
@OneToMany(mappedBy="queue") @OrderColumn(name="PRINT_ORDER") private List<PrintJob> jobs;
The following code is from PrintJob.java.
package com.java2s.common; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class PrintJob { @Id private int id; @ManyToOne private PrintQueue queue; public int getId() { return id; } public void setId(int id) { this.id = id; } public PrintQueue getQueue() { return queue; } public void setQueue(PrintQueue queue) { this.queue = queue; } }
The following code is from PrintQueue.java.
package com.java2s.common; import java.util.List; import java.util.ArrayList; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.OrderColumn; @Entity public class PrintQueue { @Id private String name; @OneToMany(mappedBy="queue") @OrderColumn(name="PRINT_ORDER") private List<PrintJob> jobs; public PrintQueue() { jobs = new ArrayList<PrintJob>(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<PrintJob> getJobs() { return jobs; } public void addJob(PrintJob job) { this.jobs.add(job); job.setQueue(this); } public String toString() { return "PrintQueue: " + name; } }
The following code is from PersonDaoImpl.java.
package com.java2s.common; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { PrintQueue q = new PrintQueue(); q.setName("q1"); PrintJob j = new PrintJob(); j.setId(1); j.setQueue(q); q.getJobs().add(j); em.persist(j); em.persist(q); } @PersistenceContext private EntityManager em; }
The following is the database dump.
Table Name: PRINTJOB Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: QUEUE_NAME, Column Type: VARCHAR: Column Value: q1 Column Name: PRINT_ORDER, Column Type: INTEGER: Column Value: 0 Table Name: PRINTQUEUE Row: Column Name: NAME, Column Type: VARCHAR: Column Value: q1