<%#
 Copyright 2013-2024 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<&_
 // Register sections and max allowed fragments, 0 for unlimited.
  fragments.registerSections({
    importSection: 0,
    staticImportSection: 0,
    annotationSection: 0,
    extendsSection: 1,
    additionalInterfacesSection: 0,
    classStaticFieldsSection: 0,
    classFieldsSection: 0,
    classAdditionalFieldsSection: 0,
    classAdditionalFieldsMethodsSection: 0,
    classAdditionalRelationshipsSection: 0,
    classAdditionalRelationshipsMethodsSection: 0,
<%_ for (const field of fields) { _%>
    field<%- field.fieldNameCapitalized %>CustomDeclarationSection: 1,
    field<%- field.fieldNameCapitalized %>CustomMethodsSection: 1,
    field<%- field.fieldNameCapitalized %>AnnotationSection: 0,
  <%_ if (field.fieldWithContentType) { _%>
    field<%- field.fieldNameCapitalized %>ContentTypeAnnotationSection: 0,
  <%_ } _%>
    field<%- field.fieldNameCapitalized %>CustomSetSection: 1,
<%_ } _%>
<%_ for (const relationship of relationships) { _%>
    relationship<%- relationship.relationshipNameCapitalized %>AnnotationSection: 0,
    relationship<%- relationship.relationshipNameCapitalized %>CustomSetSection: 1,
    relationship<%- relationship.relationshipNameCapitalized %>CustomAddSection: 1,
    relationship<%- relationship.relationshipNameCapitalized %>CustomRemoveSection: 1,
<%_ } _%>
  });
-&>
<%_ const importLines = new Set();%>
package <%= entityAbsolutePackage %>.domain;

<&- fragments.staticImportSection() -&>
<&- fragments.importSection() -&>
<%_ if (relationshipsContainOtherSideIgnore) { _%>
<%_ importLines.add('import com.fasterxml.jackson.annotation.JsonIgnoreProperties;'); _%>
<%_ } _%>
<%_ if (!dtoMapstruct && (entityApiDescription || importApiModelProperty)) { _%>
<%_
    importLines.add('import io.swagger.v3.oas.annotations.media.Schema;');
    importLines.add('import cn.afterturn.easypoi.excel.annotation.Excel;');
_%>
<%_ } _%>

<%_ importLines.add('import java.io.Serializable;'); _%>
<%_ if (anyFieldIsBigDecimal) { _%>
<%_ importLines.add('import java.math.BigDecimal;'); _%>
<%_ } if (anyFieldIsInstant) { _%>
<%_ importLines.add('import java.time.Instant;'); _%>
<%_ } if (anyFieldIsLocalDate) { _%>
<%_ importLines.add('import java.time.LocalDate;'); _%>
<%_ } if (anyFieldIsZonedDateTime) { _%>
<%_ importLines.add('import java.time.ZonedDateTime;'); _%>
<%_ } if (anyFieldIsDuration) { _%>
<%_ importLines.add('import java.time.Duration;'); _%>
<%_ } _%>
<%_
    const allRelationships = relationships.filter(relationship => (!relationship.embedded || relationship.otherEntity.embedded || relationship.ownerSide));
    const importOwner = allRelationships.some(relationship => relationship.options?.source?.annotationOnSource?.includes('relateByIdEntity') && relationship.otherEntity && relationship.otherEntity.anyFieldIsOwner);
