EntityManager.contains
src/main/resources/META-INF/persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="test">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:p6spy:postgresql://localhost:5432/test" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
</properties>
</persistence-unit>
</persistence>
package org.example.demo.hibernate;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Persistence;
public class Test {
public static void main(String[] args) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Person person = new Person();
person.name = "john doe";
em.persist(person);
em.getTransaction().commit();
// true
System.out.println(em.contains(person));
em.clear();
// false
System.out.println(em.contains(person));
em.close();
factory.close();
}
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
Integer id;
String name;
}
}
Session.contains
package org.example.demo.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
public class Test {
public static void main(String[] args) {
ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.applySetting("hibernate.connection.url", "jdbc:p6spy:postgresql://localhost:5432/test")
.applySetting("hibernate.connection.username", "postgres")
.applySetting("hibernate.connection.password", "postgres")
.applySetting("hibernate.hbm2ddl.auto", "create").build();
Metadata metadata = new MetadataSources(standardRegistry).addAnnotatedClass(Person.class).getMetadataBuilder()
.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE).build();
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
Session session = sessionFactory.openSession();
session.beginTransaction();
Person person = new Person();
person.name = "john doe";
session.persist(person);
session.getTransaction().commit();
// true
System.out.println(session.contains(person));
session.clear();
// false
System.out.println(session.contains(person));
session.close();
sessionFactory.close();
}
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
Integer id;
String name;
}
}
PersistenceUnitUtil.isLoaded
src/main/resources/META-INF/persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="test">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:p6spy:postgresql://localhost:5432/test" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
</properties>
</persistence-unit>
</persistence>
package org.example.demo.hibernate;
import java.util.ArrayList;
import java.util.List;
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.OneToMany;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnitUtil;
public class Test {
public static void main(String[] args) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Person person = new Person();
person.name = "john doe";
em.persist(person);
em.getTransaction().commit();
em.clear();
Person p = em.find(Person.class, person.id);
PersistenceUnitUtil persistenceUnitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil();
// true
System.out.println(persistenceUnitUtil.isLoaded(p));
// true
System.out.println(persistenceUnitUtil.isLoaded(p, "name"));
// false
System.out.println(persistenceUnitUtil.isLoaded(p.books));
em.close();
factory.close();
}
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
Integer id;
String name;
@OneToMany(mappedBy = "author")
List<Book> books = new ArrayList<>();
}
@Entity(name = "Book")
public static class Book {
@Id
@GeneratedValue
Integer id;
String title;
@ManyToOne
Person author;
}
}
生成的 SQL
alter table Book drop constraint FKrxrgiajod1le3gii8whx2doie
drop table if exists Book cascade
drop table if exists Person cascade
drop sequence if exists hibernate_sequence
create sequence hibernate_sequence start 1 increment 1
create table Book (id int4 not null, title varchar(255), author_id int4, primary key (id))
create table Person (id int4 not null, name varchar(255), primary key (id))
alter table Book add constraint FKrxrgiajod1le3gii8whx2doie foreign key (author_id) references Person
select nextval ('hibernate_sequence')
insert into Person (name, id) values ('john doe', 1)
select test_perso0_.id as id1_1_0_, test_perso0_.name as name2_1_0_ from Person test_perso0_ where test_perso0_.id=1
Hibernate.isInitialized/isPropertyInitialized
package org.example.demo.hibernate;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
public class Test {
public static void main(String[] args) {
ServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.applySetting("hibernate.connection.url", "jdbc:p6spy:postgresql://localhost:5432/test")
.applySetting("hibernate.connection.username", "postgres")
.applySetting("hibernate.connection.password", "postgres")
.applySetting("hibernate.hbm2ddl.auto", "create").build();
Metadata metadata = new MetadataSources(standardRegistry).addAnnotatedClass(Person.class)
.addAnnotatedClass(Book.class).getMetadataBuilder()
.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE).build();
SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
Session session = sessionFactory.openSession();
session.beginTransaction();
Person person = new Person();
person.name = "john doe";
session.persist(person);
session.getTransaction().commit();
session.clear();
Person p = session.get(Person.class, person.id);
// true
System.out.println(Hibernate.isInitialized(p));
// true
System.out.println(Hibernate.isPropertyInitialized(p, "name"));
// false
System.out.println(Hibernate.isInitialized(p.books));
session.close();
sessionFactory.close();
}
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
Integer id;
String name;
@OneToMany(mappedBy = "author")
List<Book> books = new ArrayList<>();
}
@Entity(name = "Book")
public static class Book {
@Id
@GeneratedValue
Integer id;
String title;
@ManyToOne
Person author;
}
}
生成的 SQL
alter table Book drop constraint FKrxrgiajod1le3gii8whx2doie
drop table if exists Book cascade
drop table if exists Person cascade
drop sequence if exists hibernate_sequence
create sequence hibernate_sequence start 1 increment 1
create table Book (id int4 not null, title varchar(255), author_id int4, primary key (id))
create table Person (id int4 not null, name varchar(255), primary key (id))
alter table Book add constraint FKrxrgiajod1le3gii8whx2doie foreign key (author_id) references Person
select nextval ('hibernate_sequence')
insert into Person (name, id) values ('john doe', 1)
select test_perso0_.id as id1_1_0_, test_perso0_.name as name2_1_0_ from Person test_perso0_ where test_perso0_.id=1
PersistenceUtil.isLoaded
src/main/resources/META-INF/persistence.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="test">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:p6spy:postgresql://localhost:5432/test" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
</properties>
</persistence-unit>
</persistence>
package org.example.demo.hibernate;
import java.util.ArrayList;
import java.util.List;
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.OneToMany;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUtil;
public class Test {
public static void main(String[] args) {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("test");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Person person = new Person();
person.name = "john doe";
em.persist(person);
em.getTransaction().commit();
em.clear();
Person p = em.find(Person.class, person.id);
PersistenceUtil persistenceUtil = Persistence.getPersistenceUtil();
// true
System.out.println(persistenceUtil.isLoaded(p));
// true
System.out.println(persistenceUtil.isLoaded(p, "name"));
// false
System.out.println(persistenceUtil.isLoaded(p.books));
em.close();
factory.close();
}
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
Integer id;
String name;
@OneToMany(mappedBy = "author")
List<Book> books = new ArrayList<>();
}
@Entity(name = "Book")
public static class Book {
@Id
@GeneratedValue
Integer id;
String title;
@ManyToOne
Person author;
}
}