Reputation: 37
Currently I have two tables - Product and Statistics. And two java enitities (Product, Statistics)
Mapping between them is 1:1.
I would like to get rid of one entity(Statitics), because from this entity I need only one column called "magicValue".
Is there any way how to use only one Entity - Product, which contains magicValue?
(without need of Statistics class?)
I was thinking about OneToOne mapping or Embedded type usage, but for all of these possibilities, there is need of Statistics java class
If my question is not reasonable, please write me what is best solution according your knowledge.
From table Product I need all columns.
From table Statistics I need only one column (MAGIC_VALUE)
Sql tables:
CREATE TABLE PRODUCT
(
"PRODUCT_ID" NUMBER(20,0),
"NAME" VARCHAR2(30 BYTE),
PRIMARY KEY (`PRODUCT_ID`)
);
CREATE TABLE STATISTICS
(
"STATISTICS_ID" NUMBER(20,0) PRIMARY KEY,
"PRODUCT_ID_FK" NUMBER(20,0),
"MAGIC_VALUE" VARCHAR2(30 BYTE),
"OTHER_USELESS_COLUMN" VARCHAR2(30 BYTE),
...
PRIMARY KEY (`STATISTICS_ID`)
FOREIGN KEY (`PRODUCT_ID_FK`) REFERENCES PRODUCT(PRODUCT_ID)
);
Current java classes looks like:
@Entity
@Table(name="PRODUCT")
class Product {
@Id
@Column(name = "PRODUCT_ID")
protected Integer productId;
@Column(name = "NAME")
protected String name;
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
protected Statistics stat;
}
This is Statistics java class
I need only one column (magicValue)
@Entity
@Table(name="STATISTICS")
class Statistics {
@Id
@Column(name = "STATISTICS_ID")
protected Integer statisticsId;
@OneToOne(mappedBy = "statistics")
protected Product product;
@Column(name = "MAGIC_VALUE")
protected String magicValue; // <-- only this is needed
@Column(name = "OTHER_USELESS_COLUMN")
protected String uselessColumn;
...
}
change only Product java class like:
@Entity
@Table(name="PRODUCT")
class Product {
@Id
@Column(name = "PRODUCT_ID")
protected Integer productId;
@Column(name = "NAME")
protected String name;
@someHowMap to sql table Statistics, but in Product have direct magicValue value
protected String magicValue;
}
Thank you
Upvotes: 1
Views: 103
Reputation: 3189
@Entity
@Table(name="PRODUCT")
class Product {
@Id
@Column(name = "PRODUCT_ID")
protected Integer productId;
@Column(name = "NAME")
protected String name;
@Formula("(select MAGIC_VALUE from STATISTICS where PRODUCT_ID_FK = PRODUCT_ID)")
protected String magicValue;
}
That formula will work, it uses SQL instead of HQL, so shouldn't need the entity for STATISTICS
in your Java classes.
You can see more details about the usage of @Formula
here
Upvotes: 1