因為project 會從Java 轉移到.net 中, 趁此機會順便對Java JPA 有關的mapping 摘錄, 以便以後查看.
@Entity @Table(name = "OT_WAITING_QUEUE") @JsonInclude(Include.NON_ABSENT) public class ClassA { // Primary Key mapping settings. @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long classAId; // Column mapping if different name. @Column(name = "book_time") private Date bookingStartAt; // Column mapping to assoicated table. (one to one) @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "classB_id", updatable = false) private ClassB classB; @OneToMany(cascade=CascadeType.ALL, mappedBy="classA") @Where(clause="isActive = 1") private Set<ClassC> classCs; // Getter and setters. // Getter attribute to prevent recursive serialization. @JsonManagedReference public Set<ClassC> getClassCs() { return classCs; } public void setClassCs(Set<PatientConsentForm> classCs) { this.classCs= classCs; } } @Entity @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="patientConsentFormId", scope=PatientConsentForm.class) public class ClassC extends AccountableEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long classCId; // Many-to-one mpaaing settings. @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "ClassAId",updatable = false) private ClassA classA; // Getter attribute to prevent recursive serialization. @JsonBackReference public ClassA getClassA() { return classA; } public void setClassA(ClassA classA) { this.classA = classA; } }
Abstract class 的設定.
// Decide it is abstract class and mapping implement in child class. @MappedSuperclass public abstract class AccountableEntity extends SoftDeleteEntity { private Date CreatedAt; private String CreatedBy; private Date LastModifiedAt; private String LastModifiedBy; public Date getCreatedAt() { return CreatedAt; } public void setCreatedAt(Date createdAt) { CreatedAt = createdAt; } public String getCreatedBy() { return CreatedBy; } public void setCreatedBy(String createdBy) { CreatedBy = createdBy; } public Date getLastModifiedAt() { return LastModifiedAt; } public void setLastModifiedAt(Date lastModifiedAt) { LastModifiedAt = lastModifiedAt; } public String getLastModifiedBy() { return LastModifiedBy; } public void setLastModifiedBy(String lastModifiedBy) { LastModifiedBy = lastModifiedBy; } public void updateAccountableInfo(String modifiedBy) { Date currentDate=new Date(); if(this.getCreatedAt()==null) { this.setCreatedAt(currentDate); this.setCreatedBy(modifiedBy); } this.setLastModifiedAt(currentDate); this.setLastModifiedBy(modifiedBy); } }
Repository
@Repository public interface TestRepository extends CrudRepository<Test, Long> { @Query(value="SELECT OTWQ, p, r, s1, dohp " + "FROM Test OTWQ " + "LEFT JOIN FETCH OTWQ.patient p " + "JOIN FETCH OTWQ.room r " + "JOIN FETCH OTWQ.surgeon1 s1 " + "JOIN FETCH OTWQ.departmentOfHealthProcedure dohp " + "WHERE OTWQ.room.id in :theatreIds " + "AND OTWQ.surgeon1.id in :doctorIds " + "ORDER BY OTWQ.bookingStartAt ") List<OperationTheatreWaitingQueue> findByCriteria( @Param("theatreIds") List<Long> theatreIds, @Param("doctorIds") List<Long> doctorIds); }
在 Controller 中透過DI 叫用repository.
@RestController @RequestMapping(path = "/doctor") @CrossOrigin(origins = "*") public class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController .class); @Autowired private DoctorRepository doctorRepository; @GetMapping(path = "/findAll") public List<Doctor> findAll(@RequestParam boolean isActive) { logger.debug("findAll() started. isActive="+isActive); List<Test> result=new ArrayList<Test>(); result=testRepository.findAllTests(isActive); logger.debug("findAll() executed. no. of record(s) found="+result.size()); return result; } @PostMapping(path = "/findOperationThreaterWaitingQueuesByCriteria") public @ResponseBody List<OperationTheatreWaitingQueue> findOperationThreaterWaitingQueuesByCriteria( @RequestBody WaitingQueueSearchCriteria searchCriteria) { } }
application.properties
-- Hibernate connection settings. spring.jpa.hibernate.ddl-auto=none spring.datasource.url=jdbc:sqlserver://Test:1433;databaseName=HMS spring.datasource.username=AppUser spring.datasource.password= -- JPA debug settings. spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true -- Hibernate class mapping conversion. spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl -- For JBoss deployment settings. spring.jmx.enabled=false spring.jackson.serialization.fail-on-empty-beans=false -- Set spring boot log level. logging.level.org.springframework.web=DEBUG
Leave a Reply