Hibernate: Mapping with one-to-many on non-primary-key columns - java

I'm stuck at the hibernate xml mapping configuration.
I've established some tables with foreign key constraints in my MSSQL database:
Table ItemsBase
ID int primary-key
ItemID int unique index
... some more columns
Table Others
ID int primary-key
ItemID int unique index
... some more columns
The foreign key constraint is configured to connect these two tables by using the column "ItemID".
My ItemsBase.hbm.xml files looks like:
<hibernate-mapping>
<class name="de.delife.sql.ItemsBase" table="ItemsBase" schema="dbo" catalog="Delife_Plenty">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="itemId" type="java.lang.Integer">
<column name="ItemID" unique="true" />
</property>
<set name="otherses" table="Others" inverse="true" lazy="true" fetch="select">
<key property-ref="itemId">
<column name="ItemID" />
</key>
<one-to-many class="de.delife.sql.Others" not-found="ignore" />
</set>
</class>
</hibernate-mapping>
and the Others.hbm.xml files looks like:
<hibernate-mapping>
<class name="de.delife.sql.Others" table="Others" schema="dbo" catalog="Delife_Plenty">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="itemsBase" class="de.delife.sql.ItemsBase" fetch="select" property-ref="itemId">
<column name="ItemID" unique="true" />
</many-to-one>
</class>
</hibernate-mapping>
Everything looks fine for me, but when I run my program I get a hibernate error:
property [itemId] not found on entity [de.delife.sql.Others]
I have an established relation between ItemsBase and a table named ItemsProperties and it works, but with this "pretty" table "Others" I'm stuck.
I would be glad, if someone can help me on this matter. Thx in advance.

Try like this. I tried and its working for me.
<set name="otherses" table="Others" inverse="true" lazy="true" fetch="select">
<key column="itemId" foreign-key="itemId" property-ref="itemId"/>
<one-to-many class="de.delife.sql.Others" not-found="ignore" />
</set>

Item id should be defined as a property in Others.hbm.xml:
<property name="itemId" column="ItemId"/>

Related

Hibernate One-to-One foreign key using xml Not deleting orphan

I have two classes A and B.
<id name="id" type="java.lang.Integer" column="A_ID">
<generator class="identity" />
</id>
<property name="name" column="NAME"/>
<many-to-one name="B" cascade="all"
column="B_ID" unique="true"/>
</class>
<id name="id" type="java.lang.Integer" column="B">
<generator class="identity" />
</id>
<property name="n" column="N"/>
</class>
A.setB(new B());
When I save it works fine.
when I remove the record. A.setB(null);
I see a orphan record.
I see in A Table the column for B is null but on B Table there is still a record.
I tried to look in other solutions but couldnot find the ans to this.

'Repeated column in mapping' when mapping a class to an existing list of objects

I'm adding ResourcePermission to the object Report. Each Query object can have in a one-to-one relationship. A Query extends Resource and has ResourcePermission's in a one-to-many (one Query to many Permissions).
I need to add the same property to the Report object associated with the Query because it can have different permissions. When I add the list and the map the one Query to many Permission relationship I get
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.bio.ResourcePermission column: resource_id (should be mapped with insert="false" update="false")
Which I am not understanding why, the Report does not extend Query or Resource and therefore it isn't mapped twice. Can a table just not be the many for multiple one-to-many relationships?
<class name="com.bio.Report" table="REPORT">
<id name="id" type="long" column="id">
<generator class="foreign">
<param name="property">query</param>
</generator>
</id>
<property name="name" column="name"/>
<!--Trying to add this list mapping breaks it-->
<bag name="permissions" table="RESOURCE_PERMISSION">
<key column="resource_id" not-null="true"/>
<one-to-many class="com.bio.ResourcePermission"/>
</bag>
<!-- This query extends Resource-->
<one-to-one name="query" class="com.bio.Query" />
</class>
This is the original item that had ResourcePermissions
<class name="com.bio.Resource" table="RESOURCE">
<id name="id" type="long" column="id">
<generator class="native">
<param name="sequence">SEQ_RESOURCE_AUTO</param>
</generator>
</id>
<bag name="permissions" table="RESOURCE_PERMISSION" lazy="true" batch-size="50" cascade="all-delete-orphan">
<key column="resource_id" not-null="true"/>
<one-to-many class="com.bio.ResourcePermission"/>
</bag>
</class>
The Permission mapping
<class name="com.bio.ResourcePermission" table="RESOURCE_PERMISSION">
<id name="id" type="long" column="id">
<generator class="native">
<param name="sequence">SEQ_RES_PERM_AUTO</param>
</generator>
</id>
<property name="canEdit" column="edit"/>
<property name="canView" column="can_view"/>
<property name="canRun" column="run"/>
<property name="everyone" column="everyone"/>
</class>
I had to set inverse="true" on the Report mapping since the ReportPermission will be responsible for the relationship.
<bag name="permissions" table="RESOURCE_PERMISSION" inverse="true">
<key column="resource_id" not-null="true"/>
<one-to-many class="com.bio.ResourcePermission"/>
</bag>

