我尝试第一次映射关系。我找到了很多方法。
有两个类: - 项目(家长) - 申请(儿童)
因此,一个项目可以有多个应用程序,但一个应用程序只属于一个项目..
我按如下方式构建它们:
Project.java
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "project_id", nullable=false)
@org.hibernate.annotations.IndexColumn(name = "project_index")
List<Application> applications = new ArrayList<Application>();
[get and set methods...]
}
Application.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Application {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "project_id", updatable = false, insertable = false, nullable=false)
private Project project;
[get and set methods...]
}
...但不幸的是,我得到了一个我无法处理的例外:
javax.servlet.ServletException: org.hibernate.PropertyValueException: not-null property references a null or transient value: ....common.entities.Application._applicationsBackref
javax.faces.webapp.FacesServlet.service(FacesServlet.java:321)
org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359)
org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275)
org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:343)
org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272)
org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:83)
root cause
javax.faces.el.EvaluationException: org.hibernate.PropertyValueException: not-null property references a null or transient value: ....common.entities.Application._applicationsBackref
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
javax.faces.component.UICommand.broadcast(UICommand.java:311)
javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359)
org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275)
org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:343)
org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272)
org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:83)
root cause
org.hibernate.PropertyValueException: not-null property references a null or transient value: ....common.entities.Application._applicationsBackref
org.hibernate.engine.Nullability.checkNullability(Nullability.java:95)
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:313)
org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
我的错误在哪里?我的意思是什么都没有。仅在项目方面
当我在拥有方移除@JoinColumn时会发生异常:
java.sql.SQLException: Field 'project_id' doesn't have a default value
谢谢Pascal
答案 0 :(得分:19)
您的映射中有几件事需要解决。
mappedBy
属性(或者您将得到的是两个单向关联)来定义“拥有”拥有关系的字段一方(持有外键的一方,即一对多的多方)。@JoinColumn
注释应仅在拥有方定义,而不是双方定义。@OrderColumn
的标准@IndexColumn
注释@JoinColumn
定义为updatable = false, insertable = false, nullable=false
完全只读,这听起来不合适。所以你的映射成为(假设你正在使用JPA 2.0):
@Entity
public class Project {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToMany(cascade = CascadeType.ALL, mappedBy="project")
@OrderColumn(name = "project_index")
List<Application> applications = new ArrayList<Application>();
// getters, setters
}
并且
@Entity
public class Application {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "project_id")
private Project project;
// getters, setters
}
由于你的关联是双向的,所以在创建对象图时不要忘记设置链接的两边:
Project p = new Project();
Application a = new Application();
a.setProject(p);
p.getApplications().add(a);
em.persist(p);
但我实际上建议您在实体中创建链接管理方法以设置链接的两面,例如
@Entity
public class Project {
...
public void addToApplications(Application app) {
this.applications.add(app);
app.setProject(this);
}
...
}
答案 1 :(得分:4)
@JoinColumn
注释。事实上,你应该把它放在关系的一面。
此外,您已经描述了以下内容,
@JoinColumn(name = "project_id", updatable = false, insertable = false, nullable=false)
完全没有意义。实际上,这意味着您不想更新它,既不想插入它,也不想要它为空。
此外,如果实际列的名称不是@Column
,则您在Id
字段上缺少id
注释。
答案 2 :(得分:-1)
实体
@Table(name="Customer_v1")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public class Customer implements Serializable{
@Id
@Column(name="cust_id")
protected int custId;
------------------------------------------------
@Entity
@DiscriminatorValue(value = "N")
public class NewCustomer extends Customer {}
@Entity
@DiscriminatorValue( value="E")
public class ExistingCustomer extends Customer{}