Both key columns and PK key

How to create a map property that has both a key column and a map key as part of a PK part class?

Like this:

<class entity-name="Person"  >
    <id name="id"/>
    <property name="birthDate" type="date"/>
    <map name="names">
        <key column="personId"/>
        <map-key type="string" column="code"/>
        <one-to-many class="PersonName" />
    </map>
</class>


<class entity-name="PersonName">
    <composite-id>
        <key-many-to-one name="personId" class="Person"/>
        <key-property name="code" type="string" length="32"/>
    </composite-id>
    <property name="lastName" type="string" length="64" index="nameSearch"/>
    <property name="firstName" type="string" length="64" index="nameSearch"/>
    <property name="middleName" type="string" length="64" index="nameSearch"/>
    <property name="isActual" type="boolean"/>
</class>

I need to avoid creating a surrogate key in the PersonName class, which requires special handling. Json, shown below, should be automatically saved in the database, insert and update detailed records, when necessary, based on the PersonId code.

Naturally, the "code" property identifies the string along with the identifier of the person.

I tried different combinations of "reverse", "non-zero", etc. I admit that I do not fully understand how this works. I get various error messages, for example:

: null "" NOT NULL
: Erroreous row (null, null, lastname, ffffirst, null, null).

org.hibernate.MappingException: : PersonName: person ( insert = "false" update = "false" )

, json:

{
    "birthDate": "33-44-55",
    "names": {
        "mainName": {
            "lastName": "lastname",
            "firstName": "ffffirst"
        },
        "maidenName": {
            "lastName": "lastname1",
            "firstName": "ffffirst2"
        },
        "old": {
            "lastName": "lastname3",
            "firstName": "ffffirst4"
        }

    }
}

UPD ( ) - , json. json . "id", , , , . ( "" ) "id". "personId" "code".

** 2 ** json, , . xml, . , json.

, !

, , :

CREATE TABLE personname
(
  personid character varying(32) NOT NULL,
  code character varying(32) NOT NULL,
  lastname character varying(64),
  firstname character varying(64),
  middlename character varying(64),
  isactual boolean,
  CONSTRAINT personname_pkey PRIMARY KEY (personid, code),
  CONSTRAINT fk_1skg5frawyftx8co9uawhc3r8 FOREIGN KEY (personid)
      REFERENCES person (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
+4
2

. mongodb ORM.

+1

, :

Key (, Person $Key, ) id:

public static class Key {
    private Long personId;
    private String code;

    public Key(Long personId, String code) {
        this.personId = personId;
        this.code = code;
    }

    public Long getPersonId() {
        return personId;
    }

    public String getCode() {
        return code;
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Key)) {
            return false;
        }
        Key k = (Key)obj;
        return personId.equals(k.personId) && code.equals(k.code);
    }

    @Override
    public int hashCode() {
        return 37*personId.hashCode() + code.hashCode();
    }
}

:

private Map<Key,PersonName> names = new HashMap<Key,PersonName>();

Hibernate:

<class entity-name="Person"  >
     <id name="id" column="id">
        <generator class="native"/>
    </id>
    <property name="birthDate" type="date"/>
    <map name="names" inverse="true">
        <composite-map-key class="Person$Key">
            <key-property name="personId"/>
            <key-property name="code"/>
        </composite-map-key>
        <one-to-many class="PersonName" />
    </map>
</class>

<class entity-name="PersonName">
    <composite-id name="id" class="Person">
        <key-property name="personId"/>
        <key-property name="code"/>
    </composite-id>
    <many-to-one name="person" class="Person"
        insert="false" update="false">
        <column name="personId"/>
        <column name="code"/>
    </many-to-one>
    <property name="lastName" type="string" length="64" index="nameSearch"/>
    <property name="firstName" type="string" length="64" index="nameSearch"/>
    <property name="middleName" type="string" length="64" index="nameSearch"/>
    <property name="isActual" type="boolean"/>
</class>
+2

Source: https://habr.com/ru/post/1543106/


All Articles