Skip to content

Commit

Permalink
TRUNK-5495 Replace Visit hbm mapping file with annotations (#2909)
Browse files Browse the repository at this point in the history
  • Loading branch information
wluyima authored and dkayiwa committed May 29, 2019
1 parent a7f793d commit f9ba0d0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 93 deletions.
40 changes: 39 additions & 1 deletion api/src/main/java/org/openmrs/Visit.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@

import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.persistence.Access;
import javax.persistence.AccessType;
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.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.BatchSize;
import org.openmrs.customdatatype.Customizable;

/**
Expand All @@ -23,25 +39,47 @@
*
* @since 1.9
*/

@Entity
@Table(name = "visit")
public class Visit extends BaseCustomizableData<VisitAttribute> implements Auditable, Customizable<VisitAttribute> {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "visit_id")
private Integer visitId;

@ManyToOne(optional = false)
@JoinColumn(name = "patient_id")
private Patient patient;

@ManyToOne(optional = false)
@JoinColumn(name = "visit_type_id")
private VisitType visitType;

@ManyToOne
@JoinColumn(name = "indication_concept_id")
private Concept indication;

@ManyToOne
@JoinColumn(name = "location_id")
private Location location;

@Column(name = "date_started", nullable = false, length = 19)
private Date startDatetime;

@Column(name = "date_stopped", length = 19)
private Date stopDatetime;

@OneToMany(mappedBy = "visit")
@OrderBy("encounter_datetime desc, encounter_id desc")
private Set<Encounter> encounters;

@Access(AccessType.PROPERTY)
@OneToMany(mappedBy = "visit", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy("voided asc")
@BatchSize(size = 100)
private Set<VisitAttribute> attributes = new LinkedHashSet<>();

/**
* Default Constructor
*/
Expand Down
1 change: 0 additions & 1 deletion api/src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
<mapping resource="org/openmrs/api/db/hibernate/VisitType.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/VisitAttributeType.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/VisitAttribute.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/Visit.hbm.xml" />

<!-- Provider -->
<mapping resource="org/openmrs/api/db/hibernate/Provider.hbm.xml" />
Expand Down
85 changes: 0 additions & 85 deletions api/src/main/resources/org/openmrs/api/db/hibernate/Visit.hbm.xml

This file was deleted.

15 changes: 9 additions & 6 deletions api/src/test/java/org/openmrs/api/VisitServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.TransientObjectException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -938,21 +937,25 @@ public void saveVisit_shouldAssociateEncounterWithVisitOnSaveEncounter() {
/**
* @see VisitService#saveVisit(Visit)
*/
@Test(expected = TransientObjectException.class)
@Test
public void saveVisit_shouldNotPersistNewEncounter() {

Visit visit = visitService.getVisit(1);

EncounterService es = Context.getEncounterService();
final int originalCount = es.getEncountersByPatient(visit.getPatient()).size();

Encounter encounter = new Encounter();
encounter.setEncounterDatetime(new Date());
encounter.setEncounterType(Context.getEncounterService().getEncounterType(1));
encounter.setEncounterType(es.getEncounterType(1));
encounter.setPatient(visit.getPatient());
encounter.setLocation(visit.getLocation());
visit.addEncounter(encounter);

visitService.saveVisit(visit);

Context.flushSession();
assertNull(encounter.getEncounterId());
assertEquals(originalCount, es.getEncountersByPatient(visit.getPatient()).size());
}

/**
Expand Down

0 comments on commit f9ba0d0

Please sign in to comment.