Java JPA:查询以根据实体类中定义的外键值获取结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4180115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:23:55  来源:igfitidea点击:

JPA: query to get results based on value of foreign key defined in the entity class?

javajpaentity

提问by Sajee

Netbean 6.9 generated the following JPA entity class from this SQL Server 2008 table: alt text

Netbean 6.9 从此 SQL Server 2008 表生成了以下 JPA 实体类: 替代文字

I want to get all the ProductDescriptors that have a specific SKU value. Something like this:

我想获取所有具有特定 SKU 值的 ProductDescriptor。像这样的东西:

SELECT * FROM ProductDescriptors WHERE SKU='something'

Given the entity class, what's the Java code to get the results?

给定实体类,获取结果的 Java 代码是什么?

Thanks.

谢谢。

@Entity
@Table(name = "ProductDescriptors")
@NamedQueries({
    @NamedQuery(name = "ProductDescriptors.findAll",     query = "SELECT p FROM ProductDescriptors p"),
    @NamedQuery(name = "ProductDescriptors.findByDescriptorID", query = "SELECT p FROM ProductDescriptors p WHERE p.descriptorID = :descriptorID"),
    @NamedQuery(name = "ProductDescriptors.findByLanguageCode", query = "SELECT p FROM ProductDescriptors p WHERE p.languageCode = :languageCode"),
    @NamedQuery(name = "ProductDescriptors.findByTitle", query = "SELECT p FROM ProductDescriptors p WHERE p.title = :title"),
    @NamedQuery(name = "ProductDescriptors.findByIsDefault", query = "SELECT p FROM ProductDescriptors p WHERE p.isDefault = :isDefault"),
    @NamedQuery(name = "ProductDescriptors.findByBody", query = "SELECT p FROM ProductDescriptors p WHERE p.body = :body")})
public class ProductDescriptors implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "DescriptorID")
    private Integer descriptorID;
    @Basic(optional = false)
    @Column(name = "LanguageCode")
    private String languageCode;
    @Basic(optional = false)
    @Column(name = "Title")
    private String title;
    @Basic(optional = false)
    @Column(name = "IsDefault")
    private boolean isDefault;
    @Basic(optional = false)
    @Column(name = "Body")
    private String body;
    @JoinColumn(name = "SKU", referencedColumnName = "SKU")
    @ManyToOne(optional = false)
    private Products products;

...

...

采纳答案by javamonkey79

Something like this:

像这样的东西:

@PersistenceContext( unitName = "youPersistenceUnitHere" )
private EntityManager _entityManager;

public List<ProductDescriptors> getProductDescriptorsBySku( String sku ) {
   Query query = _entityManager.createQuery( "Select ProductDescriptors from ProductDescriptors pd where pd.products.sku = ?1" );
   query.setParameter( 1, sku );
   return new ArrayList<ProductDescriptors>( query.getResultList() );
}