_%>
<%_ if ((importOwner || anyFieldIsOwner) && ormTool === 'hibernate') { _%>
<%_ importLines.add(`import ${packageName}.domain.Owner;`); _%>
<%_ importLines.add('import org.hibernate.annotations.AnyKeyJavaClass;'); _%>
<%_ importLines.add('import org.hibernate.annotations.AnyDiscriminator;'); _%>
<%_ } _%>
<%_ if (anyFieldIsOwner && ormTool === 'hibernate') { _%>
<%_ importLines.add('import org.hibernate.annotations.Any;'); _%>
<%_ importLines.add('import org.hibernate.annotations.AnyDiscriminatorValue;'); _%>
  <%_ (anyDiscriminatorValues || []).forEach(anyValue => { _%>
<%- anyValue.importStatement %>
  <%_ }); _%>
<%_ } _%>
<%_ if (anyFieldIsUUID || otherEntityPrimaryKeyTypesIncludesUUID || entityContainsCollectionField || extendData || !updatableEntity) { _%>
<%_ importLines.add('import java.util.*;'); _%>
<%_ }
Object.keys(uniqueEnums).forEach(function(element) { _%>

<%_ importLines.add(`import ${packageName}.domain.enumeration.${element};`); _%>
<%_ }); _%>
<%_ if (extendAbstractAuditingEntity && entityAbsolutePackage !== packageName) { _%>
<%_ importLines.add(`import ${packageName}.domain.AbstractAuditingEntity;`); _%>
<%_ } _%>
<%_ if (useLombok) { _%>
<%_ importLines.add('import lombok.*;'); _%>
<%_ } _%>
<%_ if (!updatableEntity && false) { _%>
<%_ importLines.add('import java.util.Objects;'); _%>
<%_ } _%>
<%_ for (const otherEntity of otherEntities.filter(otherEntity => otherEntity.entityAbsolutePackage !== entityAbsolutePackage)) { _%>
    <%_ if (applicationTypeMonolith || (applicationTypeMicroservice && (otherEntity.applications.includes('*') || otherEntity.applications.includes(baseName)))) { _%>
<%_ importLines.add(`import ${otherEntity.entityAbsoluteClass};`); _%>
    <%_ } _%>
<%_ } _%>
<%_ fields.filter(field => field.options?.ormFieldType?.includes('.'))
        .map(field => field.options.ormFieldType)
        .forEach(ormFieldType => {importLines.add(`import ${ormFieldType};`)});
_%>
<%_ importLines.forEach(importLine => { _%>
<%- importLine %>
<%_ }); _%>

<%_ if (entityJavadoc) { _%>
<%- entityJavadoc %>
<%_ } else { _%>
/**
 * A <%= persistClass %>.
 */
<%_ } _%>
<%_ if (!dtoMapstruct && entityApiDescription) { _%>
@Schema(description = "<%- entityApiDescription %>")
<%_ } _%>
<&- fragments.annotationSection() -&>
<%_ if (useLombok) { _%>
@Getter
@Setter
@RequiredArgsConstructor
<%_ } _%>
@SuppressWarnings("common-java:DuplicatedBlocks")
public class <%= persistClass %> <&- fragments.extendsSection() -&>implements Serializable<&- fragments.additionalInterfacesSection() -&> {

