Relationship set between table and mapping table to use joins - java

I have two tables: a "Module" table and a "StaffModule" table. I want to display a list of modules by which staff are present on the staffmodule mapping table.
I've tried
from Module join Staffmodule sm with ID = sm.MID
with no luck, I get the following error
Path Expected for join!
However, I thought I had the correct join too allow this, but this is obviously not the case. Can any one help?
StaffModule HBM
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 26, 2010 9:50:23 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="Hibernate.Staffmodule" schema="WALK" table="STAFFMODULE">
<composite-id class="Hibernate.StaffmoduleId" name="id">
<key-many-to-one name="mid" class="Hibernate.Module">
<column name="MID"/>
</key-many-to-one>
<key-property name="staffid" type="int">
<column name="STAFFID"/>
</key-property>
</composite-id>
</class>
</hibernate-mapping>
and Module.HBM
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 26, 2010 9:50:23 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="Hibernate.Module" schema="WALK" table="MODULE">
<id name="id" type="int">
<column name="ID"/>
<generator class="assigned"/>
</id>
<property name="modulename" type="string">
<column length="50" name="MODULENAME"/>
</property>
<property name="teacherid" type="int">
<column name="TEACHERID" not-null="true"/>
</property>
</class>

Related

Hibernate mapping - The content of element type "class" must match "(meta*,subselect

I've a problem with the mapping of a class on hibernate...
I've already checked on the web, but i don't find the same problem
This is the error:
The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|
composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-
component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-
subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)".
and this is my code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="it.****.extractor.application.DGEPRA" table="T_DGEPRA_LOG">
<id name="id" type="int" column="ID"/>
<property name="time" column="TIME" type="timestamp"/>
<property name="function" column="FUNCTION" type="string"/>
<property name="executionTime" column="EXECUTION_TIME" type="int"/>
<property name="guId" column="GUID" type="string"/>
</class>
</hibernate-mapping>
any idea?

Hibernate mapping xml one-to-one (many-to-one)

