JPA实体下拉列 [英] JPA Entity for Drop Down columns

查看:116
本文介绍了JPA实体下拉列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Hibernate 4.1.0中使用JPA 2.0,我在实体类中有以下内容:

 @Entity 
@Table(name =PRODUCTS)
public class Product {

private String productNo;
private String productDesc;
私人日期deliveryDate;
私人日期invoiceDate;
私人整数productCurrencyId;
私人字符串productCurrency;

以上内容将用于显示记录以及CRUD操作的应用程序。



从上面的 productCurrencyId productCurrency 将用于应用程序中下拉选择货币字段



我想知道将这两个放在单独的实体类中还是使用相同的产品类?为什么我问这是否在相同的产品类中的问题如何查询我的货币下拉列的值?



我知道我可以在Product类中使用 @NamedQueries 调用 @NamedQuery如果我想要使用 @NamedQuery(name > DAOImpl )来检索下拉记录。 c $ c>然后我有硬编码的SQL语句吗?有没有什么办法可以使用 @NamedQuery(name 或其他一些机制来获取货币记录下拉我没有使用单独的实体类来实现下拉值?对于这种情况,最好的方法是什么?



任何帮助都是非常可观的。



谢谢

编辑1

我想问关于实体类是什么,如果我有一些其他字段与下拉列,我应该为所有这些下拉字段创建单独的实体类?

解决方案

您想在哪里储存货币清单?Carsten解释说,他们可以有自己的标签(因此它是自己的实体),您将在产品和货币之间建立@OneToOne关系:

  @Entity 
public class Product
{
...
private String productNo;
private String productDesc;
私人日期deliveryDate;
私人日期invoiceDate;

@OneToOne
@JoinColumn(name =currencyId)
专用货币货币;
...
}

@实体
公共类货币
{
@Id
private int id;

私人字符串货币;
}

然后填充下拉列表,您只需要从货币中检索所有记录table:

  TypedQuery< Currency> query = entityManager.createQuery(SELECT c FROM Currency c); 
列表<货币> query.getResultList();

这很方便,因为您只需添加货币即可将记录添加到货币表格和下拉菜单将显示正确的值。



如果您的目录不太可能更改,而不是数据库中的表,您可以使用枚举:

  @Entity 
public class Person
{
...
public enum性别{male,female};

@Enumerated(EnumType.STRING)
私人性别性别;
}

并填写下拉菜单:

  Person.Gender.values(); 

所以基本上它取决于目录是否可能发生变化以及您希望如何处理这些更改。

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?

解决方案

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.

这篇关于JPA实体下拉列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