我在MS sql服务器中有主表和子表。我在子表中有一个外键引用。 但是仍然没有建立联系,值变成了空。
所以我有“学生”表和“ AcademicInfo”表。以下是其专栏。
学生-> [学生编号,姓名,姓氏,城市]
AcademicInfo-> [Id,StudentIdentity,主题,年份]
现在,我的外键在子表中,该表是带有父Student的StudentId列的AcademicInfo(StudentIdentity)。
我的案例是一对一的关系,因为一个学生只能有一个科目(仅出于锻炼目的)
AcademicInfo.java
package com.luv2code.hibernate.demo.entity;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="AcademicInfo")
public class AcademicInfo {
public AcademicInfo()
{}
public AcademicInfo(String subject, Integer year) {
Subject = subject;
Year = year;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="Id")
private int Id;
@Column(name="Subject")
public String Subject;
@Column(name="Year")
public Integer Year;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getSubject() {
return Subject;
}
public void setSubject(String subject) {
Subject = subject;
}
public Integer getYear() {
return Year;
}
public void setYear(Integer year) {
Year = year;
}
@Override
public String toString() {
return "AcademicInfo [Id=" + Id + ", Subject=" + Subject + ", Year=" + Year + "]";
}
@OneToOne(cascade= {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH})
@JoinColumn(name="StudentIdentity")
private Student studentInfo;
public Student getStudentInfo() {
return studentInfo;
}
public void setStudentInfo(Student studentInfo) {
this.studentInfo = studentInfo;
}
}
Student.java
package com.luv2code.hibernate.demo.entity;
import javax.persistence.*;
@Entity
@Table(name="StudentDetails")
public class Student {
public Student()
{}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="StudentID")
private int StudentID;
@Column(name="Name")
public String StudentName;
@Column(name="Surname")
public String Surname;
@Column(name="City")
public String City;
public int getStudentID() {
return StudentID;
}
public void setStudentID(int studentID) {
StudentID = studentID;
}
public String getName() {
return StudentName;
}
public void setName(String name) {
StudentName = name;
}
public String getSurname() {
return Surname;
}
public void setSurname(String surname) {
Surname = surname;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public Student(String name, String surname, String city) {
StudentName = name;
Surname = surname;
City = city;
}
@Override
public String toString() {
return "Student [StudentID=" + StudentID + ", StudentName=" + StudentName + ", Surname=" + Surname + ", City="
+ City + "]";
}
@OneToOne(mappedBy="studentInfo",cascade= {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.DETACH,CascadeType.REFRESH})
private AcademicInfo academicDetails;
public AcademicInfo getAcademicDetails() {
return academicDetails;
}
public void setAcademicDetails(AcademicInfo academicDetails) {
this.academicDetails = academicDetails;
}
}
主应用
package com.luv2code.hibernate.newDemo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.luv2code.hibernate.demo.entity.AcademicInfo;
import com.luv2code.hibernate.demo.entity.Student;
public class CreateStudentDemo {
public static void main(String[] args) {
SessionFactory factory = new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(AcademicInfo.class)
.addAnnotatedClass(Student.class)
.buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
Student theStudent1 = new Student("Andy","test","NY");
AcademicInfo stuAcademics = new AcademicInfo("OS", 2001);
theStudent1.setAcademicDetails(stuAcademics);
System.out.println(theStudent1);
session.save(stuAcademics);
session.getTransaction().commit();
try {
}
finally {
factory.close();
}
}
}
以下是“ hibernate.cfg.xml”
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=India;integratedSecurity=true</property>
<property name="connection.username">sa</property>
<property name="connection.password">Temp1234</property>
<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">100</property>
<!-- Select our SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>
<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
它引发如下错误:
无法将值NULL插入表“ dbo.AcademicInfo”的“ StudentIdentity”列中;列不允许为空。 INSERT失败。
如果我已经声明了关联,为什么不自动插入studentId?
答案 0 :(得分:0)
我使用以下链接进行更改
https://www.callicoder.com/hibernate-spring-boot-jpa-one-to-one-mapping-example/
现在抛出以下错误?
Exception in thread "main" org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation : com.luv2code.hibernate.demo.entity.AcademicInfo.studentInfo -> com.luv2code.hibernate.demo.entity.Student
我做对了吗?是否将studentId自动保存在StudentIdentity列中?