Basic EmbeddedId

package org.example.demo.hibernate;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Test {
    public static void main(String[] args) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
        EntityManager em = factory.createEntityManager();

        PK pk = new PK();
        pk.system = "Windows";
        pk.username = "me";

        Login login = new Login();
        login.pk = pk;

        em.getTransaction().begin();
        em.persist(login);
        em.getTransaction().commit();

        em.clear();

        em.find(Login.class, login.pk);

        em.close();
        factory.close();
    }

    @Entity
    public static class Login {
        @EmbeddedId
        private PK pk;
        Date time = new Date();
    }

    @SuppressWarnings("serial")
    @Embeddable
    public static class PK implements Serializable {
        String system;
        String username;
    }
}

生成 SQL

drop table if exists Test$Login cascade

create table Test$Login (system varchar(255) not null, username varchar(255) not null, time timestamp, primary key (system, username))
insert into Test$Login (time, system, username) values ('2017-07-01 17:13:48', 'Windows', 'me')

select test_login0_.system as system1_0_0_, test_login0_.username as username2_0_0_, test_login0_.time as time3_0_0_ from Test$Login test_login0_ where test_login0_.system='Windows' and test_login0_.username='me'

EmbeddedId with ManyToOne

package org.example.demo.hibernate;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Persistence;

public class Test {
    public static void main(String[] args) {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
        EntityManager em = factory.createEntityManager();

        PK pk = new PK();
        pk.system = new System();
        pk.system.name = "Windows";
        pk.username = "me";

        Login login = new Login();
        login.pk = pk;

        em.getTransaction().begin();
        em.persist(pk.system);
        em.persist(login);
        em.getTransaction().commit();

        em.clear();

        em.find(Login.class, login.pk);

        em.close();
        factory.close();
    }

    @Entity
    public static class Login {
        @EmbeddedId
        private PK pk;
        Date time = new Date();
    }

    @SuppressWarnings("serial")
    @Embeddable
    public static class PK implements Serializable {
        @ManyToOne
        private System system;
        String username;
    }

    @Entity
    public static class System {
        @Id
        @GeneratedValue
        private Long id;
        String name;
    }
}

生成 SQL

alter table Test$Login drop constraint FKmla7of81bjip6sejltfgk1ias
drop table if exists Test$Login cascade
drop table if exists Test$System cascade
drop sequence if exists hibernate_sequence

create sequence hibernate_sequence start 1 increment 1
create table Test$Login (username varchar(255) not null, time timestamp, system_id int8 not null, primary key (system_id, username))
create table Test$System (id int8 not null, name varchar(255), primary key (id))
alter table Test$Login add constraint FKmla7of81bjip6sejltfgk1ias foreign key (system_id) references Test$System
select nextval ('hibernate_sequence')
insert into Test$System (name, id) values ('Windows', 1)
insert into Test$Login (time, system_id, username) values ('2017-07-01 17:18:53', 1, 'me')

select test_login0_.system_id as system_i3_0_0_, test_login0_.username as username1_0_0_, test_login0_.time as time2_0_0_ from Test$Login test_login0_ where test_login0_.system_id=1 and test_login0_.username='me'

Hibernate supports directly modeling the ManyToOne in the PK class, whether EmbeddedId or IdClass. However that is not portably supported by the JPA specification. In JPA terms one would use "derived identifiers"

results matching ""

    No results matching ""