gpt4 book ai didi

java - FetchType = Lazy 且可选 = false 在 JPA 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:36 25 4
gpt4 key购买 nike

我在我的帐户实体中使用多对一映射

@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name = "CONTACT_ID", referencedColumnName = "CONTACT_ID", nullable = false)
private Contact Contact;

在我的 DAO 中,我正在检索数据:

entitymanager.createQuery("SELECT v FROM Account v").getResultList();

虽然我使用可选 = fasle 和 fetchType = Lazy。我看到它正在运行所有查询并检索引用的表数据。

我正在使用 Spring Boot。以下是我的 Entitymanager 配置

@Configuration
@EnableTransactionManagement
public class DatabaseConfig {


@Autowired
private Environment env;

@Autowired
private DataSource dataSource;

@Autowired
private LocalContainerEntityManagerFactoryBean entityManagerFactory;


@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
/*dataSource.setDriverClassName(env.getProperty("db.driver"));*/
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.username"));
dataSource.setPassword(env.getProperty("db.password"));
return dataSource;
}


@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactory =
new LocalContainerEntityManagerFactoryBean();

entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
Properties additionalProperties = new Properties();
// additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
// additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
entityManagerFactory.setJpaProperties(additionalProperties);
return entityManagerFactory;
}

@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());
return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}

在application.properties中

hibernate.hbm2ddl.auto: update
hibernate.dialect: org.hibernate.dialect.Oracle12cDialect
hibernate.show_sql: true

最佳答案

试试这个:

在指定两个实体之间的关系的情况下使用@JsonIgnore

@JsonIgnore : as Spring Data REST will ignore fields or getters that are marked up with @JsonIgnore annotations.

关于java - FetchType = Lazy 且可选 = false 在 JPA 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39733190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com