如何解决为什么不能使用EntityManager查询JPA实体?
我有一个项目,它从多个数据库中的多个依赖项中提取实体类。每个数据库都有其自己的EntityManager,并且关联的实体位于其自己的唯一程序包名称空间中。
在生产中,它工作正常,但我试图与H2创建集成测试,并且遇到了问题。对于集成测试,我使用一个H2实例。
例如,在运行myEntityManager.createQuery(...).getResultList()
时,我将得到两个不同的错误。要么
实体名称未映射
或
找不到表 ENTITYNAME
该实体看起来像这样:
package com.myapp.model;
@Entity
@Table(name = "brand")
public class Brand implements Identifiable<Long>,Serializable {
@Id
@Column(name = "brand_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
// getters/setters toString,equals etc
}
和我查询实体的代码如下:
String select1 = "select * from brand"; // not mapped
String select2 = "select * from Brand"; // table not found
String select3 = "select * from com.myapp.model.brand"; // not mapped
String select4 = "select * from com.myapp.model.Brand"; // table not found
myEntityManager.createQuery(selectN,Brand.class).getResultList()
当我致电myEntityManager.getMetamodel().getEntities()
时,我确实在返回的列表中看到了该实体。以下是其中包含的一些字段:
- jpaEntityName =“品牌”
- javaType =“类com.myapp.Brand”
- typeName =“ com.myapp.Brand”
最后,这是我定义EntityManager的方式:
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
dataSource.setUsername("sa");
dataSource.setPassword("sa");
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setPackagesToScan("com.myapp.model","com.myapp.graph.model");
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.H2);
entityManagerFactory.setJpaVendorAdapter(vendorAdapter);
Map<String,Object> properties = Maps.newHashMap();
properties.put("spring.jpa.hibernate.ddl-auto","create");
entityManagerFactory.setJpaPropertyMap(properties);
EntityManager res = entityManager.createEntityManager();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。