【问题标题】:Map Object containing an other object using java reflection使用 java 反射映射包含另一个对象的对象
【发布时间】:2014-01-16 11:05:27
【问题描述】:

我尝试在我的数据库中映射一个对象。 它适用于 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 来映射订单。

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

field.getType().getClass().getPackage().getName().compareTo("fr.javatp.model") == 0

不知道为什么不返回类型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() {

    StringBuilder sb = new StringBuilder();
    sb.append("INSERT INTO ");
    sb.append(type.getSimpleName());
    sb.append("(");
    sb.append(this.getCol(false));
    sb.append(")");
    sb.append(" VALUES (");
    sb.append(this.getCol(true));
    sb.append(")");
    System.out.println("QUERY TO SEND : " + sb.toString());
    return sb.toString();
}

并获取 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();
}

【问题讨论】:

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

标签: java mysql reflection orm


【解决方案1】:

问题是这个表达式:

field.getType().getClass().getPackage()

Field.getType() 返回你感兴趣的字段的 Class 对象。通过调用 getClass() 得到 Class 的类对象,即 java.lang.Class.class。因此,只需使用

field.getType().getPackage()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2021-11-29
    • 1970-01-01
    • 2017-03-16
    相关资源
    最近更新 更多