1
我試圖用正確的ID插入映射對象到postgresql數據庫。這是我的插入:休眠不使用定義的序列(postgresql)
Session session = Main.getSession();
Transaction rx = session.beginTransaction();
ProductsEntity productsEntity = new ProductsEntity();
productsEntity.setName(nameTextField.getText());
productsEntity.setDescription(descriptionTextArea.getText());
productsEntity.setCategory((ProductCategoriesEntity) categoryComboBox.getSelectedItem());
productsEntity.setPrice(new BigDecimal(1.0));
session.save(productsEntity);
tx.commit();
session.close();
類具有限定的序列:
@Entity
@Table(name = "products", schema = "public", catalog = "shop")
public class ProductsEntity {
private int id;
private String name;
private String description;
private BigDecimal price;
@Id
@Column(name = "id", nullable = false)
@SequenceGenerator(name="pk_sequence",sequenceName="products_id_seq")
@GeneratedValue(strategy=GenerationType.AUTO,generator="pk_sequence")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name", nullable = false, length = -1)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "description", nullable = false, length = -1)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Basic
@Column(name = "price", nullable = false, precision = 2)
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProductsEntity that = (ProductsEntity) o;
if (id != that.id) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (description != null ? !description.equals(that.description) : that.description != null) return false;
if (price != null ? !price.equals(that.price) : that.price != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
result = 31 * result + (price != null ? price.hashCode() : 0);
return result;
}
private ProductCategoriesEntity category;
@ManyToOne
public ProductCategoriesEntity getCategory() {
return category;
}
public void setCategory(ProductCategoriesEntity category) {
this.category = category;
}
}
最後的PostgreSQL序列products_id_seq的值是4。休眠被插入與ID = 0的對象,因此第一插入是成功的,現在我得到了獨特的約束違規。
編輯 我已經在XML映射定義
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entities.ProductsEntity" table="products" schema="public" catalog="shop">
<id name="id">
<column name="id" sql-type="integer"/>
<generator class="identity"/>
</id>
<property name="name">
<column name="name" sql-type="varchar"/>
</property>
<property name="description">
<column name="description" sql-type="varchar"/>
</property>
<property name="price">
<column name="price" sql-type="numeric(9,2)" precision="9" scale="2"/>
</property>
<many-to-one name="category" column="category_id" class="entities.ProductCategoriesEntity" lazy="false"/>
</class>
</hibernate-mapping>
這個工程沒有在類中定義序列添加發生器
<generator class="identity"/>
取得序列工作。我也試過在類文件中使用
@GeneratedValue(strategy=GenerationType.IDENTITY)
但它不起作用。
如果有人有一個想法,請告訴我爲什麼只有「XML」方法的作品。
您是使用Hibernate生成表還是通過控制檯手動生成表?這對'SequenceGenerator'註釋有所幫助。 – coladict
@coladict表序列是手動創建的,xml類是通過inteliij ide – dawidmt
生成的。當使用'@ SequenceGenerator'時,'allocationSize'必須匹配序列的增量值。默認'allocationSize'是50,默認的增量大小是1,所以這會導致它嘗試插入重複的ID。但是,如果列從該序列中獲取它的默認值,那麼沒有理由使@GeneratedValue(strategy = GenerationType.IDENTITY)不起作用。 – coladict