2014-11-05 34 views
2

我有一個Java Server Faces項目使用Hibernate將數據存儲到PostgreSQL數據庫。休眠不使用PostgreSQL序列來生成主鍵

我遇到的問題是,Hibernate不考慮現有序列(fifo_id_seq),同時將數據保存到數據庫中;它保存着id = 0;

答案可能非常明顯,因爲我是Hibernate的新手。

對於大量的代碼,我很抱歉,但我想發佈一切可能相關的內容。

FIFO表:

        Table "public.fifo" 
     Column  |  Type  |      Modifiers      
---------------------+------------------+--------------------------------------------------- 
id     | integer   | not null default nextval('fifo_id_seq'::regclass) 
number_of_processes | integer   | 
total_time   | integer   | 
average_time  | double precision | 
average_wait_time | double precision | 
total_wait_time  | integer   | 
Indexes: 
"fifo_pkey" PRIMARY KEY, btree (id) 

的fifo_id_seq序列:

  Sequence "public.fifo_id_seq" 
    Column  | Type |  Value   
---------------+---------+--------------------- 
sequence_name | name | fifo_id_seq 
last_value | bigint | 1 
start_value | bigint | 1 
increment_by | bigint | 1 
max_value  | bigint | 9223372036854775807 
min_value  | bigint | 1 
cache_value | bigint | 1 
log_cnt  | bigint | 0 
is_cycled  | boolean | f 
is_called  | boolean | f 

FifoEntity.java: *注:構造函數和由我添加@Id註釋,產生

休息
@Entity 
@Table(name = "fifo", schema = "public", catalog = "processmanagement") 
public class FifoEntity { 
private int id; 
private Integer numberOfProcesses; 
private Integer totalTime; 
private Double averageTime; 
private Double averageWaitTime; 
private Integer totalWaitTime; 

public FifoEntity() 
{ 
    // empty 
} 

public FifoEntity(int numberOfProcesses, int totalTime, double averageTime, double averageWaitTime, int totalWaitTime) 
{ 
    this.numberOfProcesses = numberOfProcesses; 
    this.totalTime = totalTime; 
    this.averageTime = averageTime; 
    this.averageWaitTime = averageWaitTime; 
    this.totalWaitTime = totalWaitTime; 
} 

@Id 
@SequenceGenerator(name="fifo_pkey", sequenceName="fifo_id_seq", allocationSize = 1) 
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="fifo_pkey") 
@Column(name = "id", unique = true, nullable = false) 
public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

@Basic 
@Column(name = "number_of_processes") 
public Integer getNumberOfProcesses() { 
    return numberOfProcesses; 
} 

public void setNumberOfProcesses(Integer numberOfProcesses) { 
    this.numberOfProcesses = numberOfProcesses; 
} 

@Basic 
@Column(name = "total_time") 
public Integer getTotalTime() { 
    return totalTime; 
} 

public void setTotalTime(Integer totalTime) { 
    this.totalTime = totalTime; 
} 

@Basic 
@Column(name = "average_time") 
public Double getAverageTime() { 
    return averageTime; 
} 

public void setAverageTime(Double averageTime) { 
    this.averageTime = averageTime; 
} 

@Basic 
@Column(name = "average_wait_time") 
public Double getAverageWaitTime() { 
    return averageWaitTime; 
} 

public void setAverageWaitTime(Double averageWaitTime) { 
    this.averageWaitTime = averageWaitTime; 
} 

@Basic 
@Column(name = "total_wait_time") 
public Integer getTotalWaitTime() { 
    return totalWaitTime; 
} 

public void setTotalWaitTime(Integer totalWaitTime) { 
    this.totalWaitTime = totalWaitTime; 
} 

@Override 
public boolean equals(Object o) { 
    if (this == o) return true; 
    if (o == null || getClass() != o.getClass()) return false; 

    FifoEntity that = (FifoEntity) o; 

    if (id != that.id) return false; 
    if (averageTime != null ? !averageTime.equals(that.averageTime) : that.averageTime != null) return false; 
    if (averageWaitTime != null ? !averageWaitTime.equals(that.averageWaitTime) : that.averageWaitTime != null) 
     return false; 
    if (numberOfProcesses != null ? !numberOfProcesses.equals(that.numberOfProcesses) : that.numberOfProcesses != null) 
     return false; 
    if (totalTime != null ? !totalTime.equals(that.totalTime) : that.totalTime != null) return false; 
    if (totalWaitTime != null ? !totalWaitTime.equals(that.totalWaitTime) : that.totalWaitTime != null) 
     return false; 

    return true; 
} 

