Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java反射映射包含其他对象的对象_Java_Mysql_Reflection_Orm - Fatal编程技术网

使用java反射映射包含其他对象的对象

使用java反射映射包含其他对象的对象,java,mysql,reflection,orm,Java,Mysql,Reflection,Orm,我尝试映射数据库中的对象。 它适用于简单类型,如int、string等。。。但是,我遇到了一个关于包含其他对象的类的问题 例如,我得到了一个类订单: public class order { int id; Client c; public int getId(); public void setId(int id); } 和类客户端: public class Client { int id; //some stuff like name ect and getter/se

我尝试映射数据库中的对象。 它适用于简单类型,如int、string等。。。但是,我遇到了一个关于包含其他对象的类的问题

例如,我得到了一个类订单:

public class order
{
  int id;
  Client c;

 public int getId();
 public void setId(int id);
}
和类客户端:

public class Client
{
 int id;
 //some stuff like name ect and getter/setter...
}
我想通过获取客户id来映射订单

问题是,当我尝试获取field Client的包名时,它会返回java.lang(field类的包…)

我不知道为什么它不返回类型Client

有线索吗

这是代码:

   public void insertObject(Object instance) throws SQLException,
        SecurityException, IllegalArgumentException,
        InstantiationException, IllegalAccessException,
        IntrospectionException, InvocationTargetException
{
    Connection connection = null;
    PreparedStatement preparedStatement = null;

    this.type = instance.getClass();
    this.query = createInsertQuery();
    try
    {
        try 
        {
            connection = this.getConnexion();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        preparedStatement = connection.prepareStatement(query);
        int i = 0;
        Class clazz;
        for (Field field : type.getDeclaredFields())
        {
            System.out.println("PACKAGE NAME : " + field.getType().getClass().getPackage().getName());

            if (field.getType().getClass().getPackage().getName().compareTo("fr.javatp.model") == 0)
                clazz = field.getType();
            PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
                    field.getName(), type);
            Method method = propertyDescriptor.getReadMethod();
            Object value = method.invoke(instance);
            preparedStatement.setObject(++i, value);
        }
        preparedStatement.addBatch();
        preparedStatement.executeBatch();
    }
    finally
    {
        connection.close();
        preparedStatement.close();
    }

}
并创建插入查询代码:

私有字符串createInsertQuery() {

并获取Col代码:

 private String getCol(boolean usePlaceHolders)
{
    StringBuilder sb = new StringBuilder();
    boolean first = true;

    for (Field f : this.type.getDeclaredFields())
    {
//      System.out.println("PACKAGE NAME : " + f.getType().getClass().getName());

        if (first)
            first = false;
        else
            sb.append(",");

        if (usePlaceHolders)
            sb.append("?");
        else if (f.getType().getClass().getPackage().getName().compareTo("fr") == 0)
            {
                sb.append(f.getName() + ".id");
            }
        else
            sb.append(f.getName());
    }

    return sb.toString();
}

问题在于这个表达式:

field.getType().getClass().getPackage()
Field.getType()返回您感兴趣的字段的Class对象。通过对其调用getClass(),您将获得Class的Class对象,即java.lang.Class.Class。因此,只需使用

field.getType().getPackage()

您是否考虑过使用现有的ORM而不是重新发明轮子?

我们需要代码来重现这一点。提供MVCE应该不难。我通过删除getClass获得了正确的包名,但仍然无法访问客户端类的getId方法
field.getType().getClass().getPackage()
field.getType().getPackage()