I'm trying to map a relationship between two classes which have a one-to-one relationship. After looking up on the internet it seems like people prefer to map it using many-to-one.
For example have have a class Order and a class Bill. Bill holds a FK to the invoice.
Here is my mapping for Bill:
<?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 Mar 21, 2016 10:46:20 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="domain.Bill" table="BILL">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<many-to-one name="order" class="domain.Order" column="ORDER_ID" unique="true" not-null="true"/>
</class>
</hibernate-mapping>
As you can see above, in my mapping of Bill I can specify the column of Fk to the Order, but what should I put in my mapping for Order since it does not have a Fk to Bill?
<?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 Mar 21, 2016 10:46:20 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="domain.Order" table="ORDER">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<many-to-one name="bill" class="domain.Bill" ???? fetch="select"/>
</class>
</hibernate-mapping>
You should try to map it as it is: one-to-one.
The only reason that I am aware of why people may recommend many-to-one is because of the lazy loading issues on the inverse side of the one-to-one associations. Then you probably want a fake one-to-many association on the inverse side (after all it's the only logical inverse side of many-to-one).
However, take a look at this answer for different alternatives.

What is wrong with this code that saves value objects?

I have two entities and two value objects - Employee, Card, Employee Number & Card Number. The relationship between Employee and Card is a one-to-many. I create an instance of Employee and an instance of Card like so and save them to the database...
EmployeeRepositoryHibernate employeeRepository = new EmployeeRepositoryHibernate();
employeeRepository.setSessionFactory();
employeeRepository.getSession().beginTransaction();
EmployeeNumber employeeNumber = new EmployeeNumber("MNO");
Location location = new Location("Room 1");
CardNumber cardNumber = new CardNumber("1");
Employee employee = new Employee(employeeNumber, location);
Card card = new Card(cardNumber, "1111", employee);
employeeRepository.getSession().save(employee);
employeeRepository.getSession().save(card);
employeeRepository.getSession().getTransaction().commit();
employeeRepository.getSession().close();
Except, it won't save, the following error message is shown... I can save an employee, but the message is thrown when I try to save a related card... the mysql database isn't relational yet.. both tables are separate...
Problem fixed: required related tables.
Caused by: java.sql.SQLException: Field 'employeeNumber' doesn't have a default value
Here are the two Hibernate XML mapping files for Card and Employee...
Card
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
<class name="model.Card" table="Card">
<id name="CardID" type="long">
<column name="CardID" />
<generator class="identity" />
</id>
<component name="cardNumber" unique="true">
<property name="number" column="cardNumber"/>
</component>
<many-to-one name="employee" class="model.Employee" fetch="select">
<column name="EmpID" not-null="true"></column>
</many-to-one>
<property name="PIN" column="PIN"/>
</class>
</hibernate-mapping>
Employee
<?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 default-access="field">
<class name="model.Employee" table="employee">
<id name="EmpID" column="EmpID">
<generator class="org.hibernate.id.IdentityGenerator"/>
</id>
<component name="employeeNumber" class="model.EmployeeNumber" >
<property name="number" column="employeeNumber" type="string"/>
</component>
<component name="location">
<property name="location" column="Location" type="string"/>
</component>
<set name="cards" inverse="true" cascade="all">
<key>
<column name="EmpID" not-null="true"></column>
</key>
<one-to-many class="model.Card"/>
</set>
</class>
</hibernate-mapping>
One of these might help:
Add a default value to the column employeeNumber
ALTER TABLE 'table_name' ALTER 'employeeNumber' SET DEFAULT NULL
Use auto increment if you are using employeeNumber as a primary key.
Supply value to the employeeNumber column during insertion.

Mapping Exception in hibernate

I'm trying to implement one-to-one association mapping but it gives MappingException
Student.hbm.xml file:
<?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 Nov, 2012 3:10:16 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.sst.student.Student" table="STUDENT">
<id name="studentId" type="java.lang.Long">
<column name="STUDENT_ID" />
<generator class="native" />
</id>
<property name="studentName" type="string" length="20" column="STUDENT_NAME"/>
<many-to-one name="studentAddress"
column="STUDENT_ADDRESS" class="com.sst.student.Address" cascade="all"
unique="true"/>
</class>
</hibernate-mapping>
my Address.hbm.xml file:
<?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 Nov, 2012 3:10:16 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.sst.student.Address" table="ADDRESS">
<id name="addressId" type="java.lang.Long">
<column name="ADDRESS_ID" />
<generator class="native" />
</id>
<property name="street" type="string" column="STREET" length="20"/>
<property name="city" type="string" column="CITY" length="20"/>
<property name="state" type="string" column="STATE" length="20"/>
<property name="country" type="string" column="COUNTRY" length="20"/>
</class>
</hibernate-mapping>
at the time of code generation I'm getting the following exception:
org.hibernate.MappingException: An association from the table STUDENT refers to an
unmapped class: com.sst.student.Address
my cfg file "one2one.cfg.xml":
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root$12</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306
/mysql</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="com/sst/student/Student.hbm.xml"/>
<mapping resource="com/sst/student/Address.hbm.xml"/>
</session-factory>
</hibernate-configuration>
You need a one-to-many mapping in Address hbm.xml.

Why my join table is empty? (Using Hibernate mapping many-to-many)

I'm trying to do many-to-many mapping, but have a problem with a code below. Tables created with this code looks fine, foreign keys are added properly. The problem occurs when I'm trying to create related objects and save them to db. After lunching app I have one row in table KURS, one row in table KATEGORIE, but the third table - KURSY_KATEGORIE is empty :(
Table1:
<?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 2012-11-08 11:48:42 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="model.Kurs" table="KURS" node="kurs">
<id name="id" type="long">
<column name="ID_KURSU" />
<generator class="native" />
</id>
...
<set cascade="save-update" name="kategorie" inverse="true" lazy="true" table="KURSY_KATEGORIE">
<key foreign-key="FK_KATEGORIE_KURSY">
<column name="ID_KURSU" />
</key>
<many-to-many class="model.Kategoria" column="ID_KATEGORI"/>
</set>
</class>
</hibernate-mapping>
Table2:
<?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 2012-11-08 11:48:42 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="model.Kategoria" table="KATEGORIA" node="kategoria">
<id name="id" type="long">
<column name="ID_KATEGORI" />
<generator class="native" />
</id>
...
<set cascade="save-update" name="kursy" table="KURSY_KATEGORIE" inverse="true" lazy="true">
<key foreign-key="FK_KURSY_KATEGORIE">
<column name="ID_KATEGORI" />
</key>
<many-to-many class="model.Kurs" column="ID_KURSU"/>
</set>
</class>
</hibernate-mapping>
And .java file:
Session session = SESSION_FACTORY.openSession();
Transaction tx = session.beginTransaction();
GregorianCalendar Data_rozp = new GregorianCalendar();
Kategoria cat1 = new Kategoria("Kategoria1");
Set<Kategoria> kategorie = new HashSet<Kategoria>(3);
kategorie.add(cat1);
Kurs k1 = new Kurs(Data_rozp, "kurs1", 100);
Set<Kurs> kursy = new HashSet<Kurs>(1);
kursy.add(k1);
cat1.setKursy(kursy);
k1.setKategorie(kategorie);
session.save(cat1);
session.save(k1);
tx.commit();
session.close();
Try changing:
<set cascade="save-update" name="kursy" table="KURSY_KATEGORIE" inverse="true" lazy="true">
To:
<set cascade="save-update" name="kursy" table="KURSY_KATEGORIE" lazy="true">
And:
<set cascade="save-update" name="kategorie" inverse="true" lazy="true" table="KURSY_KATEGORIE">
To:
<set name="kategorie" inverse="true" lazy="true" table="KURSY_KATEGORIE">
I'm surprised that hibernate does not throw an exception for invalid configuration (both ends can't be set as inverse - one must be the owner)

Categories