views:

202

answers:

3

I am importing stock quotes data using HibernateTemplate + SpringFramework

Format of the data

AAPL,09-Jun-2010 09:00,251.47,251.47,251.39,251.39,640
AAPL,09-Jun-2010 09:01,251.4,251.4,251.05,251.26,6844
INTC,09-Jun-2010 09:00,251.47,251.47,251.39,251.39,640
INTC,09-Jun-2010 09:01,251.4,251.4,251.05,251.26,6844
MSFT,09-Jun-2010 09:00,251.47,251.47,251.39,251.39,640
MSFT,09-Jun-2010 09:01,251.4,251.4,251.05,251.26,6844

I have one Entity class called Stock

@Entity
public class Stock implements Serializable {
public Stock() {}

private Long id;
private BigDecimal open;
    .... close,high, low..etc
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
    ....

My problem with this setup is that hibernate will put everything into a table called "Stock".

I want to separate different stock into a different table, so with the data above I will end up having 3 tables (AAPL, MSFT, INTC)

Is there an easy way to do this without resorting to hand-written sql statement in Hibernate/HibernateTemplate? sort of creating the table dynamically?

Environment: - Mysql, Hibernate3.5.3, springframework3.0

+1  A: 

Without having custom classes for each one, I don't know of an easy way to make these tables appropriately.

Having said that, I would encourage you to reconsider your design, and consider the usage of better OO concepts.

For instance, say you kept your Stock object/table, but then had another object such as DailyActivity component which contained a date, open, high, low, close, etc.

You could then specify your stock as having a collection of DailyActivity objects. This could then be mapped by Hibernate as a Stock to DailyActivity, one-to-many, relationship.

Then, if you wanted to evaluate how MSFT did it would become a relatively trivial query selecting the MSFT record from the Stock table, selecting its DailyActivity objects for a specified period of time.

apiri
@apiri so are you suggesting a big table to keep the Stock object. The data that will be there is minutes data from NASDAQ, and the table will be big in size, which is why I want a separate table for each stock. How would people out there deal with the data?
portoalet
The fact that the data was done on a per minute basis wasn't clear with the class as you had presented it. The attributes you do have are more daily values than periodic, intraday values. Are you talking about just updating a value field on a per minute basis or actually capturing the stock price every minute? If so, could you clarify your data model?
apiri
@apiri we basically try to import the minutes data from csv file, so this is not a real time operation. each row of the csv would have a date-time component accurate to the minute (have a look above, i.e. 09-Jun-2010 09:01 ). In the end, we do mostly read of the database, seldom write (read/write ratio is 10/1)
portoalet
@portoalet I think you may have to reconsider your desire to use Hibernate as this falls into the area where, in the long run, and in terms of scalability (should you have many stocks you wish to keep this data for), Hibernate isn't going to be as well suited. It seems as though your data model is simple enough that a JDBC implementation coupled with some triggers and stored procedures to create new data tables for each new stock ticker symbol that enters your core stock table might prove to be a better approach.
apiri
@apiri yeah I am having doubts too about Hibernate's scalability in the long run.
portoalet
A: 

I'm not sure this is recommended. The reference docs suggest the property is called hibernate.hbm2ddl.auto

There is some previous discussion at http://stackoverflow.com/questions/221379/hibernate-hbm2ddl-autoupdate-in-production and http://stackoverflow.com/questions/438146/hibernate-question-hbm2ddl-auto-possible-values-and-what-they-do which you should also look at

JoseK
+2  A: 

I want to separate different stock into a different table, so with the data above I will end up having 3 tables (AAPL, MSFT, INTC)

Then either introduce an inheritance hierarchy (with a JOINED or TABLE_PER_CLASS strategy) and persist the appropriate sub type or maybe use horizontal partitioning (this would require some testing though, I've never done that with MySQL and Hibernate).

Pascal Thivent