Skip to content

Commit

Permalink
Revert "TRUNK-5664 Replace User hbm mapping file with annotations (op…
Browse files Browse the repository at this point in the history
…enmrs#3027)"

This reverts commit 267f4df.
  • Loading branch information
wluyima committed Dec 19, 2019
1 parent 35fee6f commit 4d53a02
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 75 deletions.
61 changes: 1 addition & 60 deletions api/src/main/java/org/openmrs/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,7 @@
import java.util.Map;
import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Cacheable;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.MapKeyColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.openmrs.api.context.Context;
import org.openmrs.util.LocaleUtility;
import org.openmrs.util.OpenmrsConstants;
Expand All @@ -59,65 +35,30 @@
* key-value pairs for either quick info or display specific info that needs to be persisted (like
* locale preferences, search options, etc)
*/
@Entity
@Table(name = "users")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
/*
*User inherits name and description properties from BaseOpenmrsMetadata but they don't exist in the users table since
*they are not used, because these field exist in BaseOpenmrsData and they are marked as persistent, hibernate generates
*a query containing these columns which of course fails to execute because of missing columns, so we introduce this
*dummy table containing these columns to 'please' hibernate until we fix the Class hierarchy see TRUNK-5665
*/
@SecondaryTable(name = "users_unused_fields")
@AttributeOverrides({
@AttributeOverride(name = "name", column = @Column(table = "users_unused_fields", name = "name", insertable = false, updatable = false)),
@AttributeOverride(name = "description", column = @Column(table = "users_unused_fields", name = "description", insertable = false, updatable = false)) })
public class User extends BaseChangeableOpenmrsMetadata implements java.io.Serializable, Attributable<User> {

public static final long serialVersionUID = 2L;

private static final Logger log = LoggerFactory.getLogger(User.class);

// Fields
@Id
@GeneratedValue
@Column(name = "user_id")

private Integer userId;

@ManyToOne(optional = false)
@JoinColumn(name = "person_id")
@LazyCollection(LazyCollectionOption.FALSE)
@Cascade(CascadeType.SAVE_UPDATE)
private Person person;

@Column(name = "system_id", nullable = false, length = 50)
private String systemId;

@Column(length = 50)
private String username;

@Column(unique = true)
private String email;

@ManyToMany
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role"))
@LazyCollection(LazyCollectionOption.FALSE)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Cascade({ CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.EVICT })
private Set<Role> roles;

@ElementCollection
@CollectionTable(name = "user_property", joinColumns = @JoinColumn(name = "user_id", nullable = false))
@MapKeyColumn(name = "property", length = 100)
@Column(name = "property_value")
@Cascade({ CascadeType.SAVE_UPDATE, CascadeType.MERGE, CascadeType.EVICT })
private Map<String, String> userProperties;

@Transient
private List<Locale> proficientLocales = null;

@Transient
private String parsedProficientLocalesProperty = "";

// Constructors
Expand Down
1 change: 1 addition & 0 deletions api/src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<mapping resource="org/openmrs/api/db/hibernate/PersonAddress.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/PersonMergeLog.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/PersonName.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/User.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/LoginCredential.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/Privilege.hbm.xml" />
<mapping resource="org/openmrs/api/db/hibernate/Role.hbm.xml" />
Expand Down
15 changes: 0 additions & 15 deletions api/src/main/resources/liquibase-update-to-2.3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,6 @@
</addColumn>
</changeSet>

<changeSet id="TRUNK-5664-20191010" author="Wyclif">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="users_unused_fields" />
</not>
</preConditions>
<comment>Adding users_unused_fields table</comment>
<createTable tableName="users_unused_fields">
<column name="user_id" type="int" >
<constraints nullable="false" unique="true" />
</column>
<column name="name" type="varchar(1)" />
<column name="description" type="varchar(1)" />
</createTable>
</changeSet>
<changeSet id="TRUNK-5672-201910211105" author="Mritunjay">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="0">
Expand Down
88 changes: 88 additions & 0 deletions api/src/main/resources/org/openmrs/api/db/hibernate/User.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0"?>
<!--
This Source Code Form is subject to the terms of the Mozilla Public License,
v. 2.0. If a copy of the MPL was not distributed with this file, You can
obtain one at http:https://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
the terms of the Healthcare Disclaimer located at http:https://openmrs.org/license.
Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
graphic logo is a trademark of OpenMRS Inc.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http:https://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.openmrs">

<class name="User" table="users">

<cache usage="read-write"/>

<id name="userId" type="java.lang.Integer" column="user_id" unsaved-value="0">
<generator class="native">
<param name="sequence">users_user_id_seq</param>
</generator>
</id>

<property name="uuid" type="java.lang.String"
column="uuid" length="38" unique="true" not-null="true"/>

<many-to-one name="person" class="Person" column="person_id"
not-null="true" lazy="false" cascade="save-update" />

<property name="systemId" type="java.lang.String" not-null="true"
column="system_id" length="50"/>

<property name="username" type="java.lang.String" length="50"/>

<property name="email" type="java.lang.String" length="255" unique="true"/>

<!--<many-to-one name="creator" class="User" not-null="true"/>
-->
<many-to-one name="creator" class="User" not-null="true"/>

<property name="dateCreated" type="java.util.Date" column="date_created"
not-null="true" length="19"/>

<many-to-one name="changedBy" class="User" column="changed_by"/>

<property name="dateChanged" type="java.util.Date" column="date_changed"
length="19"/>

<property name="retired" type="boolean" column="retired"
length="1" not-null="true"/>

<many-to-one name="retiredBy" class="User" column="retired_by"/>

<property name="dateRetired" type="java.util.Date" column="date_retired"
length="19"/>

<property name="retireReason" type="java.lang.String" column="retire_reason"
length="255"/>

<!-- Associations -->

<!-- bi-directional many-to-many association to Role -->
<set name="roles" table="user_role" lazy="false" cascade="save-update,merge,evict">
<cache usage="read-write"/>
<key>
<column name="user_id"/>
</key>
<many-to-many class="Role">
<column name="role"/>
</many-to-many>
</set>

<map name="userProperties" table="user_property" lazy="true"
cascade="save-update,merge,evict">
<key column="user_id" not-null="true"/>
<map-key column="property" type="java.lang.String" length="100"/>
<element column="property_value" type="java.lang.String"
length="255"/>
</map>

</class>

</hibernate-mapping>

0 comments on commit 4d53a02

Please sign in to comment.