Add columns to table at runtime

I have a table mapped by the following xml file:
<hibernate-mapping auto-import="true" default-access="property"
default-cascade="none" default-lazy="true">
<class abstract="false" name="domain.CustomFlow"
table="CUSTOM_FLOW">
<id column="CUSTOM_FLOW_ID" name="id">
<generator class="native" />
</id>
<many-to-one name="customForm"
class="domain.CustomForm" fetch="select">
<column name="FORM_ID" not-null="true" />
</many-to-one>
<dynamic-component insert="true" name="customProperties"
optimistic-lock="true" unique="false" update="true">
</dynamic-component>
</class>
</hibernate-mapping>
I do update xml mapping at runtime so it will look like:
<dynamic-component insert="true" name="customProperties" optimistic-lock="true" unique="false" update="true"><property column="testColumn" generated="never" lazy="false" name="testColumn" not-null="false" optimistic-lock="true" type="java.lang.String" unique="false"/></dynamic-component>
How can I update the db schema at runtime. I'm using hibernate and spring?
have look here
it will show you how to update table structure on fly

org.hibernate.exception.MappingException or ConstraintViolation

I am using hibernate 4.1.9. I have Users, and the Users have a list of Accounts, and Accounts have list of Transactions. Here is my hbm.xml
<?xml version="1.0"?>
<class name="User" table="users">
<id name="userId" column="userid">
<generator class="increment"/>
</id>
<property name="username" column="username" not-null="true"/>
<property name="password" column="password" not-null="true"/>
<property name="registerDate" type="timestamp" column="register_date"/>
<list name="accounts" table="accounts" inverse="true" cascade="all">
<key column="userid" not-null="true"/>
<index column="accountid"/>
<one-to-many class="com.joe.data.Account"/>
</list>
</class>
<class name="Account" table="accounts">
<id name="accountId" column="accountid">
<generator class="increment"/>
</id>
<property name="balance" type="big_decimal" column="balance"/>
<property name="lastModified" type="timestamp" column="last_modified"/>
<list name="txns" table="transactions" inverse="true" cascade="all">
<key column="accountId" not-null="true"/>
<index column="transactionId"/>
<one-to-many class="com.joe.data.Transaction"/>
</list>
<many-to-one name="userId" class="User" column="userid" not-null="true"
unique="true" cascade="all"/>
<many-to-one name="accountType" class="AccountType" column="account_type"
not-null="true" cascade="all" unique="true" />
</class>
<class name="Transaction" table="transactions">
<id name="transactionId" column="transactionid">
<generator class="increment"/>
</id>
<property name="description" column="description"/>
<property name="amount" type="big_decimal" column="amount"/>
<property name="dateAdded" column="date_added"/>
<property name="reoccuring" type="numeric_boolean" column="reoccuring"/>
<many-to-one name="category" class="Category" column="category"
not-null="true" cascade="all" unique="true" />
</class>
<class name="Category" table="categories">
<id name="categoryId" column="categoryid"/>
<property name="categoryName" column="categoryname" not-null="true"/>
</class>
<class name="AccountType" table="account_types">
<id name="accountType" column="account_type"/>
<property name="accountName" column="name"/>
</class>
If I leave inverse="true" on the list of accounts (in the User) I get the ConstraintViolationException because the userid is not getting put in the insert query. If I take inverse="true" off of the list of accounts, I get org.hibernate.MappingException: Repeated column in mapping for entity: com.joe.data.Account column: accountid (should be mapped with insert="false" update="false")
To clarify lowercase names are database columns names, camel case are class variable names. I know Transaction class isn't working quite right yet, but if I could get the Accounts to insert I could do the same thing to get the Transactions to insert.
Edit: I added the many-to-one on the Account class and now I am getting another exception where hibernate is complaining about missing a getter for userId in com.joe.data.Account
In order to get inverse="true" work, you need to define many-to-one for User in Account and for Account in Transaction class and mappings.

Establish relationship from one table to another table in Hibernate

I have 2 tables, one table is
City Table ( int id, string name)
and my another table is
Distance table(int id,int cityId (FK city),int neighbourId(FK city))
I want to use Hibernate but I can't establish a relationship between these tables in Hibernate.
what about something like
<class name="City" table="CITIES">
<id name="id" type="integer">
<generator class="native" />
</id>
<property name="name" />
<set name="neighbours" table="DISTANCES">
<key column="city_id" />
<many-to-one name="neighbour" class="City" />
</set>
</class>
didn't test it though.
Ok, I can see any problem to do it normally.
<class name="City" table="CITY">
<id name="id" type="integer">
<generator class="native" />
</id>
<property name="name" />
</class>
<class name="Distance" table="DISTANCE">
<id name="id" type="integer">
<generator class="native" />
</id>
<many-to-one name="city" column="cityId" class="City"/>
<many-to-one name="neighbour" column="neighbourId" class="City"/>
</class>
didn't test it neither.

Categories

Resources