Hibernate:必须在调用 save() 之前手动分配此类的 id

Posted

技术标签:

【中文标题】Hibernate:必须在调用 save() 之前手动分配此类的 id【英文标题】:Hibernate: ids for this class must be manually assigned before calling save() 【发布时间】:2013-04-15 01:22:50 【问题描述】:

我在使用 Hibernate 和 oneToMany 映射时遇到了一些问题。

这是我的功能:

        Location location = new Location();
        location.setDateTime(new Date());
        location.setLatitude(lat);
        location.setLongitude(lon);

        location = this.locationDao.save(location);

        merchant = new Merchant();
        merchant.setAddress(address);
        merchant.setCity(city);
        merchant.setCountry(country);
        merchant.setLocation(location);
        merchant.setName(name);
        merchant.setOrganization(organization);
        merchant.setPublicId(publicId);
        merchant.setZipCode(zipCode);
        merchant.setApplication(this.applicationDAO.findByPublicId(applicationPublicId));

        merchant = this.merchantDao.save(merchant);

        return merchant;

这是我的两个实体:

位置

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;


@Entity
@Table(name = "location")
@XmlRootElement
public class Location implements Serializable 
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Long id;

    @Basic(optional = false)
    @NotNull
    @Column(name = "latitude", nullable = false)
    private double latitude;

    @Basic(optional = false)
    @NotNull
    @Column(name = "longitude", nullable = false)
    private double longitude;

    @Basic(optional = false)
    @NotNull
    @Column(name = "date_time", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateTime;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "location")
    private List<Merchant> retailerList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "location")
    private List<MobileUser> userList;

    public Location() 
    

    public Location(Long id) 
        this.id = id;
    

    public Location(Long id, double latitude, double longitude, Date dateTime) 
        this.id = id;
        this.latitude = latitude;
        this.longitude = longitude;
        this.dateTime = dateTime;
    

    public Long getId() 
        return id;
    
    //getters and setters


    @XmlTransient
    public List<Merchant> getRetailerList() 
        return retailerList;
    

    public void setRetailerList(List<Merchant> retailerList) 
        this.retailerList = retailerList;
    

    @XmlTransient
    public List<MobileUser> getUserList() 
        return userList;
    

    public void setUserList(List<MobileUser> userList) 
        this.userList = userList;
    

    @Override
    public int hashCode() 
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    

    @Override
    public boolean equals(Object object) 
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Location)) 
            return false;
        
        Location other = (Location) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) 
            return false;
        
        return true;
    

    @Override
    public String toString() 
        return "com.fgsecure.geoloc.entities.Location[ id=" + id + " ]";
    

商家

import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonIgnore;


@JsonAutoDetect
@Entity
@Table(name = "retailer")
@XmlRootElement

public class Merchant implements Serializable 

    private static final long serialVersionUID = 1L;
    @JsonIgnore
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Long id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "public_id", nullable = false, length = 50)
    private String publicId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "name", nullable = false, length = 50)
    private String name;
    @Size(max = 50)
    @Column(name = "organization", length = 50)
    private String organization;
    @Size(max = 128)
    @Column(name = "address", length = 128)
    private String address;
    @Size(max = 10)
    @Column(name = "zip_code", length = 10)
    private String zipCode;
    @Size(max = 50)
    @Column(name = "city", length = 50)
    private String city;
    @Size(max = 50)
    @Column(name = "country", length = 50)
    private String country;
    @JoinColumn(name = "location_id", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Location location;
    @JoinColumn(name = "application_id", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false)
    private Application application;


    public Merchant() 
    

    public Merchant(Long id) 
        this.id = id;
    

    public Merchant(Long id, String publicId, String name) 
        this.id = id;
        this.publicId = publicId;
        this.name = name;
    

   //getters and setters


    @Override
    public int hashCode() 
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    

    @Override
    public boolean equals(Object object) 
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Merchant)) 
            return false;
        
        Merchant other = (Merchant) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) 
            return false;
        
        return true;
    

    @Override
    public String toString() 
        return "com.fgsecure.geoloc.entities.Retailer[ id=" + id + " ]";
    

每次我调用我的函数时,我都会得到:

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():

对于这一行:

location = this.locationDao.save(location);

我不明白为什么。 因为在这种情况下,无需手动分配位置 id,因为它是自动生成的。 我一定是做错了什么,但看了 3 天后我没有找到。

我正在使用 PostGreSQL,并且 id 是具有以下序列的自动生成的 ID:

nextval('location_id_seq'::regclass)`

编辑求解:

好的,谢谢您的回答。

如果以后可以帮助到其他人,只需要修改id的参数:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id",unique=true, nullable = false)

【问题讨论】:

你使用的是哪个数据库 商家为您正常工作 尝试将id算法名称更改为另一个进行测试 尝试将类型从 IDENTITY 更改为另一种仅用于测试目的 不应将您的问题标记为已解决并将解决方案添加到您的问题中,而应将您的解决方案添加为答案并接受该答案。 【参考方案1】:

Assign primary key in hibernate

确保该属性是数据库中的主键和Auto Incrementable。然后将其映射到数据类中,并使用 IDENTITY 使用带有 @GeneratedValue 注释的注释。

@Entity
@Table(name = "client")
data class Client(
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id") 
    private val id: Int? = null
)

GL

Source

【讨论】:

【参考方案2】:

您的 id 属性未设置。这可能是因为 DB 字段未设置为自动递增?你用的是什么数据库? mysql?您的字段是否设置为 AUTO INCREMENT?

【讨论】:

感谢您的回答。我正在使用带有自动生成 ID 的 PostGre。我刚刚编辑了我的帖子。 对于 MySQL 和 AI,这是可行的。您可能想检查 postgresql 是否也是这种情况。我的 pojo 几乎总是这样:@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id", unique=true, nullable=false) public Long getId() return this.id; 在 oracle 中这可能是由于序列名称引起的

以上是关于Hibernate:必须在调用 save() 之前手动分配此类的 id的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate Session中的save(),update(),delete(),saveOrUpdate() 细粒度分析

HIbernate - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例

hibernate 对象的持久化状态。 在session.save()之后 commit()之前 数据库有对应的记录吗?

来自其他包的 Api 出现错误“必须在调用 save() 之前手动分配此类的 ID”

用hibernate的 getCurrentSession取的 session啥时候关闭?

Save()saveOrUpdate()Hibernate的merge()方法