Tag: hibernate

ParameterizedType并创建一个通用的dao

我尝试这个通用代码,因为我不想为我的数据库中的每个实体创建一个dao类,因为我有80个专门用于那些我将只执行CRUD查询的人。 因为在大多数情况下我只需要坚持或通过id进行查找。 public interface GenericDao { T create(T t); T read(PK id); T update(T t); void delete(T t); } 接口的impl @Component public class GenericDaoJpaImpl implements GenericDao { protected Class entityClass; @PersistenceContext protected EntityManager entityManager; public GenericDaoJpaImpl() { ParameterizedType genericSuperclass = (ParameterizedType) getClass() .getGenericSuperclass(); this.entityClass = (Class) genericSuperclass .getActualTypeArguments()[0]; } @Override public T create(T t) { […]

Hibernate 4.3,在构建SessionFactory时为什么要提供两次属性?

当你想创建一个SessionFactory(例如unit testing)时,你需要使用Hibernate 4.3.5,你必须提供两次属性: 一旦进行配置 第二次,将设置应用于服务注册表生成器 该示例如下所示: Properties properties = new Properties(); properties.put(“hibernate.dialect”, “org.hibernate.dialect.HSQLDialect”); properties.put(“hibernate.connection.driver_class”, “org.hsqldb.jdbcDriver”); properties.put(“hibernate.connection.url”, “jdbc:hsqldb:mem:test”); properties.put(“hibernate.connection.username”, “sa”); properties.put(“hibernate.connection.password”, “”); properties.put(“hibernate.hbm2ddl.auto”, “update”); properties.put(“hibernate.show_sql”, “true”); SessionFactory sessionFactory = new Configuration() .addProperties(properties) .addAnnotatedClass(SecurityId.class) .buildSessionFactory( new StandardServiceRegistryBuilder() .applySettings(properties) .build() ); 如果我评论: //.addProperties(properties) 然后“hibernate.hbm2ddl.auto”属性没有彻底: Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: SECURITYID 如果我评论: //.applySettings(properties) 我得到: […]

Hibernateexception:非法尝试取消引用集合

请考虑以下实体。 public class Product{ int id; Date effectiveDate; Date expiryDate; Set productInventories; } public class Inventory{ int invId; Date inventoryDate; boolean soldOut; int availableQuantity; Product product; } 以上两个实体分别映射到表Product和Inventory。 现在我必须根据Product实体和Inventory实体中的某些条件检索产品。 对于前提条件,旅行开始日期和旅行结束日期必须适合产品的有效期和失效日期。 产品库存应该有可用数量> 0。 要做到这一点,我怎么能写hql。 我可以写下面的内容吗? Query query = session.createQuery(“from Product As product ” + “where product.effectiveDate = :travelEndDate ” + “AND product.productInventories.availableQuantity >0 “); 当我执行上面的查询时,它会抛出非法尝试取消引用集合exception。

Hibernate向数据库发送多余的查询

我有一个奇怪的问题,Hibernate运行的查询比我要求的更多,并且看不到需要。 这是我的控制器: @Autowired UserService users; @RequestMapping(“/test”) @ResponseBody public String test() { User user = users.findUser(1L); return “Found user: “+user.getEmail(); } 这是UserService : @Component public class UserService { @javax.persistence.PersistenceUnit private EntityManagerFactory emf; private JpaTemplate getJpaTemplate() { return new JpaTemplate(emf); } public User findUser(long id) { long start = System.currentTimeMillis(); JpaTemplate jpaTemplate = getJpaTemplate(); User user = […]

Hibernate级联

Hibernate逆向工程生成的所有内容都是这样的 @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name =“column_id”) public Itinerary getColumnId(){ 返回this.columnId; } 我想要这个场景:当会话刷新时,首先保存所有构造的子节点,然后根据FK约束保存父对象。 当然,孩子需要先保存(自动!),因为有FK约束。 你告诉我:有一个CASCADE选项,但如何在JPA中使用它? 我尝试像这样添加级联: @ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.PERSIST) @JoinColumn(name =“column_id”) public Itinerary getColumnId(){ 返回this.columnId; } 对我不起作用。 先告诉我:应该用这个指令注释什么以及如何使它工作。 我得到“无法添加或更新子行:外键约束失败”exception。 事实上,我不想手工坚持一切! 只构造一个对象并坚持下去! 注释什么,使用什么指令以及如何使用?

ClassNotFoundException:org.hibernate.hql.internal.ast.HqlToken甚至在添加了classic.ClassicQueryTranslatorFactory 之后

可能重复: org.hibernate.HibernateException:无法实例化QueryTranslatorFactory:org.hibernate.hql.classic.ClassicQueryTransactionFactory 我正在使用Hibernate 4,Spring 3和JSF 2.0。 我正在运行jsf页面 org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.internal.ast.HqlToken [select generatedAlias0 from net.test.model.Request as generatedAlias0] 我已经在applicationContext.xml中的hibernate属性下面有以下内容 org.hibernate.hql.internal.classic. ClassicQueryTranslatorFactory 请注意我已经添加了org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean和org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter ,我怀疑在添加之后我再次获得org.hibernate.QueryException: ClassNotFoundException 我的问题是什么是ClassicQueryTranslatorFactory属性的ClassicQueryTranslatorFactory ? 如果没有这样的属性我怎么能解决这个问题? applicationContext.xml中 net.test.model.Request org.hibernate.dialect.Oracle10gDialect true org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory <!– Enable the configuration of transactional behavior based on annotations –> <!– Transaction Manager is defined –>

禁止使用Hibernate Envers为实体创建_AUD表?

如何禁用实体的Hibernate Envers创建_AUD表? 我想通过使用AuditListeners而不是允许控制Hibernate来创建表来进行自定义审计。

Hibernate自定义架构创建

create创建一个新的数据库模式, update create如果它不存在并更新现有数据库架构。 如果我想检查数据库模式是否存在,并且根据将创建数据库模式,我该如何实现。 目前我的applicationContext.xml的配置是: info.ems.models.User info.ems.models.Role org.hibernate.dialect.HSQLDialect true create 和HibernateEMSDao.java: public class HibernateEMSDao implements EMSDao { private final Logger logger = LoggerFactory.getLogger(getClass()); private HibernateTemplate hibernateTemplate; public void setSessionFactory(SessionFactory sessionFactory) { this.hibernateTemplate = new HibernateTemplate(sessionFactory); } public void saveUser(User user) { hibernateTemplate.saveOrUpdate(user); } public List listUser() { return hibernateTemplate.find(“from User”); } public void createSchema() { […]

Crudrepository /从其他表到实体的单个字段作为只读

因为我犯的任何错误,请耐心等待,因为这是我的第一个问题。 我有一个包含两个表的数据库,一个名为PERSON的表 使用以下实体: @Entity class Person { @Id private String guid; private String firstName; private String organisationGuid; … … } 一个表叫做: 组织 使用以下实体: @Entity class Organisation { @Id private String guid; private String name; … … } 如您所见,每个人都属于一个组织。 现在我需要列出我所有的人,以及组织的名称。 我不希望Person-entity上有完整的Organization-entity,而只是名称。 像这样: [{ “guid”: “xxx”, “firstName”: “Name”, “organisationGuid”: “yyy”, “organisationName”: “Name of yyy” }] 我怎样才能以最简单的方式实现这一目标? 我已经尝试过的事情: […]

在Hibernate中连接多个数据库

如何使用Hibernate动态连接到多个mysql数据库?