[Java Spring Data] JPA Example with unit test
2023-09-14 09:00:46 时间
domain/Course.java
package com.example.university.domain; import javax.persistence.*; /** * JPA Entity for a Course offered at the University. * <p> * Created by maryellenbowman. */ @Entity @Table(name = "COURSE") public class Course { @Id @GeneratedValue private Integer id; @Column private String name; @ManyToOne @JoinColumn private Department department; public Course(String name, Department department) { this.name = name; this.department = department; } protected Course() { } public Integer getId() { return id; } public String getName() { return name; } public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "Course{" + "id=" + id + ", name='" + name + '\'' + ", department=" + department.getName() + '}'; } }
domain/Department.java
package com.example.university.domain; import javax.persistence.*; import java.util.ArrayList; import java.util.List; /** * JPA Entity for a Department of study at the University. * <p> * Created by maryellenbowman */ @Entity @Table(name = "Department") public class Department { @Id @GeneratedValue private Integer id; @Column private String name; @OneToMany(mappedBy = "department", fetch = FetchType.EAGER, cascade = CascadeType.ALL) private List<Course> courses = new ArrayList<>(); public Department(String name) { this.name = name; } protected Department() { } public Integer getId() { return id; } public String getName() { return name; } public void addCourse(Course course) { courses.add(course); } @Override public String toString() { return "Department{" + "id=" + id + ", name='" + name + '\'' + ", courses=" + courses + '}'; } }
domain/Person.java
package com.example.university.domain; import javax.persistence.Column; import javax.persistence.Embeddable; /** * Person encapsulates an individual's first and last name. * <p> * Created by maryellenbowman */ @Embeddable public class Person { @Column private String firstName; @Column private String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } protected Person() { } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } @Override public String toString() { return " firstName='" + firstName + '\'' + ", lastname='" + lastName + "\' "; } }
domain/Student.java
package com.example.university.domain; import javax.persistence.*; import java.util.ArrayList; import java.util.List; /** * JPA Entity representing a student at the University. * <p> * Created by maryellenbowman */ @Entity @Table(name = "STUDENT") public class Student { @Id @GeneratedValue private Integer studentId; @Column private boolean fullTime; @Column private Integer age; @Embedded private Person attendee; @OneToMany private List<Course> courses = new ArrayList<>(); public Student(Person attendee, boolean fullTime, Integer age) { this.attendee = attendee; this.fullTime = fullTime; this.age = age; courses = new ArrayList<>(); } protected Student() { } public Integer getStudentId() { return studentId; } public Person getAttendee() { return attendee; } public void setAge(Integer age) { this.age = age; } public boolean isFullTime() { return fullTime; } public Integer getAge() { return age; } public List<Course> getCourses() { return courses; } @Override public String toString() { return "Student{" + "studentId=" + studentId + ", " + attendee + ", fullTime=" + fullTime + ", age=" + age + "}\n"; } }
repo/StudentRepository.java
package com.example.university.repo; import com.example.university.domain.Student; import org.springframework.data.repository.CrudRepository; /** * DataSource Management for the Students at the University. * * Created by maryellenbowman */ public interface StudentRepository extends CrudRepository<Student, Integer>{ }
test:
package com.example.university; import com.example.university.domain.Person; import com.example.university.domain.Student; import com.example.university.repo.StudentRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * Demonstrate Creation, Reading, Updating, and Deletion of Students with StudentRepository * <p> * Created by maryellenbowman */ @RunWith(SpringRunner.class) @SpringBootTest public class CrudRepositoryDemo { @Autowired StudentRepository studentRepository; /** * Exercise CrudRepository methods. */ @Test public void simpleStudentCrudExample() { boolean fullTime = true; studentRepository.save(new Student(new Person("jane", "doe"), fullTime, 20)); studentRepository.save(new Student(new Person("john", "doe"), fullTime, 22)); studentRepository.save(new Student(new Person("mike", "smith"), fullTime, 18)); studentRepository.save(new Student(new Person("ally", "kim"), !fullTime, 19)); System.out.println("\n*************Original Students*************"); studentRepository.findAll().forEach(System.out::println); //age up the students studentRepository.findAll().forEach(student -> { student.setAge(student.getAge() + 1); studentRepository.save(student); }); System.out.println("\n*************Students a year older*************"); studentRepository.findAll().forEach(System.out::println); studentRepository.deleteAll(); System.out.println("\n*************Students removed*************"); studentRepository.findAll().forEach(System.out::println); } }
相关文章
- Java解析Xmind文件
- excel宏 java,Microsoft Excel宏运行Java程序
- java验证手机号正则表达式_Java使用正则表达式验证手机号和电话号码的方法「建议收藏」
- java定时器scheduled_spring定时任务注解
- java中获取当前时间_java如何获得当前时间
- vector中find函数用法_java中set的用法
- Java框架Spring入门-第一个spring项目
- IDEA打开导入别人的java项目后不能运行的解决方法
- 快递鸟Java SDK的功能介绍、API介绍和示例代码
- java并发编程(2):Java多线程-java.util.concurrent高级工具
- Java基础学习笔记十 Java基础语法之final、static、匿名对象、内部类详解编程语言
- java音乐播放器-乐乐音乐播放器详解编程语言
- Java数组去掉重复的方法集详解编程语言
- Oracle 视图 DBA_JAVA_IMPLEMENTS 官方解释,作用,如何使用详细说明
- Spring Boot(五):spring data jpa的使用详解编程语言
- 利用Java JDBC连接Oracle数据库(jdbc连接oracle)
- 数据处理处理Redis Java中过期数据的最佳方法(redisjava过期)
- 缓存Redis与Java结合实现高效过期缓存(redisjava过期)
- 处理Java中Redis过期处理实践(redisjava过期)
- Exploring the Power of Java with MongoDB for Enhanced Data Management(java与mongodb)
- Using Java to Work with MongoDB: A Guide for Developers(java操作mongodb)
- Linux平台上C语言接口调用Java语言实现(linux c调java)
- 让Java开发能力在Linux下得到更大发挥(java linux编程)
- 数据Java操作MySQL库:获取你所需的数据(java获取mysql)
- Linux下开发靠谱的Java应用(linux基于java)
- 如何在Linux系统中安装Java(linux中安装java)
- Java Redis一次实现无限数据加速的尝试(用java redis)
- 客户端Java客户端快速关闭Redis连接(关闭redis的java)