    private static final long serialVersionUID = 1L;

<%_ if (!embedded && primaryKey.composite) { _%>
    @Id
    private <%= primaryKey.type %> <%= primaryKey.name %>;

<%_ } _%>
<&- fragments.classStaticFieldsSection() -&>
<&- fragments.classFieldsSection() -&>
<%_ for (const field of fields.filter(field => !(field.options?.extendAbstractAuditingEntity) && !field.javaInherited && !field.transient && (embedded || !field.id || !primaryKey.composite))) { _%>
<&- fragments.field<%- field.fieldNameCapitalized %>CustomDeclarationSection() -&>
<&_ if (!fragments.field<%- field.fieldNameCapitalized %>CustomDeclarationSection()) { -&>
  <%_ if (field.fieldJavadoc) { _%>
<%- field.fieldJavadoc %>
  <%_ } _%>
  <%_ if (!dtoMapstruct && field.fieldApiDescription) { _%>
    @Schema(description = "<%- field.fieldApiDescription %>"<% if (field.fieldValidationRequired) { %>, required = true<% } %>)
    @Excel(name = "<%- field.fieldApiDescription %>")
  <%_ } _%>
<&- fragments.field<%- field.fieldNameCapitalized %>AnnotationSection() -&>
    <%_
        let fieldDefaultValue = '';
        if (field.options?.defaultValue) {
          if (field.javaFieldType === 'String') {
              fieldDefaultValue = ` = "${field.options.defaultValue}"`
          } else if (field.fieldIsEnum) {
              fieldDefaultValue = ` = ${field.javaFieldType}.${field.options.defaultValue}`
          } else if (field.javaFieldType === 'BigDecimal') {
              fieldDefaultValue = ` = new BigDecimal("${field.options.defaultValue}")`
          } else {
              fieldDefaultValue = ` = ${field.options.defaultValue}`
          }
        }
    _%>
    <%_ if (field.options?.ormFieldType) { _%>
    private <%= field.options?.ormFieldType.split('.').pop() %> <%= field.fieldName %><%- fieldDefaultValue %>;
    <%_ } else if (field.options?.customWrapType) { _%>
    <%_
        const fieldCustomTypeData = field.options?.customWrapType.split('_');
        let fieldCustomType = '';
        if (fieldCustomTypeData.length === 2) {
            if (fieldCustomTypeData[0].toLowerCase() === 'array') {
                fieldCustomType = fieldCustomTypeData[1] + '[]';
            }
            if (fieldCustomTypeData[0].toLowerCase() === 'list') {
                fieldCustomType = `List<${fieldCustomTypeData[1]}>`;
                if (!fieldDefaultValue) {
                    fieldDefaultValue = 'new ArrayList<>()';
                }
            }
            if (fieldCustomTypeData[0].toLowerCase() === 'map') {
                fieldCustomType = `Map<${fieldCustomTypeData[1]}>`;
                if (!fieldDefaultValue) {
                    fieldDefaultValue = 'new HashMap<>()';
                }
            }
        } else {
            fieldCustomType = field.options?.customWrapType;
        }
    _%>
    private <%= fieldCustomType %> <%= field.fieldName %><%- fieldDefaultValue %>;
    <%_ } else { _%>
    private <%= field.javaFieldType %> <%= field.fieldName %><%- fieldDefaultValue %>;
    <%_ } _%>

  <%_ if (field.fieldWithContentType) { _%>
<&- fragments.field<%- field.fieldNameCapitalized %>ContentTypeAnnotationSection() -&>
    private String <%= field.fieldName %>ContentType;

  <%_ } _%>
<&_ } -&>
<%_ } _%>
<&- fragments.classAdditionalFieldsSection() -&>
<%_ if (extendData && ormTool === 'mybatis') { _%>

    @BindExtData
    @TableField(exist = false)
    private Map<String, Object> extData = new HashMap<>();
<%_ } _%>
<%_ if (anyFieldIsOwner && ormTool === 'hibernate') { _%>
    @Any(fetch = FetchType.LAZY)
    @Column(name="owner_entity_name")
    @JoinColumn(name = "owner_entity_id")
    <%_ (anyDiscriminatorValues || []).filter(anyValue => anyValue.anyEntityName === persistClass).forEach(anyValue => { _%>
    @AnyDiscriminatorValue(discriminator="<%= anyValue.discriminator %>", entity=<%= anyValue.entity%>)
    <%_ }); _%>
    @AnyDiscriminator(DiscriminatorType.STRING)
    @AnyKeyJavaClass(Long.class)
    private Owner owner;
<%_ } _%>

<%_
// An embedded entity should not reference entities that embed it, unless the other entities are also embedded
for (relationship of relationships.filter(relationship => (!relationship.embedded || relationship.otherEntity.embedded || relationship.ownerSide) && !(relationship.relationshipRightSide && relationship.bcConfig.leftUnidirectional && (relationship.relationshipManyToOne && relationship.bcConfig.leftRelateByIdEntity || relationship.relationshipManyToMany)))) { // modify by begcode
  if (typeof relationship.fieldApiDescription) { _%>
<%- relationship.relationshipJavadoc %>
    <%_ if (!dtoMapstruct && relationship.relationshipyApiDescription) { _%>
    @Schema(description = "<%- relationship.relationshipyApiDescription %>")
    <%_ } _%>
  <%_ } _%>
<&- fragments.relationship<%- relationship.relationshipNameCapitalized %>AnnotationSection() -&>
  <%_ if (relationship.relationshipOneToMany) { _%>
    <%_ if (relationship.ignoreOtherSideProperty) { _%>
    @JsonIgnoreProperties(value = {
        <%_ relationship.otherEntity.relationships.forEach(otherRelationship => { _%>
        "<%= otherRelationship.relationshipReferenceField %>",
        <%_ }); _%>
    }, allowSetters = true)
    <%_ } _%>
    <%_ if (ormTool === 'hibernate') { _%>
    private Set<<%= relationship.otherEntity.persistClass %>> <%= relationship.relationshipFieldNamePlural %> = new LinkedHashSet<>();
    <%_ } else if (relationship.source?.annotationOnSource?.includes('unidirectional') && relationship.source?.annotationOnSource?.includes('countByPrimaryKey')) { _%>
    private Long <%= relationship.relationshipName %>Count;
    <%_ } else { _%>
    private List<<%= relationship.otherEntity.persistClass %>> <%= relationship.relationshipFieldNamePlural %> = new ArrayList<>();
    <%_ } _%>

  <%_ } else if (relationship.relationshipManyToOne || (relationship.relationshipOneToOne && relationship.ownerSide)) { _%>
    <%_ if (relationship.ignoreOtherSideProperty) { _%>
    @JsonIgnoreProperties(value = {
      <%_ relationship.otherEntity.relationships.forEach(otherRelationship => { _%>
        "<%= otherRelationship.relationshipReferenceField %>",
      <%_ }); _%>
    }, allowSetters = true)
    <%_ } _%>
    <%_ if (ormTool === 'hibernate') { _%>
    private <%= relationship.otherEntity.persistClass %> <%= relationship.relationshipFieldName %>;
    <%_ } else { _%>
        <%_
        if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== relationship.otherEntity.moduleName) ||
                (applicationTypeMicroservice && !(relationship.otherEntity.applications.includes('*') || relationship.otherEntity.applications.includes(baseName)))
        ) {
        _%>
    private String <%= relationship.relationshipName %><%= this._.upperFirst(relationship.otherEntityField) %>;
        <%_ } else { _%>
    private <%= relationship.otherEntity.persistClass %> <%= relationship.relationshipFieldName %>;
          <%_ if (!relationship.id) { _%>

    @TableField(value = "<%= relationship.columnName %>_<%= relationship.otherEntity.primaryKey.name %>")
    private <%= relationship.otherEntity.primaryKey.type %> <%= relationship.relationshipFieldName %><%= relationship.otherEntity.primaryKey.nameCapitalized %>;
          <%_ } _%>
        <%_ } _%>
    <%_ } _%>

