Hibernate runtime persist ?

M

mark

Let's say I have one object
class Cat{
private String name;
private int year;
}
and another

class Dog{
private BigDecimal cost;
}
and one time i sent Cat object as param to some persist function, and the
other time it is Dog, and i want to save it in the same table ? how can i do
that ?




__________ Information from ESET NOD32 Antivirus, version of virus signature database 4482 (20091005) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
D

Donkey Hottie

15.10.2009 10:37, mark kirjoitti:
Let's say I have one object
class Cat{
private String name;
private int year;
}
and another

class Dog{
private BigDecimal cost;
}
and one time i sent Cat object as param to some persist function, and the
other time it is Dog, and i want to save it in the same table ? how can i do
that ?

Those would be your value objects for business logic. You would need a
separate entity bean like

class Pet
{
private String kind ; // "DOG", "CAT"
private String name ; // null for Dogs; somehow dogs do not need a name
private int year ; // 0 for Dogs; dogs do not have a year.
private BigDecimal cost ; // null for Cats, cats are free.
}
 
M

mark

Thanks for the quick reply, but i don't know how many object(Animal) i'm
going to have... and I want to do it with final number of tables to save
this properties ? I saw in hibernate that there is something calles @any
annotation.. can i use this in my problem ?



Donkey Hottie said:
15.10.2009 10:37, mark kirjoitti:

Those would be your value objects for business logic. You would need a
separate entity bean like

class Pet
{
private String kind ; // "DOG", "CAT"
private String name ; // null for Dogs; somehow dogs do not need a
name
private int year ; // 0 for Dogs; dogs do not have a year.
private BigDecimal cost ; // null for Cats, cats are free.
}


__________ Information from ESET NOD32 Antivirus, version of virus
signature database 4482 (20091005) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4482 (20091005) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
D

Donkey Hottie

15.10.2009 11:15, mark kirjoitti:
Thanks for the quick reply, but i don't know how many object(Animal) i'm
going to have... and I want to do it with final number of tables to save
this properties ? I saw in hibernate that there is something calles @any
annotation.. can i use this in my problem ?

Sorry.. I do not know Hibernate, I try to stick with JPA only.

But you could make a table like

CREATE TABLE animal
(
java_class VARCHAR(500) ;
object CLOB ;
) ;

and serialize the Java object into the CLOB (or BLOB). One entity bean
would serve all your needs, but...
 
M

Marcin Rze¼nicki

15.10.2009 11:15, mark kirjoitti:


Sorry.. I do not know Hibernate, I try to stick with JPA only.

Hibernate is JPA compatible
But you could make a table like

CREATE TABLE animal
(
        java_class      VARCHAR(500) ;
        object          CLOB ;
) ;

and serialize the Java object into the CLOB (or BLOB). One entity bean
would serve all your needs, but...

Never do this !!!
 
M

Marcin Rze¼nicki

Let's say I have one object
class Cat{
private String name;
private int year;}

and another

class Dog{
private BigDecimal cost;}

and one time i sent Cat object as param to some persist function, and the
other time it is Dog, and i want to save it in the same table ? how can i do
that ?


You need to map inheritance onto db table. I suppose that what you
need is discriminator-based inheritance. Take a look:
http://docs.jboss.org/hibernate/sta...ml#inheritance-tablepersubclass-discriminator
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top