Hibernate在jobLifeTime實體的現有數據庫中創建了表,但它給予以8781而不是0開頭的第一個記錄自動生成的代碼。該表具有其他對象(scraperTask)的外鍵,它存儲在DB之前的jobLifeTime實體對象和hibernate給出了一個代碼8780到scraperTask記錄(這一點很清楚,因爲帶有scraperTask的表已經有一些記錄)。Hibernate PostgreSQL與自動生成的增量代碼字段有什麼混淆?
爲什麼hibernate使用來自scraperTask.code的jobLifeTime.code的增量計數?
更新 看起來像Hibernate或PostgreSQL對兩個表主鍵使用一個數字序列。這是正確的行爲?
這裏Java類和截圖,從數據庫表:
@Entity
@Table(name="job_chron_stg", indexes = { @Index(name = "job_life_time_code_hidx", columnList = "code"),
@Index(name = "job_life_time_job_id_hidx", columnList = "job_id")})
public class JobLifeTime {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long code;
@Column(name="job_id", length=24)
private String targetJobCode;
@ManyToOne()
private ScraperTask scraperTask;
@Column(name="salary_low")
private Integer salaryMin;
@Column(name="salary_high")
private Integer salaryMax;
@Column(name="scrape_timestamp")
private Date scrapeTimestamp;
@Column(name="remove_timestamp")
private Date removeTimestamp;
public JobLifeTime(){}
public JobLifeTime(Element node, ScraperTask scraperTask){
targetJobCode = node.attr("data-jk");
this.scraperTask = scraperTask;
salaryMin = scraperTask.getSalaryMin();
salaryMax = scraperTask.getSalaryMax();
scrapeTimestamp = new Date();
}
//getters-setters
}
scraperTask類
@Entity
@Table(indexes = { @Index(name = "scraper_task_code_hidx", columnList = "code"),
@Index(name = "task_start_at_hidx", columnList = "task_start_at")})
public class ScraperTask {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long code;
@Column(name="task_start_at")
private Date taskStartAt;
private Date taskCompleteAt;
private String description;
@Transient
private Integer salaryMin;
@Transient
private Integer salaryMax;
//@Transient
private Integer websiteJobsNumber;
@Transient
private String firstResponse;
//@Transient
//private Integer processedNodes;
//@Transient
private Boolean doneSuccessfully;
@Transient
private List<JobLifeTime> scrapedJobLifeTimeList;
@Transient
private List<Job> scrapedJobList;
@Transient
private KeywordsEntity keywordsEntity;
@Transient
private String category;
protected ScraperTask(){
}
public ScraperTask(String uriString, Integer salaryMin, Integer salaryMax){
description = uriString;
taskStartAt = new Date();
this.salaryMin = salaryMin;
this.salaryMax = salaryMax;
websiteJobsNumber=0;
//processedNodes = 0;
doneSuccessfully = false;
scrapedJobLifeTimeList = new LinkedList<JobLifeTime>();
scrapedJobList = new LinkedList<Job>();
}
//getters-setters
}
使用GenerationType.IDENTITY和postgres –