  <%_ } else if (relationship.relationshipManyToMany) { _%>
    <%_ if (relationship.ignoreOtherSideProperty) { _%>
    @JsonIgnoreProperties(value = {
      <%_ relationship.otherEntity.relationships.forEach(otherRelationship => { _%>
        "<%= otherRelationship.relationshipReferenceField %>",
      <%_ }); _%>
    }, allowSetters = true)
    <%_ } _%>
    <%_ if (ormTool === 'hibernate') { _%>
    private Set<<%= relationship.otherEntity.persistClass %>> <%= relationship.relationshipFieldNamePlural %> = new LinkedHashSet<>();
    <%_ } else { _%>
        <%# 不在同一个模块或在不同的微服务 -%>
        <%_ if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== relationship.otherEntity.moduleName) ||
                (applicationTypeMicroservice && !(relationship.otherEntity.applications.includes('*') || relationship.otherEntity.applications.includes(baseName)))
        ) { _%>
    private List<<%= relationship.otherEntity.primaryKey.fields[0].fieldType %>> <%= relationship.relationshipFieldNamePlural %> = new ArrayList<>();
        <%_ } else { _%>
    private List<<%= relationship.otherEntity.persistClass %>> <%= relationship.relationshipFieldNamePlural %> = new ArrayList<>();
        <%_ } _%>
    <%_ } _%>

  <%_ } else { _%>
    private <%= relationship.otherEntity.persistClass %> <%= relationship.relationshipFieldName %>;
  <%_ } _%>

<%_ }; _%>
<&- fragments.classAdditionalRelationshipsSection() -&>
    // jhipster-needle-entity-add-field - JHipster will add fields here