@Override 
public int hashCode() { 
    int result = id; 
    result = 31 * result + (numberOfProcesses != null ? numberOfProcesses.hashCode() : 0); 
    result = 31 * result + (totalTime != null ? totalTime.hashCode() : 0); 
    result = 31 * result + (averageTime != null ? averageTime.hashCode() : 0); 
    result = 31 * result + (averageWaitTime != null ? averageWaitTime.hashCode() : 0); 
    result = 31 * result + (totalWaitTime != null ? totalWaitTime.hashCode() : 0); 
    return result; 
} 
} 

FifoEntity映射到hbm.xml中: *

<class name="com.processmanagement.hibernate.entities.FifoEntity" table="fifo" schema="public" catalog="processmanagement"> 
    <id name="id" column="id"> 
     <generator class="sequence-identity"> 
      <param name="sequence">fifo_id_seq</param> 
     </generator> 
    </id> 
    <property name="numberOfProcesses" column="number_of_processes"/> 
    <property name="totalTime" column="total_time"/> 
    <property name="averageTime" column="average_time"/> 
    <property name="averageWaitTime" column="average_wait_time"/> 
    <property name="totalWaitTime" column="total_wait_time"/> 
</class> 

這就是我怎樣,我嘗試使用Hibernate來保存數據到數據庫:試圖保存一個連續的對象時

FifoEntity fifoEntity = new FifoEntity(processList.size(), totalTime, 
      averageProcessTime, averageWaitTime, totalWaitTime); 

    databaseHelper.saveOutcomeToDatabase(fifoEntity); 
. 
. 
. 
    public void saveOutcomeToDatabase(Object object) 
{ 
//  session.beginTransaction(); 
//  session.save(object); 
//  session.getTransaction().commit(); 
    Transaction transaction = session.beginTransaction(); 
    session.persist(object); 
    transaction.commit(); 
    session.clear(); 
} 

當然,錯誤:

org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "fifo_pkey" 
    Detail: Key (id)=(0) already exists. 

回答

0

經過幾個小時的代碼搞亂之後,即使從頭開始重新編譯這個項目,我也無法讓它正常工作。

結束了與使用SQL查詢,以將數據保存到數據庫,就像這樣:

 session.getTransaction().begin(); 
     SQLQuery sqlQuery = session.createSQLQuery("INSERT into public.fifo" + 
       " (number_of_processes, total_time, average_time, average_wait_time, total_wait_time) VALUES " + 
       "(" +((FifoEntity) object).getNumberOfProcesses() + ", " + 
       ((FifoEntity) object).getTotalTime()+ ", " + 
       ((FifoEntity) object).getAverageTime() + ", " + 
       ((FifoEntity) object).getAverageWaitTime() + ", " + 
       ((FifoEntity) object).getTotalWaitTime() + ");"); 
     session.getTransaction().commit(); 

這是一個有點亂,但它的工作原理,給我時間去了解Hibernate的更好,而不放棄項目或大量修改背後的整個想法。

2

爲了能夠使用序列有關休眠的實體,我們可以使用@SequenceGenerator和@Generated值 例如:

@Id 
@Basic(optional = false) 
@Column(name = "id") 
@SequenceGenerator(name="third_party_seq", sequenceName="third_party_seq",allocationSize=1) 
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="third_party_seq") 
private Long id; 

我使用Hibernate和PostgreSQL。欲瞭解更多的參考,我們可以從中讀到 - http://goo.gl/RQ0Dxg

0

剛纔碰到了同樣的事情。當在Hibernate中使用Postgres序列時,不要在序列中使用主鍵列上設置默認值,因爲Hibernate想要自己設置序列值。即。從您的表格定義中刪除:「default nextval('fifo_id_seq':: regclass)」