简体   繁体   English

如何在Java / MySQL中查询继承的类类型?

[英]How to I query for the inherited class type in Java/MySQL?

Item is an abstract class, which is implemented by both the Material and Product classes. Item是一个抽象类,由Material和Product两个类实现。

The Distribution entity has an attribute "item" which is of an Item type. 分发实体具有项目类型的属性“项目”。 How do I check if the "item" is of the Product type? 如何检查“项目”是否属于产品类型? Right now I'm using this but it doesn't seem to be optimal. 现在,我正在使用它,但它似乎并不是最佳选择。

"SELECT d FROM " + Distribution.class.getName() + " d, " + Product.class.getName() + " p WHERE d.item = p"

Product.class.getName() will return the fully qualified class name. Product.class.getName()将返回完全限定的类名称。

However, if you have something like : 但是,如果您有类似以下内容:

Item item1= new Product();          Then 
item1.getClass().getSimpleName(); 

will return Product as a String type. 将返回Product作为String类型。

So you can have : 因此,您可以:

if(item1.getClass().getSimpleName().equals("Product")) {
    // Do something like 
    Product product = (Product)item1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM