Hibernate create too much java classes - java

I generate java classes from db with hibernate. In configuration i set to generate:
And get 3 files from each table. For example:
Address.java
package gen;
// Generated 16.08.2012 12:47:01 by Hibernate Tools 3.4.0.CR1
/**
* Address generated by hbm2java
*/
public class Address implements java.io.Serializable
{
private AddressId id;
public Address()
{
}
public Address(AddressId id)
{
this.id = id;
}
public AddressId getId()
{
return this.id;
}
public void setId(AddressId id)
{
this.id = id;
}
}
AddressId.java
package gen;
// Generated 16.08.2012 12:47:01 by Hibernate Tools 3.4.0.CR1
/**
* AddressId generated by hbm2java
*/
public class AddressId implements java.io.Serializable
{
private String codeOkato;
private String codeKladr;
private String postalCode;
private String region;
private String house;
private String building;
private String structure;
private String apartment;
private String note;
public AddressId()
{
}
public AddressId(String codeOkato, String codeKladr, String postalCode, String region, String house, String building,
String structure, String apartment, String note)
{
this.codeOkato = codeOkato;
this.codeKladr = codeKladr;
this.postalCode = postalCode;
this.region = region;
this.house = house;
this.building = building;
this.structure = structure;
this.apartment = apartment;
this.note = note;
}
public String getCodeOkato()
{
return this.codeOkato;
}
public void setCodeOkato(String codeOkato)
{
this.codeOkato = codeOkato;
}
public String getCodeKladr()
{
return this.codeKladr;
}
public void setCodeKladr(String codeKladr)
{
this.codeKladr = codeKladr;
}
public String getPostalCode()
{
return this.postalCode;
}
public void setPostalCode(String postalCode)
{
this.postalCode = postalCode;
}
public String getRegion()
{
return this.region;
}
public void setRegion(String region)
{
this.region = region;
}
public String getHouse()
{
return this.house;
}
public void setHouse(String house)
{
this.house = house;
}
public String getBuilding()
{
return this.building;
}
public void setBuilding(String building)
{
this.building = building;
}
public String getStructure()
{
return this.structure;
}
public void setStructure(String structure)
{
this.structure = structure;
}
public String getApartment()
{
return this.apartment;
}
public void setApartment(String apartment)
{
this.apartment = apartment;
}
public String getNote()
{
return this.note;
}
public void setNote(String note)
{
this.note = note;
}
public boolean equals(Object other)
{
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof AddressId))
return false;
AddressId castOther = (AddressId) other;
return ((this.getCodeOkato() == castOther.getCodeOkato()) || (this.getCodeOkato() != null
&& castOther.getCodeOkato() != null && this.getCodeOkato().equals(castOther.getCodeOkato())))
&& ((this.getCodeKladr() == castOther.getCodeKladr()) || (this.getCodeKladr() != null
&& castOther.getCodeKladr() != null && this.getCodeKladr().equals(castOther.getCodeKladr())))
&& ((this.getPostalCode() == castOther.getPostalCode()) || (this.getPostalCode() != null
&& castOther.getPostalCode() != null && this.getPostalCode().equals(castOther.getPostalCode())))
&& ((this.getRegion() == castOther.getRegion()) || (this.getRegion() != null && castOther.getRegion() != null && this
.getRegion().equals(castOther.getRegion())))
&& ((this.getHouse() == castOther.getHouse()) || (this.getHouse() != null && castOther.getHouse() != null && this
.getHouse().equals(castOther.getHouse())))
&& ((this.getBuilding() == castOther.getBuilding()) || (this.getBuilding() != null
&& castOther.getBuilding() != null && this.getBuilding().equals(castOther.getBuilding())))
&& ((this.getStructure() == castOther.getStructure()) || (this.getStructure() != null
&& castOther.getStructure() != null && this.getStructure().equals(castOther.getStructure())))
&& ((this.getApartment() == castOther.getApartment()) || (this.getApartment() != null
&& castOther.getApartment() != null && this.getApartment().equals(castOther.getApartment())))
&& ((this.getNote() == castOther.getNote()) || (this.getNote() != null && castOther.getNote() != null && this
.getNote().equals(castOther.getNote())));
}
public int hashCode()
{
int result = 17;
result = 37 * result + (getCodeOkato() == null ? 0 : this.getCodeOkato().hashCode());
result = 37 * result + (getCodeKladr() == null ? 0 : this.getCodeKladr().hashCode());
result = 37 * result + (getPostalCode() == null ? 0 : this.getPostalCode().hashCode());
result = 37 * result + (getRegion() == null ? 0 : this.getRegion().hashCode());
result = 37 * result + (getHouse() == null ? 0 : this.getHouse().hashCode());
result = 37 * result + (getBuilding() == null ? 0 : this.getBuilding().hashCode());
result = 37 * result + (getStructure() == null ? 0 : this.getStructure().hashCode());
result = 37 * result + (getApartment() == null ? 0 : this.getApartment().hashCode());
result = 37 * result + (getNote() == null ? 0 : this.getNote().hashCode());
return result;
}
}
Address.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 16.08.2012 12:47:01 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="gen.Address" table="address">
<composite-id name="id" class="gen.AddressId">
<key-property name="codeOkato" type="string">
<column name="code_okato" length="11" />
</key-property>
<key-property name="codeKladr" type="string">
<column name="code_kladr" length="20" />
</key-property>
<key-property name="postalCode" type="string">
<column name="postal_code" length="6" />
</key-property>
<key-property name="region" type="string">
<column name="region" />
</key-property>
<key-property name="house" type="string">
<column name="house" />
</key-property>
<key-property name="building" type="string">
<column name="building" />
</key-property>
<key-property name="structure" type="string">
<column name="structure" />
</key-property>
<key-property name="apartment" type="string">
<column name="apartment" />
</key-property>
<key-property name="note" type="string">
<column name="note" length="1500" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
How you can see in mapping file uses class Address.java. But with this class i cant set or get any parametrs like apartment or building. There only in AddressId class.
I all examples with hibernate what i see uses only one java class. What function of second class which hibernate fenerate.
So i wanna do all correct. And how i gonna generate java classes or use what i generete to waork with xml files and data base?
And another question how to create annotations in cenerated java classes autometiclly?

To generate annotations instead of hbm.xml, you should uncheck Hibernate XML mappings(.hbm.xml) and choose Generate EJB3 annotations.
Hibernate generates separate class for ID when table's primary key consists of multiple columns. It also does so when table does not have primary key at all.

Related

Composite key with one to many hibernate

I'm having a big problem trying to make this little program work
Here are my objects:
Class Country
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class Country implements Serializable {
private static final long serialVersionUID = 4947071545454L;
private String countryID;
private String countryName;
private Set<City> cities = new HashSet<City>();
public Country() {
}
public Country(String countryID, String countryName, Set<City> cities) {
this.countryID = countryID;
this.countryName = countryName;
this.cities = cities;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getCountryID() {
return countryID;
}
public void setCountryID(String countryID) {
this.countryID = countryID;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public Set<City> getCities() {
return cities;
}
public void setCities(Set<City> cities) {
this.cities = cities;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Country country = (Country) o;
return countryID != null ? countryID.equals(country.countryID) : country.countryID == null;
}
#Override
public int hashCode() {
return countryID != null ? countryID.hashCode() : 0;
}
public boolean addCity(City c){
return cities.add(c);
}
public boolean removeCity(City c){
return cities.remove(c);
}
}
Class City
import java.io.Serializable;
public class City implements Serializable{
private static final long serialVersionUID = 49470713545454L;
private String cityName;
private Country id;
public City(String cityName, Country id) {
this.cityName = cityName;
this.id = id;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public Country getId() {
return id;
}
public void setId(Country id) {
this.id = id;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
City city = (City) o;
if (cityName != null ? !cityName.equals(city.cityName) : city.cityName != null) return false;
return id != null ? id.equals(city.id) : city.id == null;
}
#Override
public int hashCode() {
int result = cityName != null ? cityName.hashCode() : 0;
result = 31 * result + (id != null ? id.hashCode() : 0);
return result;
}
}
An here are my xml archives:
country.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.samuel.hibernate.Country" table="country" catalog="training2">
<id name="country" type="string" column="countryID">
<generator class="assign"/>
</id>
<property name="countryName" type="string">
<column name="countryName" length="40" not-null="true" unique="true" />
</property>
<set name="city" inverse="true" cascade="all">
<key column="countryID" not-null="true" />
<one-to-many class="com.samuel.hibernate.City"/>
</set>
</class>
</hibernate-mapping>
city.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.samuel.hibernate.City" table="city" catalog="training2">
<composite-id name="id">
<key-many-to-one name="countryID" class="com.samuel.hibernate.Country"
column="countryID" />
<key-property name="cityName" column="cityName" type="string"/>
</composite-id>
</class>
</hibernate-mapping>
And here's my main class:
Main class
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
System.out.println("..");
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
// aquí es donde peta si falla conexión con Postgres
//creating seession factory object
System.out.println("Antes de crear sesion");
SessionFactory factory=cfg.buildSessionFactory();
System.out.println("Despues de crear sesion");
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
Set<City> citiesSpain = new HashSet<>();
Country spain = new Country("es","Spain",citiesSpain);
citiesSpain.add(new City("Barcelona",spain));
citiesSpain.add(new City("Madrid",spain));
session.persist(spain);
t.commit();
session.close();
factory.close();
System.out.println("END");
}
}
When I execute this code I get this error message:
Exception in thread "main" org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.component.PojoComponentTuplizer]
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:98)
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructDefaultTuplizer(ComponentTuplizerFactory.java:119)
at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:68)
at org.hibernate.mapping.Component.getType(Component.java:169)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:422)
at org.hibernate.mapping.RootClass.validate(RootClass.java:266)
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:451)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at com.samuel.hibernate.Main.main(Main.java:22)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.hibernate.tuple.component.ComponentTuplizerFactory.constructTuplizer(ComponentTuplizerFactory.java:95)
... 10 more
Caused by: org.hibernate.PropertyNotFoundException: Could not locate getter method for property [com.samuel.hibernate.Country#cityName]
at org.hibernate.internal.util.ReflectHelper.findGetterMethod(ReflectHelper.java:418)
at org.hibernate.property.access.internal.PropertyAccessBasicImpl.<init>(PropertyAccessBasicImpl.java:41)
at org.hibernate.property.access.internal.PropertyAccessStrategyBasicImpl.buildPropertyAccess(PropertyAccessStrategyBasicImpl.java:27)
at org.hibernate.mapping.Property.getGetter(Property.java:308)
at org.hibernate.tuple.component.PojoComponentTuplizer.buildGetter(PojoComponentTuplizer.java:138)
at org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:47)
at org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:41)
... 15 more
I've tried looking online but I don't seem to find the solution. In my example, one country can have many cities, but one city can only have on country.
This error means that one or more setters/getters is missing. Make sure you define matching getters/setters for all your properties. And make sure that your properties annotated correctly.
I think that problem is that your cities set name is different in a class and in hbm.xml file. In your entity class you defined set as Set<City> cities and in your XML file you defined this property as name="city". So hibernate is searching setters and getters for city named property.
Make sure that variable and property name coincides. And add empty constructor in City.class.

