Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRUNK-5664 Replace User hbm mapping file with annotations #3027

Merged
merged 1 commit into from
Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion api/src/main/java/org/openmrs/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,31 @@
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 @@ -35,30 +59,65 @@
* 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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put some code comments explaining why we are introducing the users_unused_fields table?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@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: 0 additions & 1 deletion api/src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<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
16 changes: 16 additions & 0 deletions api/src/main/resources/liquibase-update-to-2.3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,20 @@
<column name="date_changed" type="DATETIME"/>
</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>
</databaseChangeLog>
88 changes: 0 additions & 88 deletions api/src/main/resources/org/openmrs/api/db/hibernate/User.hbm.xml

This file was deleted.