a).I have a Trade object which can have multiple status's.
One-to-many relation
Code:
-----------------------------------------------------
Trade TradeDetails
------ ------------
ID ID STATUS TRADE_ID(Foreign Key)
--- -----------------------------------
1 1 New 1
2 Verified 1
3 Executed 1
-----------------------------------------------------
Create table trade(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id)
);
CREATE TABLE tradedetails(
id INT NOT NULL AUTO_INCREMENT,
status VARCHAR(20),
trade_id INT NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(trade_id) REFERENCES trade(id)
);
b.Now,I have inserted a new record with a status of 'New' in the parent(Trade) and child (TradeDetails) tables.
This works fine.(Code is attached below).
c.My Query is:
Now 3 days later, I want to enter a new record in the child table with the same 'parent id'
and status set to 'Verified'.
How can I insert a new record only in the child table with the status set to 'Verified'
and then another with the status set to 'Executed' for the same parent?
i.e 1 Parent - 3 Children
My code to create a new record in Parent and child is below.
Code:
Trade.hbm.xml
-------------
<hibernate-mapping>
<class name="hibernate.Trade" table="TRADE">
<id name="id" column="ID">
<generator class="native"/>
</id>
<set name="statuses" cascade="all" inverse="true" lazy="true">
<key column="trade_id"/>
<one-to-many class="hibernate.TradeDetails"/>
</set>
</class>
</hibernate-mapping>
TradeDetails.hbm.xml
--------------------
<hibernate-mapping>
<class name="hibernate.TradeDetails" table="TRADEDETAILS">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="status"/>
<many-to-one name="trade" class="hibernate.Trade" column="trade_id" not-null="true"/>
</class>
</hibernate-mapping>
Hibernate,cfg.xml
-----------------
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">bhuru</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="Trade.hbm.xml"/>
<mapping resource="TradeDetails.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Java Code
--------
public class Trade {
private Long id;
private Set statuses = new HashSet();
// Constructor
public Trade(){}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public void setStatuses(Set l) {
statuses = l;
}
public Set getStatuses() {
return statuses;
}
}
TradeDetails.java
-----------------
public class TradeDetails {
private Long id;
private String status;
private Trade trade;
// Constructor
public TradeDetails(/*String status*/){
//this.status = status;
//this.trade = trade;
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public void setStatus(String status) {
this.status=status;
}
public String getStatus() {
return status;
}
public void setTrade(Trade trade){
this.trade = trade;
}
public Trade getTrade(){
return trade;
}
}
Test Harness
-----------
public class OneToMany_Test {
public static void main(String[] args) {
Configuration configuration = new Configuration();
SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// Parent.
Trade trade = new Trade();
// Child.
TradeDetails tDetails = new TradeDetails();
tDetails.setStatus("New");
tDetails.setTrade(trade);
// Create a sey of status
Set setOfStatus = new HashSet();
setOfStatus.add(tDetails);
// Add to Parent.
trade.setStatuses(setOfStatus);
// Save Parent.
session.save(trade);
// Commit Record.
transaction.commit();
} // end main()
} // end class