这些标注尽量都要写到get方法的头上,我无意中把下面的标注写到了定义变量的头上时候,发生了错误。
报告错误:java.lang.NullPointerException
怎么回事,刚刚我试了下把@Id放到getId()方法的头上结果也报上面那样的错误,然后我再把@Id又放回到private Integer id;的头上结果就没错了。这个问题要待解决下...(我个人认为都放到get的头上,但是id要放到定义的头上,这个我也不知道是什么原因)
private Set<SiteChannel> siteChannelCollection = new HashSet<SiteChannel>();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "site")
public Set<SiteChannel> getSiteChannelCollection() {
return siteChannelCollection;
}
public void setSiteChannelCollection(Set<SiteChannel> siteChannelCollection) {
this.siteChannelCollection = siteChannelCollection;
}
private Site site;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "site_id")
public Site getSite() {
return site;
}
public void setSite(Site site) {
this.site = site;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created", length = 19)
public Date getCreated() {
return this.created;
}
public void setCreated(Date created) {
this.created = created;
}
参考的例子Site.java
package cn.ctit.cms.entity;
// Generated Feb 3, 2009 4:08:09 AM by Hibernate Tools 3.2.2.GA
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Transient;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Parent;
import org.hibernate.validator.Length;
import org.hibernate.validator.Min;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Pattern;
import cn.ctit.cms.entity.pay.Biller;
import cn.ctit.cms.entity.pay.Joinoption;
/**
* Site generated by hbm2java
*/
@Entity
@Table(name = "site", catalog = "cms")
public class Site implements java.io.Serializable {
private Long id;
private String name;
private String description;
private String siteUrl;
private Short siteType;
private String ioUrl;
private String ioService;
private Integer ioPort;
private String ioUser;
private String ioPass;
private String defaultFormat;
private Boolean enable = true;
private Short status;
private Date created;
private Date modified;
private Long serverId;
private String template;
private Set<SiteChannel> siteChannels = new HashSet<SiteChannel>(0);
private Set<SiteSchedule> siteSchedules = new HashSet<SiteSchedule>(0);
private Set<Joinoption> joinoptions = new HashSet<Joinoption>(0);
private Set<Biller> billers = new HashSet<Biller>(0);
//NATS
private String nats_siteid;
public Site() {
}
public Site(String name, String description, String siteUrl,
Short siteType, String ioUrl, String ioService, Integer ioPort,
String ioUser, String ioPass, String defaultFormat, Boolean enable,
Short status, Date created, Date modified,
Set<SiteChannel> siteChannels, Set<SiteSchedule> siteSchedules,Long serverId) {
this.name = name;
this.description = description;
this.siteUrl = siteUrl;
this.siteType = siteType;
this.ioUrl = ioUrl;
this.ioService = ioService;
this.ioPort = ioPort;
this.ioUser = ioUser;
this.ioPass = ioPass;
this.defaultFormat = defaultFormat;
this.enable = enable;
this.status = status;
this.created = created;
this.modified = modified;
this.siteChannels = siteChannels;
this.siteSchedules = siteSchedules;
this.serverId = serverId;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@NotNull
@Column(name = "name", length = 100)
@Length(max = 100)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "description", length = 200)
@Length(max = 200)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@NotNull
@Column(name = "site_url", length = 200)
@Length(max = 200)
public String getSiteUrl() {
return this.siteUrl;
}
public void setSiteUrl(String siteUrl) {
this.siteUrl = siteUrl;
}
@Column(name = "site_type")
public Short getSiteType() {
return this.siteType;
}
public void setSiteType(Short siteType) {
this.siteType = siteType;
}
@NotNull
@Column(name = "io_url", length = 200)
@Length(max = 200)
public String getIoUrl() {
return this.ioUrl;
}
public void setIoUrl(String ioUrl) {
this.ioUrl = ioUrl;
}
@Pattern(regex="[a-zA-Z]+[0-9]*",message="Please enter right value : transform!")
@Column(name = "io_service", length = 50)
@Length(max = 50)
public String getIoService() {
return this.ioService;
}
public void setIoService(String ioService) {
this.ioService = ioService;
}
@Min(value=1)
@Column(name = "io_port")
public Integer getIoPort() {
return this.ioPort;
}
public void setIoPort(Integer ioPort) {
this.ioPort = ioPort;
}
@NotNull
@Column(name = "io_user", length = 50)
@Length(max = 50)
public String getIoUser() {
return this.ioUser;
}
public void setIoUser(String ioUser) {
this.ioUser = ioUser;
}
@NotNull
@Column(name = "io_pass", length = 50)
@Length(max = 50)
public String getIoPass() {
return this.ioPass;
}
public void setIoPass(String ioPass) {
this.ioPass = ioPass;
}
@Column(name = "default_format", length = 200)
@Length(max = 200)
public String getDefaultFormat() {
return this.defaultFormat;
}
public void setDefaultFormat(String defaultFormat) {
this.defaultFormat = defaultFormat;
}
@Column(name = "enable")
public Boolean getEnable() {
return this.enable;
}
public void setEnable(Boolean enable) {
this.enable = enable;
}
@Column(name = "status")
public Short getStatus() {
return this.status;
}
public void setStatus(Short status) {
this.status = status;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created", length = 19)
public Date getCreated() {
return this.created;
}
public void setCreated(Date created) {
this.created = created;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "modified", length = 19)
public Date getModified() {
return this.modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "site")
public Set<SiteChannel> getSiteChannels() {
return this.siteChannels;
}
public void setSiteChannels(Set<SiteChannel> siteChannels) {
this.siteChannels = siteChannels;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "site")
public Set<SiteSchedule> getSiteSchedules() {
return this.siteSchedules;
}
public void setSiteSchedules(Set<SiteSchedule> siteSchedules) {
this.siteSchedules = siteSchedules;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "site")
public Set<Joinoption> getJoinoptions() {
return joinoptions;
}
public void setJoinoptions(Set<Joinoption> joinoptions) {
this.joinoptions = joinoptions;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "site")
public Set<Biller> getBillers() {
return billers;
}
public void setBillers(Set<Biller> billers) {
this.billers = billers;
}
@Column(name = "server_id")
public Long getServerId() {
return this.serverId;
}
public void setServerId(Long serverId) {
this.serverId = serverId;
}
@Column(name = "nats_siteid")
public String getNats_siteid() {
return nats_siteid;
}
public void setNats_siteid(String nats_siteid) {
this.nats_siteid = nats_siteid;
}
//@Transient
public String getTemplate() {
return template;
}
public void setTemplate(String template) {
this.template = template;
}
}