<%_ for (const field of fields.filter(field => !field.options?.extendAbstractAuditingEntity && !field.transient && (embedded || !field.id || !primaryKey.composite))) { _%>
<&- fragments.field<%- field.fieldNameCapitalized %>CustomMethodsSection() -&>
<&_ if (!fragments.field<%- field.fieldNameCapitalized %>CustomMethodsSection()) { -&>
<%_ if (!useLombok) { _%>

    public <%= field.javaFieldType %> get<%= field.fieldInJavaBeanMethod %>() {
        return this.<%= field.fieldName %>;
    }
<%_ } _%>
  <%_ if (fluentMethods) { _%>

    <%_ if (field.options?.ormFieldType) { _%>
    public <%= persistClass %> <%= field.fieldName %>(<%= field.options?.ormFieldType.split('.').pop() %> <%= field.fieldName %>) {
    <%_ } else if (field.options?.customWrapType) { _%>
    <%_
        const fieldCustomTypeData = field.options?.customWrapType.split('_');
        let fieldCustomType = '';
        if (fieldCustomTypeData.length === 2) {
            if (fieldCustomTypeData[0].toLowerCase() === 'array') {
                fieldCustomType = fieldCustomTypeData[1] + '[]';
            }
            if (fieldCustomTypeData[0].toLowerCase() === 'list') {
                fieldCustomType = `List<${fieldCustomTypeData[1]}>`;
                if (!fieldDefaultValue) {
                    fieldDefaultValue = 'new ArrayList<>()';
                }
            }
            if (fieldCustomTypeData[0].toLowerCase() === 'map') {
                fieldCustomType = `Map<${fieldCustomTypeData[1]}>`;
                if (!fieldDefaultValue) {
                    fieldDefaultValue = 'new HashMap<>()';
                }
            }
        } else {
            fieldCustomType = field.options?.customWrapType;
        }
    _%>
    public <%= persistClass %> <%= field.fieldName %>(<%= fieldCustomType %> <%= field.fieldName %>) {
    <%_ } else { _%>
    public <%= persistClass %> <%= field.fieldName %>(<%= field.javaFieldType %> <%= field.fieldName %>) {
    <%_ } _%>
        <%_ if (ormTool === 'hibernate') { _%>
        this.set<%= field.fieldInJavaBeanMethod %>(<%= field.fieldName %>);
        <%_ } else { _%>
        this.<%= field.fieldName %> = <%= field.fieldName %>;
        <%_ } _%>
        return this;
    }
  <%_ } _%>

    <%_ if (!useLombok) { _%>
  <&- fragments.field<%- field.fieldNameCapitalized %>CustomSetSection() -&>
  <&_ if (!fragments.field<%- field.fieldNameCapitalized %>CustomSetSection()) { -&>
    public void set<%= field.fieldInJavaBeanMethod %>(<%= field.javaFieldType %> <%= field.fieldName %>) {
        this.<%= field.fieldName %> = <%= field.fieldName %>;
    }
  <&_ } -&>
    <%_ } _%>
  <%_ if (field.fieldWithContentType) { _%>
    <%_ if (!useLombok) { _%>

    public String get<%= field.fieldInJavaBeanMethod %>ContentType() {
        return this.<%= field.fieldName %>ContentType;
    }
    <%_ } _%>
    <%_ if (fluentMethods) { _%>

    public <%= persistClass %> <%= field.fieldName %>ContentType(String <%= field.fieldName %>ContentType) {
        this.<%= field.fieldName %>ContentType = <%= field.fieldName %>ContentType;
        return this;
    }
    <%_ } _%>
    <%_ if (!useLombok) { _%>

    public void set<%= field.fieldInJavaBeanMethod %>ContentType(String <%= field.fieldName %>ContentType) {
        this.<%= field.fieldName %>ContentType = <%= field.fieldName %>ContentType;
    }
    <%_ } _%>
  <%_ } _%>
<&_ } -&>
<%_ } _%>
<%_ if (extendAbstractAuditingEntity && primaryKey.name.toLowerCase() !== 'id') { _%>
    public String getId() {
        return <%= primaryKey.name %><%= primaryKey.type !== 'String' ? '.toString()' : ''%>;
    }
<%_ } _%>

<&- fragments.classAdditionalFieldsMethodsSection('\n\n') -&>
<%_
for (const relationship of relationships) {
  // An embedded entity should not reference entities that embed it, unless the other entities are also embedded
  if (embedded && !relationship.otherEntity.embedded && !relationship.ownerSide) {
    continue;
  }
  // add by begcode start
  if (relationship.relationshipRightSide && relationship.bcConfig.leftUnidirectional && (relationship.relationshipManyToOne && relationship.bcConfig.leftRelateByIdEntity || relationship.relationshipManyToMany)) {
    continue;
  }
  // add by begcode end
_%>
  <%_ if (!useLombok) { _%>
  <%_ if (relationship.collection) { _%>
    public <%= ormTool === 'hibernate' ? 'Set' : 'List'%><<%= relationship.otherEntity.persistClass %>> get<%= relationship.relationshipNameCapitalizedPlural %>() {
        return this.<%= relationship.relationshipFieldNamePlural %>;
    }

  <%_ } else { _%>
    public <%= relationship.otherEntity.persistClass %> get<%= relationship.relationshipNameCapitalized %>() {
        return this.<%= relationship.relationshipFieldName %>;
    }

  <%_ } _%>
  <%_ } _%>
<&- fragments.relationship<%- relationship.relationshipNameCapitalized %>CustomSetSection() -&>
<&_ if (!fragments.relationship<%- relationship.relationshipNameCapitalized %>CustomSetSection()) { -&>
  <%_ if (!useLombok || ormTool === 'hibernate') { _%>
  <%_ if (relationship.collection) { _%>
    public void set<%= relationship.relationshipNameCapitalizedPlural %>(<%= ormTool === 'hibernate' ? 'Set' : 'List'%><<%= relationship.otherEntity.persistClass %>> <%= relationship.otherEntityNamePlural %>) {
    <%_ if (relationship.otherRelationship && relationship.relationshipUpdateBackReference && ormTool === 'hibernate' && relationship.otherRelationship.reference) { _%>
        if (this.<%= relationship.relationshipFieldNamePlural %> != null) {
      <%_ if (relationship.otherRelationship.collection) { _%>
            this.<%= relationship.relationshipFieldNamePlural %>.forEach(i -> i.remove<%= relationship.otherRelationship.relationshipNameCapitalized %>(this));
      <%_ } else { _%>
        <%_ if (relationship?.bcConfig?.leftRelateByIdEntity) { _%>
            this.<%= relationship.relationshipFieldNamePlural %>.forEach(i -> i.setOwner(null));
        <%_ } else { _%>
            this.<%= relationship.relationshipFieldNamePlural %>.forEach(i -> i.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(null));
        <%_ } _%>
      <%_ } _%>
        }
        if (<%= relationship.otherEntityNamePlural %> != null) {
      <%_ if (relationship.otherRelationship.collection) { _%>
            <%= relationship.otherEntityNamePlural %>.forEach(i -> i.add<%= relationship.otherRelationship.relationshipNameCapitalized %>(this));
      <%_ } else { _%>
        <%_ if (relationship?.bcConfig?.leftRelateByIdEntity) { _%>
            <%= relationship.otherEntityNamePlural %>.forEach(i -> i.setOwner(this));
        <%_ } else { _%>
            <%= relationship.otherEntityNamePlural %>.forEach(i -> i.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(this));
        <%_ } _%>
      <%_ } _%>
        }
    <%_ } _%>
        this.<%= relationship.relationshipFieldNamePlural %> = <%= relationship.otherEntityNamePlural %>;
    }
  <%_ } else { _%>
    public void set<%= relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistClass %> <%= relationship.otherEntityName %>) {
    <%_ if (relationship.otherRelationship && relationship.relationshipUpdateBackReference && ormTool === 'hibernate' && relationship.otherRelationship.reference) { _%>
        if (this.<%= relationship.relationshipFieldName %> != null) {
      <%_ if (relationship.otherRelationship.collection) { _%>
            this.<%= relationship.relationshipFieldName %>.remove<%= relationship.otherRelationship.relationshipNameCapitalized %>(this);
      <%_ } else { _%>
        <%_ if (relationship?.bcConfig?.leftRelateByIdEntity) { _%>
            this.<%= relationship.relationshipFieldName %>.setOwner(null);
        <%_ } else { _%>
            this.<%= relationship.relationshipFieldName %>.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(null);
        <%_ } _%>
      <%_ } _%>
        }
        if (<%= relationship.otherEntityName %> != null) {
      <%_ if (relationship.otherRelationship.collection) { _%>
            <%= relationship.otherEntityName %>.add<%= relationship.otherRelationship.relationshipNameCapitalized %>(this);
      <%_ } else { _%>
        <%_ if (relationship?.bcConfig?.leftRelateByIdEntity) { _%>
            <%= relationship.otherEntityName %>.setOwner(this);
        <%_ } else { _%>
            <%= relationship.otherEntityName %>.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(this);
        <%_ } _%>
      <%_ } _%>
        }
    <%_ } _%>
        this.<%= relationship.relationshipFieldName %> = <%= relationship.otherEntityName %>;
    }
  <%_ } _%>
  <%_ } _%>
<&_ } -&>
    <%_ if (fluentMethods) { _%>
      <%_ if (relationship.collection) { _%>
        <%_ if (relationship.source?.annotationOnSource?.includes('unidirectional') && relationship.source?.annotationOnSource?.includes('countByPrimaryKey')) { _%>
    public <%= persistClass %> <%= relationship.relationshipFieldNamePlural %>Count(Long <%= relationship.relationshipFieldNamePlural %>Count) {
        this.<%= relationship.relationshipFieldNamePlural %>Count = <%= relationship.relationshipFieldNamePlural %>Count;
        return this;
    }
        <%_ } else { _%>
    public <%= persistClass %> <%= relationship.relationshipFieldNamePlural %>(<%= ormTool === 'hibernate' ? 'Set' : 'List'%><<%= relationship.otherEntity.persistClass %>> <%= relationship.otherEntityNamePlural %>) {
        <%_ if (ormTool === 'hibernate') { _%>
        this.set<%= relationship.relationshipNameCapitalizedPlural %>(<%= relationship.otherEntityNamePlural %>);
        <%_ } else { _%>
        this.<%= relationship.relationshipFieldNamePlural %> = <%= relationship.otherEntityNamePlural %>;
        <%_ } _%>
        return this;
    }
        <%_ } _%>
        <%_ if (ormTool === 'hibernate') { _%>
<&- fragments.relationship<%- relationship.relationshipNameCapitalized %>CustomAddSection() -&>
<&_ if (!fragments.relationship<%- relationship.relationshipNameCapitalized %>CustomAddSection()) { -&>
    public <%= persistClass %> add<%= relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistClass %> <%= relationship.otherEntityName %>) {
        this.<%= relationship.relationshipFieldNamePlural %>.add(<%= relationship.otherEntityName %>);
      <%_ if (relationship.otherRelationship && relationship.relationshipUpdateBackReference) { _%>
        <%_ if (relationship.otherRelationship.collection) { _%>
            <%_ if (relationship.otherRelationship.relationshipNamePlural && !relationship.otherEntityUser && relationship.otherRelationship.collection) { _%>
        <%= relationship.otherEntityName %>.get<%= relationship.otherRelationship.relationshipNameCapitalizedPlural %>().add(this);
            <%_ } _%>
        <%_ } else { _%>
            <%_ if (relationship?.bcConfig?.leftRelateByIdEntity) { _%>
        <%= relationship.otherEntityName %>.setOwner(this);
            <%_ } else if (relationship.otherRelationship.relationshipNameCapitalized) { _%>
        <%= relationship.otherEntityName %>.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(this);
            <%_ } _%>
        <%_ } _%>
      <%_ } _%>
        return this;
    }
<&_ } -&>

<&- fragments.relationship<%- relationship.relationshipNameCapitalized %>CustomRemoveSection() -&>
<&_ if (!fragments.relationship<%- relationship.relationshipNameCapitalized %>CustomRemoveSection()) { -&>
    public <%= persistClass %> remove<%= relationship.relationshipNameCapitalized %>(<%= relationship.otherEntity.persistClass %> <%= relationship.otherEntityName %>) {
        this.<%= relationship.relationshipFieldNamePlural %>.remove(<%= relationship.otherEntityName %>);
      <%_ if (relationship.otherRelationship && relationship.relationshipUpdateBackReference) { _%>
        <%_ if (relationship.otherRelationship.collection) { _%>
            <%_ if (!relationship.otherEntityUser && relationship.otherRelationship.collection) { _%>
        <%= relationship.otherEntityName %>.get<%= relationship.otherRelationship.relationshipNameCapitalizedPlural %>().remove(this);
            <%_ } _%>
        <%_ } else { _%>
            <%_ if (relationship?.bcConfig?.leftRelateByIdEntity) { _%>
        <%= relationship.otherEntityName %>.setOwner(null);
            <%_ } else if (relationship.otherRelationship.relationshipNameCapitalized) { _%>
        <%= relationship.otherEntityName %>.set<%= relationship.otherRelationship.relationshipNameCapitalized %>(null);
            <%_ } _%>
        <%_ } _%>
      <%_ } _%>
        return this;
    }
<&_ } -&>
        <%_ } _%>
      <%_ } else { _%>

    public <%= persistClass %> <%= relationship.relationshipFieldName %>(<%= relationship.otherEntity.persistClass %> <%= relationship.otherEntityName %>) {
        <%_ if (ormTool === 'hibernate') { _%>
        this.set<%= relationship.relationshipNameCapitalized %>(<%= relationship.otherEntityName %>);
        <%_ } else { _%>
        this.<%= relationship.relationshipFieldName %> = <%= relationship.otherEntityName %>;
        <%_ } _%>
        return this;
    }
        <%_ if (ormTool === 'mybatis' && !(relationship.otherRelationship?.id || relationship.id) && (!relationship.relationshipOneToOne || relationship.relationshipOneToOne && relationship.ownerSide)) { _%>

    public <%= persistClass %> <%= relationship.relationshipFieldName %><%= relationship.otherEntity.primaryKey.nameCapitalized %>(<%= relationship.otherEntity.primaryKey.type %> <%= relationship.relationshipFieldName %><%= relationship.otherEntity.primaryKey.nameCapitalized %>) {
        this.<%= relationship.relationshipFieldName %><%= relationship.otherEntity.primaryKey.nameCapitalized %> = <%= relationship.relationshipFieldName %><%= relationship.otherEntity.primaryKey.nameCapitalized %>;
        return this;
    }
        <%_ } _%>
      <%_ } _%>
    <%_ } _%>

<%_ } _%>
<%_ if (extendData) { _%>
    <%_ if (!useLombok) { _%>

    public void setExtData(Map<String, Object> extData) {
        this.extData = extData;
    }
    <%_ } _%>

    public <%= persistClass %> extData(Map<String, Object> extData) {
        this.extData = extData;
        return this;
    }
    <%_ if (!useLombok) { _%>
    public Map<String, Object> getExtData() {
        return extData;
    }
    <%_ } _%>

    @Override
    public String getOwnerEntityName() {
        return this.getClass().getSimpleName();
    }

<%_ } _%>
<&- fragments.classAdditionalRelationshipsMethodsSection() -&>
    // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof <%= persistClass %>)) {
            return false;
        }
