我在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,因为它是自动生成的。我一定是做错了什么,但三天后,我没有发现。
nextval('location_id_seq'::regclass)`
如果将来对其他人有帮助,只需通过以下方法修改id的参数:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id",unique=true, nullable = false)
未设置id属性。这可能是因为DB字段没有设置为自动递增?你用的是什么数据库?MySQL?您的字段是否设置为自动递增?
我有一个包含字段名、姓、性别、电子邮件、密码、DOB、地址和角色的user类。 我想创建电子邮件作为我的主键,因此我有@id,但由于它是字符串类型,我不能使用@Generation注释。每当我尝试在Db中插入一个新用户时,我都会得到一个错误,嵌套的异常是org.hibernate.id.IdentifierGenerationException:在使用根本原因调用save():com.niit.m
我知道这个问题以前可能有人问过,但我没有得到一个解决我的问题的方法。我有一个varchar(20)的categoryID的主键列,实体类通过id属性映射到该主键列。在添加数据时,它采用通过UI输入的id值,但在编辑其他字段并通过UI保存时,它抛出异常“IdentifierGenerationException:在调用save()之前必须手动分配该类的id:” 请找到下面的代码: 在过去的两天里,请
我有这个我解决不了的问题,这是我的实体类: 最后一行是失败的,如果我移除它们,它起作用,我的意思是,它可以保存主题,但当我尝试这样做,它失败,我不知道如果我做错了什么,请帮助我!!
问题内容: 我在hibernate映射方面遇到了一些问题。就像这里 : 当我打电话给我时,出现异常: 为何hibernate无法单独使用这些ID?怎么了 问题答案: 首先-它不能生成它们,因为您没有告诉它怎么做。使用并选择所需的策略。 然后-您不能有两个字段。如果您想使用复合ID,请使用或
我的数据库上需要一个主键。 问题是,如果我在注释字段中使用类型,那么当我尝试保存对象时,Hibernate会抛出一个异常。 是的,我正在为字段设置一个值。 将批注添加到字段-未工作 将字段类型更改为-这对我是不可行的 添加接收作为参数的构造函数-未工作 使用UUID-我不能,因为此id是由POST请求中的字段设置的。 这些变通办法对我都没用。 更新 我发布的请求: MyService.java