001/*
002 * #%L
003 * HAPI FHIR JPA Model
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.model.entity;
021
022import jakarta.annotation.Nullable;
023import jakarta.persistence.Column;
024import jakarta.persistence.ConstraintMode;
025import jakarta.persistence.ForeignKey;
026import jakarta.persistence.JoinColumn;
027import jakarta.persistence.ManyToOne;
028import jakarta.persistence.MappedSuperclass;
029import org.hibernate.annotations.NotFound;
030import org.hibernate.annotations.NotFoundAction;
031
032import java.io.Serializable;
033
034@MappedSuperclass
035public abstract class BaseTag extends BasePartitionable implements Serializable {
036
037        private static final long serialVersionUID = 1L;
038
039        /**
040         * Every tag has a reference to the tag definition. Note that this field
041         * must not have a FK constraint! In this case, Postgres (and maybe others)
042         * are horribly slow writing to the table if there's an FK constraint.
043         * See https://pganalyze.com/blog/5mins-postgres-multiXact-ids-foreign-keys-performance
044         */
045        @ManyToOne(cascade = {})
046        @JoinColumn(name = "TAG_ID", nullable = false, foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
047        @NotFound(action = NotFoundAction.IGNORE)
048        private TagDefinition myTag;
049
050        @Column(name = "TAG_ID", insertable = false, updatable = false)
051        private Long myTagId;
052
053        public Long getTagId() {
054                return myTagId;
055        }
056
057        /**
058         * This can be null if the tag definition has been deleted. This
059         * should never happen unless someone has manually messed with
060         * the database, but it could happen.
061         */
062        @Nullable
063        public TagDefinition getTag() {
064                return myTag;
065        }
066
067        public void setTag(TagDefinition theTag) {
068                myTag = theTag;
069        }
070}