<%_ if (!embedded) { _%>
        return get<%= primaryKey.nameCapitalized %>() != null && get<%= primaryKey.nameCapitalized %>().equals(((<%= persistClass %>) o).get<%= primaryKey.nameCapitalized %>());
<%_ } else { _%>
        return false;
<%_ } _%>
    }

    @Override
    public int hashCode() {
<%_ if (updatableEntity) { _%>
        // see https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
        return getClass().hashCode();
<%_ } else { _%>
        return Objects.hashCode(get<%= primaryKey.nameCapitalized %>());
<%_ } _%>
    }

    // prettier-ignore
    @Override
    public String toString() {
        return "<%= persistClass %>{" +
<%_ if (!embedded) { _%>
            "<%= primaryKey.name %>=" + get<%= primaryKey.nameCapitalized %>() +
<%_ } _%>
<%_ for (const field of fields.filter(field => !field.id && !field.transient)) {
  const isNumeric = field.fieldTypeNumeric; _%>
            ", <%= field.fieldName %>=<% if (! isNumeric) { %>'<% } %>" + get<%= field.fieldInJavaBeanMethod %>() <% if (! isNumeric) { %>+ "'" <% } %>+
  <%_ if (field.fieldWithContentType) { _%>
            ", <%= field.fieldName %>ContentType='" + get<%= field.fieldInJavaBeanMethod %>ContentType() + "'" +
  <%_ } _%>
<%_ } _%>
<%_ for (const relationship of relationships) { _%>
  <%_ if (relationship.otherEntity.embedded) {
    if (relationship.relationshipManyToOne) { _%>
    <%_
    if (((applicationTypeMonolith || applicationTypeMicroservice) && multiModule && moduleName && moduleName !== relationship.otherEntity.moduleName) ||
            (applicationTypeMicroservice && !(relationship.otherEntity.applications.includes('*') || relationship.otherEntity.applications.includes(baseName)))
    ) {
    _%>
            ", <%= relationship.relationshipFieldName %><%= relationship.otherEntity.primaryKey.nameCapitalized %>='" + get<%= relationship.relationshipNameCapitalized %><%= relationship.otherEntity.primaryKey.nameCapitalized %>() + "'" +
            ", <%= relationship.relationshipFieldName %><%= this._.upperFirst(relationship.otherEntityField) %>='" + get<%= relationship.relationshipNameCapitalized %><%= _.upperFirst(relationship.otherEntityField) %>() + "'" +
    <%_ } else { _%>
            ", <%= relationship.relationshipFieldName %>='" + get<%= relationship.relationshipNameCapitalized %>() + "'" +
    <%_ } _%>
    <%_ } else if (relationship.relationshipOneToOne && relationship.ownerSide) { _%>
            ", <%= relationship.relationshipFieldName %>='" + get<%= relationship.relationshipNameCapitalized %>() + "'" +
    <%_ } _%>
  <%_ } _%>
<%_ } _%>
            "}";
    }
}
