我在spring boot尝试用hibernate进行多对一的单向映射。我有以下几个学生班--很多
package jpa.many.to.one.unidirectional.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "STUDENT")
public class Student {
@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
private long id;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "SECTION")
private String section;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="UNIVERSITY_ID")
private University university;
public Student() {
}
public Student(String firstName, String lastName, String section, University university) {
this.firstName = firstName;
this.lastName = lastName;
this.section = section;
this.university = university;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public University getUniversity() {
return university;
}
public void setUniversity(University university) {
this.university = university;
}
@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName="
+ lastName + ", section=" + section + "]";
}
}
大学-一个班级
package jpa.many.to.one.unidirectional.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "UNIVERSITY")
public class University {
@Id
@GeneratedValue
@Column(name = "UNIVERSITY_ID")
private long id;
@Column(name = "NAME")
private String name;
@Column(name = "COUNTRY")
private String country;
public University() {
}
public University(String name, String country) {
this.name = name;
this.country = country;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "University [id=" + id + ", name=" + name + ", country=" + country + "]";
}
}
应用程序另存和运行为
package jpa.many.to.one.unidirectional;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import jpa.many.to.one.unidirectional.model.Student;
import jpa.many.to.one.unidirectional.model.University;
import jpa.many.to.one.unidirectional.repo.StudentRepo;
import jpa.many.to.one.unidirectional.repo.UniversityRepo;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
// SpringApplication.run(Application.class, args);
ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Application.class, args);
UniversityRepo universityRepo = configurableApplicationContext.getBean(UniversityRepo.class);
StudentRepo studentRepo = configurableApplicationContext.getBean(StudentRepo.class);
University university = new University("MG", "India");
Student fstudent = new Student("Deeksha", "Sivakumar", "A", university);
fstudent.setUniversity(university);
studentRepo.save(fstudent);
Student fstudent1 = new Student("Sivakumar", "Nair", "A", university);
fstudent1.setUniversity(university);
studentRepo.save(fstudent1);
}
}
但我的原因是:org.hibernate.PersistentObjectException:传递给Persisted的分离实体:jpa.many.to.oniredirectoral.model.university(位于org.hibernate.event.internal.defaultPersistentListener.onPersist(DefaultPersistEventListener.java:120)(位于org.hibernate.event.service.internal.eventListener.java:113)(位于t org.hibernate.event.internal.AbstractSaveEventListener.PerformSaveorReplicate(AbstractSaveEventListener.java:264)在org.hibernate.event.internal.AbstractSaveEventListener.PerformSave(AbstractSaveEventListener.java:193)在org.hibernate.event.internal.AbstractSaveEventListener.SaveWithGeneratedId(AbstractSaveEventListener.135)在方法)在java.base/jdk.internal.reflect.NativeMethodAccessorImpl.Invoke(NativeMethodAccessorImpl.java:62)在java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.Invoke(DelegatingMethodAccessorImpl.java:43)在java.base/java.lang.Reflect.Method.Invoke(Method.java:567)在OnMetadata.Invoke(ImplementationInvocationMetadata.java:72)位于org.springframework.data.repository.core.support.repositorycomposition$RepositoryFragments.Invoke(Repositorycompositor.java:382)位于org.springframework.data.repository.core.support.repositorycomposition.Invoke(Repositorycompositor.java:205)位于InvokingMethodInceptor.Invoke(DefaultMethodInceptor.java:80)在org.springFramework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)在org.springFramework.transaction.Inceptor.TransactionAspectSupport.InvokeWithinTransaction(TransactionAspectSupport.367)在
我在多对一注释中尝试了cascade.merge和optional-=false。但我得到了上面提到的错误,请帮助
@ManyToOne(cascade=CascadeType.All)在您的情况下,这似乎是一个坏主意,因为删除一个学生将导致删除相关的大学。由于一所大学可以有多个学生,另一个学生将成为孤儿
我认为你应该在大学实体中使用这个关系,而不是学生实体
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="STUDENT_ID")
private List<Student> students;
如果您想使用您的模型,请尝试在将大学设置为student之前保存它(因为在您的代码中,您试图保存同一个大学两次,第一次是fstudent,第二次是fstudent1),在这种情况下,我建议您不要使用级联:
University university = universityRepo.save(new University("MG", "India"));
Student fstudent = new Student("Deeksha", "Sivakumar", "A", university);
fstudent.setUniversity(university);
studentRepo.save(fstudent);
Student fstudent1 = new Student("Sivakumar", "Nair", "A", university);
fstudent1.setUniversity(university);
studentRepo.save(fstudent1);
我有如下雇员类 当我调用来persisting时,我会得到以下错误
我在Tomcat 7.0.32服务器上运行Java7.0.11。 你知道吗?
当试图在web应用程序上创建课程实体实例时,我们遇到以下错误。这是一个JHipster应用程序完整的错误日志,下面是,但主要错误是 更新的代码:
问题内容: 我已经成功用hibernate写了我的第一个主要的孩子例子。几天后,我再次使用它并升级了一些库。不知道我做了什么,但是我再也无法使它运行了。有人可以帮助我找出返回以下错误消息的代码中的错误吗: hibernate映射: 编辑: InvoiceManager.java 发票.java InvoiceItem.java 编辑: 从客户端发送的JSON对象: 编辑: 一些详细信息: 我试图通
我的课程如下: sportcenter.java service.java 提前道谢!
问题内容: 我正在创建一个简单的应用程序,只需使用将一行插入到表中(如果表不存在,则创建它)。 我为它的一个可运行示例附加了一些代码。 这是我得到的异常和stacktrace: 这是我的代码: 主班: 和Person类: 这是我的persistence.xml文件 -----------------------编辑-------------------------- – 我只是将提供程序更改为Ec