How Lazy loading of child records works in Hibernate?

I know the above question is very common but I just wanted to know exactly when and How Hibernate is fetching the lazily loaded child records.
below is the sample table structure:
Table Structure:
employee_table(e_id, e_name, e_sal)
(100, XYZ, 20000)
mobile_table(m_id, m_number, e_id)
(1, 8728271817, 100)
(2, 0983813919, 100)
Employee.java
public class Employee implements Serializable {
private static final long serialVersionUID = 1930751473454928876L;
private long employeeId;
private String employeeName;
private double employeeSal;
private Set<Mobile> mobiles;
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public double getEmployeeSal() {
return employeeSal;
}
public void setEmployeeSal(double employeeSal) {
this.employeeSal = employeeSal;
}
public Set<Mobile> getMobiles() {
return mobiles;
}
public void setMobiles(Set<Mobile> mobiles) {
this.mobiles = mobiles;
}
}
Employee.hbm.xml
<hibernate-mapping>
<class name="edu.sandip.hibernate.Employee" table="EMPLOYEE_T">
<id name="employeeId" column="e_id" type="long">
<generator class="increment" />
</id>
<property name="employeeName" column="e_name" type="string" />
<property name="employeeSal" column="e_sal" type="double" />
<set name="mobiles" table="MOBILE_T" inverse="true" lazy="true" fetch="select" >
<key>
<column name="e_id" not-null="true" />
</key>
<one-to-many class="edu.sandip.hibernate.Mobile" />
</set>
</class>
</hibernate-mapping>
Mobile.java
public class Mobile implements Serializable {
private static final long serialVersionUID = 6279006639448045512L;
private long mobId;
private String mobileNumber;
private Employee employee;
public long getMobId() {
return mobId;
}
public void setMobId(long mobId) {
this.mobId = mobId;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((mobileNumber == null) ? 0 : mobileNumber.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Mobile other = (Mobile) obj;
if (mobileNumber == null) {
if (other.mobileNumber != null)
return false;
} else if (!mobileNumber.equals(other.mobileNumber))
return false;
return true;
}
}
Mobile.hbm.xml
<hibernate-mapping>
<class name="edu.sandip.hibernate.Mobile" table="MOBILE_T">
<id name="mobId" column="m_id" type="long">
<generator class="increment" />
</id>
<property name="mobileNumber" column="m_number" type="string" />
<many-to-one name="employee" class="edu.sandip.hibernate.Employee" fetch="select">
<column name="e_id" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
Below is the code block to fetch the list of employees:
public List<Employee> getAllEmployees() {
List<Employee> list = null;
Session session = null;
try {
session = getSession();
Query query = session.createQuery("FROM Employee");
list = query.list();
for(Employee employee : list) {
System.out.println("Emaployee Name: " + employee.getEmployeeName);
Set<Mobile> mobileSet = employee.getMobiles();
System.out.println("Mobile Numbers: ");
for(Mobile mobile : mobileSet) {
System.out.println(mobile.getMobileNumber());
}
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(session != null) {
System.out.println("session is still alive");
releaseSession(session);
}
}
return (list);
}
Now, Here the Mobile numbers is the child record of Employee which is been loaded by the Hibernate lazily as per the hibernate configuration in Employee.hbm.xml (lazy = true)
In the above code, after printing the employee name I am printing the mobile numbers of that
employee by iterating the Set mobiles.
I have checked and found that the iteration of the mobileSet is actually fetching the mobileNumbers.
i.e.
for(Mobile mobile : mobileSet) {
System.out.println(mobile.getMobileNumber());
}
SO, How It is happening? Because I am not using any hibernate specific API to fetch the lazily loaded child records. Just Iterating over the set of mobile numbers.
Then how Hibernate is fetching the child records internally? What is the background job that Hibernate is doing while I am iterating over the mobileSet?
Please help in understanding my doubt.
In hibernate lazy loading is triggered whenever a lazy loaded property is accessed and it is still managed by the em. If you would access a lazy loaded property when the entity is detached from the entity manager you would get a org.hibernate.LazyInitializationException
To apply the logic to your example:
mobile.getMobileNumber() will trigger the lazy loading in your code. Hibernate will then automaticly issue a query to the database. This works because hibernate instantiates proxy objects for lazy loaded propertys, once you try to access these proxys hibernate tries to load them in from the database. this will work if the object is managed and this will result in forementioned exception if the entity is detached. There is no need at all to use a hibernate API to trigger the lazy loading it just happens automaticly when a lazy configured property is accessed
In your Employee.hbm.xml file you have stated that the fetch strategy for Mobile is lazy.
That tells hibernate to generate a simple SQL statement to the table that corresponds to Employee without performing any joins with the corresponding Mobile table.
However when employee.getMobiles() is called,
Hibernate will issue a select query to the latter table using a where clause with the primary key of the former table.
The way the fetching is implemented here leads to the infamous N+1 problem
This topic could help you to understand what is the trick behind lazy loading :
How proxy loads the lazy property in Hibernate/JPA

JPA with Hibernate - Sequence not working with XML

I'm using JPA with Hibernate for the first time and I try to set up automated ID generation using existing sequences from my Oracle database.
My entity mapping looks as follows:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
version="2.0">
<entity class="AmmAdapter.DataAccessLayer.Models.AmmSyncTypePriorities">
<table name="AMM_SYNC_TYPE_PRIORITIES" schema="TESTESTERON" catalog=""/>
<attributes>
<id name="id">
<column name="ID" nullable="false" length="10"/>
<generated-value strategy="SEQUENCE" generator="MODEL_SEQ"/>
<sequence-generator name="MODEL_SEQ" sequence-name="AMM_ED_GROUP_TYPES_SEQ"/>
</id>
<basic name="priority">
<column name="PRIORITY" nullable="false" length="10"/>
</basic>
<one-to-many name="ammSyncMessageTypesesById" mapped-by="ammSyncTypePrioritiesByPriority"
target-entity="AmmAdapter.DataAccessLayer.Models.AmmSyncMessageTypes"/>
</attributes>
</entity>
</entity-mappings>
My Java class looks as follows:
package AmmAdapter.DataAccessLayer.Models;
import java.util.Collection;
public class AmmEdGroupTypes
{
private Integer id;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
#Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AmmEdGroupTypes that = (AmmEdGroupTypes)o;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return true;
}
#Override
public int hashCode()
{
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
private Collection<AmmEndDeviceGroups> ammEndDeviceGroupsesById;
public Collection<AmmEndDeviceGroups> getAmmEndDeviceGroupsesById()
{
return ammEndDeviceGroupsesById;
}
public void setAmmEndDeviceGroupsesById(Collection<AmmEndDeviceGroups> ammEndDeviceGroupsesById)
{
this.ammEndDeviceGroupsesById = ammEndDeviceGroupsesById;
}
}
And my sequence AMM_ED_GROUP_TYPES_SEQ has:
Min value: 1
Max value: 9999999999999999999999999999
Increment by: 1
I use the following test code to determine if auto generation is working:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("AdapterPersistence");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
AmmEdGroupTypes newGroup = new AmmEdGroupTypes();
newGroup.setName("test");
System.out.println(newGroup.getId());
And I get a null ID as result. Does someone know what the problem is?
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
AmmEdGroupTypes newGroup = new AmmEdGroupTypes();
newGroup.setName("test");
entityManager.persist(newGroup);
entityManager.getTransaction().commit();
System.out.println(newGroup.getId());

Hsql error after inserting 2. object via hibernate

I am using hibernate to insert objects of the class meal in a hsql DB It works fine if I only insert 1 object but as soon as I try to insert a second it gives me an error. Here is my code and the error:
public static void main(String[] args) {
Main obj = new Main();
Long mealId1 = obj.saveMeal("Pommes");
Long mealId2 = obj.saveMeal("Doener1");
}
public Long saveMeal(String mealName)
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
Long mealId = null;
try {
transaction = session.beginTransaction();
Meal meal = new Meal(mealName);
mealId = (Long) session.save(meal);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
return mealId;
}
package data;
import java.util.List;
import java.util.LinkedList;
public class Meal implements java.io.Serializable {
private long id;
private String name;
private List<Image> images;
private List<Review> reviews;
private Grouping grouping;
public Meal() {
}
public Meal(String mealName) {
name = mealName;
}
public Meal(long mealId, String mealName) {
id = mealId;
name = mealName;
}
public long getId() {
return id;
}
public void setId(long mealId) {
id = mealId;
}
public String getMealName() {
return name;
}
public void setMealName(String mealName) {
name = mealName;
}
public float getAvg() {
float avg = 0;
for (int i = 0; i < reviews.size(); i++)
{
avg = avg + reviews.get(i).getReviewPoints();
}
return avg;
}
public List<Image> getNumberImages(int number) {
assert (number >= 0);
return images.subList(0, number) ;
}
public List<Image> getImages() {
return images;
}
public void setImages(LinkedList<Image> images) {
this.images = images;
}
public List<Review> getReviews(int number) {
assert (number >= 0);
return reviews.subList(0, number) ;
}
public LinkedList<String> getAltNames() {
LinkedList<String> altNames = new LinkedList<String>();
LinkedList<Meal> altNameMeals = grouping.getMeals();
for (int i = 0; i < altNameMeals.size(); i++)
{
altNames.add(altNameMeals.get(i).getMealName());
}
return altNames;
}
public void addReview(Review review) {
if (!reviews.contains(review)) {
reviews.add(review);
}
}
public Grouping getGrouping() {
return grouping;
}
public void setGrouping(Grouping grouping) {
this.grouping = grouping;
}
public void addImage(Image image) {
if (!images.contains(image)) {
images.add(image);
}
}<hibernate-mapping>
<class name="data.Meal" table="MEAL">
<id name="id" type="long" access="field">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String" access="field">
<column name="NAME" />
</property>
<list name="images" inverse="false" table="IMAGE" lazy="true" access="field">
<key>
<column name="ID" />
</key>
<list-index column="column_name" />
<one-to-many class="data.Image" />
</list>
<list name="reviews" inverse="false" table="REVIEW" lazy="true" access="field">
<key>
<column name="ID" />
</key>
<list-index></list-index>
<one-to-many class="data.Review" />
</list>
<many-to-one name="grouping" class="data.Grouping" fetch="join">
<column name="GROUPING" />
</many-to-one>
</class>
First is the main method 2nd the class to be persitet and 3rd the hibernate mapping for the class. This is the error message:
843 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: -104, SQLState: 23505
843 [main] ERROR org.hibernate.util.JDBCExceptionReporter - integrity constraint violation: unique constraint or index violation; SYS_PK_10585 table: MEAL
843 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
You declared that you want to assign values to id property manually (<generator class="assigned" />), but don't actually assign it.
So, you need to assign the value manually or declare different id generation strategy.

Hibernate mapping issue with composite-id

I am using hibernate3 in my java app to access sqlserver 2008 enterprise.
The hibernate mapping uses composite id and when i try to load model it returns null. I spent days to resolve it but still no result. Composite id mapping should be used for multiple field based PK, but in my table no such PK, i wonder why the JBoss Hibernate Tool(eclipse plugin) generated it with composite id mapping ?
I would appreciate any help or comments.
Hibernate Models:
public class AuthorLoginTrack implements java.io.Serializable {
private AuthorLoginTrackId id;
public AuthorLoginTrack() {
}
public AuthorLoginTrack(AuthorLoginTrackId id) {
this.id = id;
}
public AuthorLoginTrackId getId() {
return this.id;
}
public void setId(AuthorLoginTrackId id) {
this.id = id;
}
}
public class AuthorLoginTrackId implements java.io.Serializable {
private long id;
private String userId;
private Date dateCreated;
public AuthorLoginTrackId() {
}
public AuthorLoginTrackId(long id, String userId) {
this.id = id;
this.userId = userId;
}
public AuthorLoginTrackId(long id, String userId, Date dateCreated) {
this.id = id;
this.userId = userId;
this.dateCreated = dateCreated;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getUserId() {
return this.userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof AuthorLoginTrackId))
return false;
AuthorLoginTrackId castOther = (AuthorLoginTrackId) other;
return (this.getId() == castOther.getId())
&& ((this.getUserId() == castOther.getUserId()) || (this
.getUserId() != null
&& castOther.getUserId() != null && this.getUserId()
.equals(castOther.getUserId())))
&& ((this.getDateCreated() == castOther.getDateCreated()) || (this
.getDateCreated() != null
&& castOther.getDateCreated() != null && this
.getDateCreated().equals(castOther.getDateCreated())));
}
public int hashCode() {
int result = 17;
result = 37 * result + (int) this.getId();
result = 37 * result
+ (getUserId() == null ? 0 : this.getUserId().hashCode());
result = 37
* result
+ (getDateCreated() == null ? 0 : this.getDateCreated()
.hashCode());
return result;
}
}
Hibernate Mapping:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 6, 2010 4:17:46 PM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="com.entity.model.AuthorLoginTrack" table="AuthorLoginTrack" schema="dbo" catalog="tribetoyota_db">
<composite-id name="id" class="com.entity.model.AuthorLoginTrackId">
<key-property name="id" type="long">
<column name="ID" precision="18" scale="0" />
</key-property>
<key-property name="userId" type="string">
<column name="UserID" length="20" />
</key-property>
<key-property name="dateCreated" type="timestamp">
<column name="DateCreated" length="16" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
Table Dump:
/****** Object: Table [dbo].[AuthorLoginTrack] Script Date: 04/14/2010 20:43:02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[AuthorLoginTrack](
[ID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[UserID] [varchar](20) NOT NULL,
[DateCreated] [smalldatetime] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[AuthorLoginTrack] ADD CONSTRAINT [DF_AuthorLoginTrack_DateCreated] DEFAULT (getdate()) FOR [DateCreated]
GO
Table:
ID UserID DateCreated
------------------------------------
5 cooler 2005-03-17 18:56:00
6 miumiu 2005-03-17 19:46:00
DAO Code:
AuthorLoginTrack track;
AuthorLoginTrackId trackId;
trackId = new AuthorLoginTrackId();
trackId.setId(5);
track = (AuthorLoginTrack)getSession().load(AuthorLoginTrack.class, trackId);
return track.getId().getUserId(); // returns null why ?:((
Session.load(...) assumes that there's indeed an instance with the given id, most of the time it will return a proxy object without hitting the database. What you're really querying is an instance with id 5, userId = null and date == null.
Basically you're getting an unitialized proxy with a copy of the composite id you used to query it. If the instance really exists you'll be fine, otherwise you'll get an ObjectNotFoundException the first time you'll try to use the object.

Categories