-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Trunk 5866 : OrderSet Domain - Switching from Hibernate Mappings to Annotations #5472
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
base: master
Are you sure you want to change the base?
Changes from all commits
0cc41b4
565e9fe
1b8e0a6
ffc3856
206cad7
4f7b185
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,23 @@ | |
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.OneToMany; | ||
| import jakarta.persistence.OrderColumn; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.CascadeType; | ||
| import jakarta.persistence.AssociationOverride; | ||
| import org.hibernate.annotations.JdbcTypeCode; | ||
| import org.hibernate.envers.Audited; | ||
| import org.hibernate.type.SqlTypes; | ||
| import org.openmrs.api.APIException; | ||
|
|
||
| /** | ||
|
|
@@ -21,7 +37,13 @@ | |
| * | ||
| * @since 1.12 | ||
| */ | ||
| @Entity | ||
| @Table(name = "order_set") | ||
| @Audited | ||
| @AssociationOverride( | ||
| name = "attributes", | ||
| joinColumns = @JoinColumn(name = "order_set_id") | ||
| ) | ||
| public class OrderSet extends BaseCustomizableMetadata<OrderSetAttribute> { | ||
|
|
||
| public static final long serialVersionUID = 72232L; | ||
|
|
@@ -36,12 +58,23 @@ public enum Operator { | |
| ALL, ONE, ANY | ||
| } | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name ="order_set_id" ) | ||
| private Integer orderSetId; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| @JdbcTypeCode(SqlTypes.VARCHAR) | ||
| @Column(name = "operator", nullable = false) | ||
| private Operator operator; | ||
|
|
||
|
|
||
| @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How did you deal with the lazy attribute?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think OneToMany has default fetch type lazy , ref : https://stackoverflow.com/questions/26601032/default-fetch-type-for-one-to-one-many-to-one-and-one-to-many-in-hibernate
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think you can come up with a test in https://github.com/openmrs/openmrs-core/blob/master/api/src/test/java/org/openmrs/api/OrderSetServiceTest.java to catch this problem? As in a test which would fail when using |
||
| @JoinColumn(name = "order_set_id", nullable = false) | ||
| @OrderColumn(name = "sequence_number") | ||
| private List<OrderSetMember> orderSetMembers; | ||
|
|
||
|
|
||
| @ManyToOne | ||
| @JoinColumn(name = "category") | ||
| private Concept category; | ||
|
|
||
| /** | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this override for?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This overrides the join column name for attributes field ,actually in the OrderSet's parent class which is BaseCustomizableMetadata it has join column name location_id but in orderset.hbm.xml it has order_set_id join column name so it's overriding that join column name from it's parent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you evaluate using
AttributeOverride?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AttributeOverride works well for normal fields not for relationship attributes so here AssociationOverride will work for relationships columns like OneToMany
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even when you are not modifying a relationship but just renaming a filed like here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes , this just renaming the column name , it's just can change how it should be mapped to databased like joincolumn name or jointable name , but can't actually modify the relationship like onetomany to manytoone , fetch type etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't that then mean AttributeOverride is enough for this use case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t think
@AttributeOverrideworks here. It’s used for simple attributes for example, renaming a column on a basic field like@Column(name = "idt") private String identifier;or for fields inside an@Embeddable.It only changes how a single column is named or mapped.But for relationships like
@OneToManyor@ManyToOne, we use@AssociationOverride. That’s what lets us change the name of the join column or join table for an existing relation.This is what I understand, please let me know if I’m missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between this and https://github.com/openmrs/openmrs-core/blob/2.8.1/api/src/main/java/org/openmrs/Location.java#L52 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The @AttributeOverride(name = "attributes", column = @column(name = "location_id")) annotation on the Location entity is not doing anything meaningful and can be safely removed. Why ? because :@AttributeOverride only works for simple fields (String, Integer, Date, etc.), not relationships like @onetomany
In BaseCustomizableMetadata, the attributes field is a relationship (@onetomany), not a basic column:
Since @AttributeOverride cannot override relationships, it gets silently ignored during entity mapping. The application runs fine without it which i also verified .
Ref : https://www.baeldung.com/jpa-attributeoverride
Recommendation: Remove the @AttributeOverride annotation from Location as it serves no purpose.