问题
I am using JPA 2.0 with Hibernate 4.1.0, I have the following in my Entity
class
@Entity
@Table(name = "PRODUCTS")
public class Product {
private String productNo;
private String productDesc;
private Date deliveryDate;
private Date invoiceDate;
private Integer productCurrencyId;
private String productCurrency;
The above will be used in application for displaying records as well as for CRUD operation.
From the above productCurrencyId
and productCurrency
will be used in the application for drop down fields for selection of currency
I would like to know whether it is better to have those two in a separate Entity class or use in same Product
class? Reason why I asked this if it is in same Product
class how can I query the values for my drop down column for currency?
I am aware that I can use @NamedQueries
in Product class call @NamedQuery(name
in DAOImpl
to retrieve the records for drop down. If I would want to use @NamedQuery(name
then I have hard code a sql statement right? Are there any ways I to use @NamedQuery(name
or some other mechanism to fetch records for currency drop down if I am not using a separate Entity class for drop down values? What is the best approach for such scenarios?
Any help is highly appreciable.
Thanks
Edit 1
Another question I would like to ask in relation to Entity class is what if I have a few other fields with drop down columns, should I create separate Entity class for all those drop down fields?
回答1:
Where do you want to store the list of currencies? as Carsten explained they can have their own table in the database (and therefore its own entity) and you will have a @OneToOne relationship between the Product and the Currency:
@Entity
public class Product
{
...
private String productNo;
private String productDesc;
private Date deliveryDate;
private Date invoiceDate;
@OneToOne
@JoinColumn(name="currencyId")
private Currency currency;
...
}
@Entity
public class Currency
{
@Id
private int id;
private String currency;
}
Then to populate the dropdown you just need to retrieve all the records from the Currency table:
TypedQuery<Currency> query = entityManager.createQuery("SELECT c FROM Currency c");
List<Currency> query.getResultList();
This is convenient because to add currencies you just need to add a record to the Currency table and the dropdown will show the right values.
If your catalog is unlikely to change, instead of a table in the database you could use an enum:
@Entity
public class Person
{
...
public enum Gender {male, female};
@Enumerated(EnumType.STRING)
private Gender gender;
}
And to populate the dropdown:
Person.Gender.values();
So basically it depends if the catalogs are likely to change and how do you want to handle those changes.
回答2:
The technical implementation aside I would make an own entity for the currency since it is a business object of its own. And changes to the currencies would not need to be applied to every product. But the personal preference to make it a own entity aside I can't give any real adive on the underlying technical question. :(
来源:https://stackoverflow.com/questions/16934174/jpa-entity-for-drop-down-columns