I have one class and 2 tables. 1. Items. The first table is for storing and display the items to the user and from this table the user choose item.(I want to read the items from a file).
2.oredered_items : Here i want to save the items the user chose.
The tables has the same columns and data types.
public class Item {
private Integer id;
private String name;
private Integer price;
}
XML mapping :
<?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">
<hibernate-mapping>
<class name="entities.Item" table="______________">
<id name= "id" column="item_id" type="integer">
</id>
<property name="name" column="item_name" type="string"/>
<property name="price" column="item_price" type="integer"/>
</class>
</hibernate-mapping>
This method for reading from a file and insert to the database.
public void insertItems()
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tr = session.beginTransaction();
Item currentItem;
String str;
String dataString[];
try {
File fileForReading = new File(PATH_FILE);
FileReader fr = new FileReader(fileForReading);
BufferedReader br = new BufferedReader(fr);
while((str = br.readLine()) != null)
{
dataString = str.split(" ");
Integer id = Integer.parseInt(dataString[INDEX_ID]);
String name = dataString[INDEX_NAME];
Integer price = Integer.parseInt(dataString[INDEX_PRICE]);
currentItem = new Item(id,name,price);
session.persist(currentItem);
}
tr.commit();
session.close();
This method read from the table Items and display to the user.
public void displayItems()
How can i do it?
I want to use the table items only for insert from a file and to display to the user the items , and when he choose what he wants it will store the item in the second table.
Can i tell hibernate : "This method will map to this table , and this method will map to this table" ?
Maybe to create 2 classes?