我是JPA和Hibernate的新手。我试图了解如何更新各个实体中的属性值。以我正在编写的代码为例 -
Semester sem = entitymanager.find(Semester.class, id);
sem.setSemNum(3);
entitymanager.getTransaction().commit();
此代码对数据库没有影响,当我查询学期表时没有更改任何值,也没有在日志中生成更新查询。根据这个tutorial,您获得了更改值的对象,然后将其提交回来。
我做错了什么?
以下是完整代码 - :
SemesterTest.java
public class SemesterTest {
private static EntityManagerFactory entityManagerFactory;
public static void main(String[] args) {
EntityManager entitymanager = null;
try {
entityManagerFactory = Persistence.createEntityManagerFactory("com.test.hibernate.jpa");
entitymanager = entityManagerFactory.createEntityManager();
entitymanager.getTransaction().begin();
Course course = new Course();
course.setCourseId("BCA");
course.setCourseName("Bachelor of Computer Applications");
Semester sem1 = new Semester();
sem1.setSemNum(1);
Semester sem2 = new Semester();
sem2.setSemNum(2);
ArrayList<Semester> semesters = new ArrayList<>();
semesters.add(sem1);
semesters.add(sem2);
course.setSemesters(semesters);
sem1.setCourse(course);
sem2.setCourse(course);
entitymanager.persist(course);
entitymanager.persist(sem1);
entitymanager.persist(sem2);
entitymanager.getTransaction().commit();
entitymanager.getTransaction().begin();
Semester id = new Semester();
id.setCourse(course);
id.setSemNum(1);
Semester sem = entitymanager.find(Semester.class, id);
sem.setSemNum(3);
entitymanager.getTransaction().commit();
}
catch(Exception e) {
e.printStackTrace();
if(entitymanager!=null)
entitymanager.getTransaction().rollback();;
}
finally {
if(entitymanager!=null)
entitymanager.close();
if(entityManagerFactory!=null)
entityManagerFactory.close();
}
}
}
Course.java
@Entity
@Table (name = "COURSES")
public class Course implements Serializable{
private static final long serialVersionUID = 1L;
private String courseId;
private String courseName;
private Collection<Semester> semesters = new ArrayList<>();
@OneToMany (targetEntity = Semester.class, mappedBy = "course")
public Collection<Semester> getSemesters() {
return semesters;
}
public void setSemesters(Collection<Semester> semesters) {
this.semesters = semesters;
}
@Id
@Column (name = "COURSE_ID")
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
@Column (name = "COURSE_NAME", nullable = false, unique = true)
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
}
Semester.java
@Entity
@Table (name = "SEMESTERS")
public class Semester implements Serializable{
private static final long serialVersionUID = 1L;
private int semNum;
private Course course;
@EmbeddedId
private SemesterPK pk;
@Id
@ManyToOne
@JoinColumn (name = "COURSE_ID")
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
@Id
@Column (name = "SEM_NUM")
public int getSemNum() {
return semNum;
}
public void setSemNum(int semNum) {
this.semNum = semNum;
}
}
Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="com.test.hibernate.jpa" transaction-type="RESOURCE_LOCAL">
<description>A Sample Hibernate Application</description>
<class>test.entity.Course</class>
<class>test.entity.Semester</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/practice"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
</properties>
</persistence-unit>
</persistence>
答案 0 :(得分:2)
ID是不可变的。你可能不会改变它。做正确的事情并使用单列,自动生成的纯技术ID。