hibernate

How to implement an Audit Interceptor using iBATIS ?

I want to log all changes in my database for auditing purposes, using a table called AuditEvent that stores the modified row ID (primary key), table name, column name, previous value, new value, date of change (timestamp), operation type (insert / update / delete) and the name of the user who did the changes. I'm using SQL Server 2005,...

Hibernate - child collection Identifier changed in update

Hi guys, when I trying to update the object RFQ , Hibernate tends to update its child collection BQMaster and trying to set and update the identifier of BQMaster which normally it should not be allowed. The postgre log file: *2009-05-22 11:16:53 ERROR: duplicate key violates unique constraint "bq_masters_pkey"* *2009-05-22 11:16:53 STAT...

Can hibernate map EnumTypes with whitespaces?

Having a hibernate mapping a legacy database I want to use EnumTypes to map certain columns that contain string constants with whitespace to some Enum class. The mapping: @Entity @Table(name = "OPERATOR") public class Operator { @Id @Column(name = "ID") private Long id; ... @Enumerated(EnumType.STRING) @Colu...

Why do associated collections contain null values? (Hibernate, Annotation, Spring)

[Edit: Apparently, this is only an issue for arrays and FoxyBOA's answer might direct to (or even is) the answer.] Hi, my question relates to these software: Hibernate3+Annotation, Spring MVC, MySQL and in this example also Spring Security. I was wondering, why collections, which are automatically associated by Hibernate contain null v...

Can't get many-to-many to work with Hibernate 3.3.1GA

I have a simple many-to-many mapping: @Entity @Table(name="ITEM") public static class Item { @Id @GeneratedValue long id; @Column String name; @ManyToMany(cascade={ CascadeType.ALL }) Set<Category> categories = new HashSet<Category> (); } @Entity @Table(name="CATEGORY") public static class Category { @Id...

Is there any way to declare final fields for Hibernate-managed objects?

I'm just getting started with Hibernate, and all the examples I'm seeing so far look pretty much like the tutorial in the Hibernate documentation: package org.hibernate.tutorial.domain; import java.util.Date; public class Event { private Long id; private String title; private Date date; public Event() {} /* Acces...

How to update a collection-type relation with mappedBy in Hibernate?

I have two related entities, say @Entity public class Book { @ManyToOne Shelf shelf; } @Entity public class Shelf { @OneToMany(mappedBy="shelf") Set<Book> books; } If I fetch an empty shelf (no books), create and persist a new book to the shelf and then fetch that shelf again, its books collection is empty. When I run...

What is the proper way to re-attach detached objects in Hibernate?

I have a situation in which I need to re-attach detached objects to a hibernate session, although an object of the same identity MAY already exist in the session, which will cause errors. Right now, I can do one of two things. 1.) getHibernateTemplate().update( obj ) This works if and only if an object doesn't already exist in the hibe...

Best Spring/Hibernate configuration for never changing tables

I'm currently using Spring+Hibernate+MySQL for a project I'm working on. I realized that a have quite a few tables which never change. They are static, no insert or updates can ever occur on these tables, and so can be considered immutable. All accesses to these tables are via lazy loading (can be made eager) of entity collections and...

Workarounds for Hibernate's lack of delete-orphan support for one-to-one and many-to-one relationships?

Hibernate has no support for "delete-orphan" cascading of one-to-one or many-to-one relationships. I recently discovered this, and it's giving me a serious headache. I have a couple classes in my model that were designed such that the child has no real world meaning outside of the parent. I only have one DAO for the parent, and not a ...

Can Hibernate work with MySQL's "ON DUPLICATE KEY UPDATE" syntax?

MySQL supports an "INSERT ... ON DUPLICATE KEY UPDATE ..." syntax that allows you to "blindly" insert into the database, and fall back to updating the existing record if one exists. This is helpful when you want quick transaction isolation and the values you want to update to depend on values already in the database. As a contrived exa...

Getting unique result in Hibernate

how can we get distinct result by using criteria in hibernate. ...

hibernate one-to-one or component?

Hi Gurus, I am wondering how to write the model, hbm.xml for table Company ------- id(PK) name address typeid(fk) Type ---- id(PK) type class Company(){ int id; String name; String address; Type type; } class Type(){ int id; String type; } (with get/set methods) How to write the hbm? I am using the hibernate 3.x. ...

Using @Table with schema name in Hibernate 3.3.1ga and HSQLDB

How can I make this work in unit tests using Hibernate 3.3.1ga and HSQLDB: @Entity @Table(name="CATEGORY", schema="TEST") public static class Category { ... } The problem is that Hibernate expects the schema to exist. The second problem is that Hibernate issues the CREATE TABLE TEST.CATEGORY before any of my code runs (this happens de...

Cannot use identity column key generation with <union-subclass> ( TABLE_PER_CLASS )

com.something.SuperClass: @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class SuperClass implements Serializable { private static final long serialVersionUID = -695503064509648117L; long confirmationCode; @Id @GeneratedValue(strategy = GenerationType.AUTO) // Causes exception!!! p...

ManyToMany in Hibernate without composite keys

I am dealing with legacy database. And I am writing unit test using pojos & hibernate & HSQLDB. And I am getting the following error: 17:09:03,946 ERROR SchemaExport:349 - Attempt to define a second primary key in statement Let's say I have Post and Tag entities (and of course their tables posts and tags). And there is another table to...

how to assign different concurrency strategy to the same (persistence) entity?

I'm using JPA and I am using second-level cache for all the reference entities. Everything's working well, I can get the entities from second-level cache is they were already been selected before. Now, I have two applications, they both use the same database (so they both use the same table, values, etc). 1.The read-only application ju...

Hibernate/JPA Parent-Child - is it okay for Parent equals()/hashCode() to use the DB Id?

Given the following: @Entity public class Parent implements Serializable { @Id private Long id; // mapped ManyToOne below... private List<Child> children = new ArrayList<Child>(); ... } Is it a bad practice to have Parent.equals() and Parent.hashCode() use only id? I understand that Child.equals() and Child.hashCode() shou...

Hibernate Embeddable Inheritance

I have an object with a field that can be a number of object types. This object is encoded in a single table with a discriminator column for the field's subtypes. Each of these subtypes have their fields mapped to a column in the parent objects table. I cannot seem to model this in hibernate. The code bellow will return null for getSubfi...

Remote Hibernate Criteria

I have multiple services which call on my database service, which uses Hibernate, and I would like the remote services to be able to create a query and then pass that to be processes. Ideally I would like to pass a Criteria Object but it looks like it needs a Session which they won't have access to. Is there a process similar to the Crit...