[問題] 關於 reflection 問題
各位前輩好
小弟目前有個問題,目前我有一個作法是想自動產生 HQL 的語法
但是卡在 reflection 的作法,所以想上來請教一下大家,
請問要如何將 id 部份的 fields 和 values 取出
這部份不太知道要如何做,請前輩們幫解題一下,謝謝
以下為程式碼
User.java
---------------------------------------------------------
@Entity
@Table(name = "user")
public class User implements Serializable {
@EmbeddedId
UserId id;
@NotEmpty
@Column(name = "user_name", nullable = false)
private String userName;
@NotEmpty
@Column(name = "password", nullable = false)
private String password;
@Column(name = "status")
private String status;
@Column(name = "email", nullable = false)
private String email;
@NotEmpty
@Column(name = "registerTime", nullable = false)
private String registerTime;
@NotEmpty
@Column(name = "lastActiveTime", nullable = false)
private String lastActiveTime;
.......getters setters
============================================================
UserId.java
------------------------------------------------------------
@Embeddable
public class UserId implements Serializable {
@Column(name = "user_Id")
public Integer userId;
@Column(name = "account")
public String account;
.......getters setters
=============================================================
Test.java
-------------------------------------------------------------
public class Test {
public static void main(String[] args) throws Exception {
User user = new User();
UserId id = new UserId();
user.setEmail("weqqr");
user.setStatus("2");
user.setPassword("eeeeee");
id.setAccount("wwwwwerrr");
id.setUserId(2244);
user.setId(id);
Test test = new Test();
System.out.println(test.genUpdateHql(user, null, null));
}
private String genUpdateHql(Object entity, Map<String, Object> argMap,
String condition) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(entity.getClass());
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
String entityName = entity.getClass().getSimpleName();
StringBuilder hqlStr = new StringBuilder();
hqlStr.append("UPDATE " + entityName + " " + entityName + " SET ");
for (int i = 0; i < descriptors.length; i++) {
String propName = descriptors[i].getName();
Class<?> propType = descriptors[i].getPropertyType();
Object value = PropertyUtils.getProperty(entity, propName);
if (!propName.equals("class") && null != value) {
if (propType.getSimpleName().equals("Integer") ||
propType.getSimpleName().equals("int")) {
hqlStr.append(entityName + "." + propName + "=" + value + "");
} else if (propType.getSimpleName().equals("String")) {
hqlStr.append(entityName + "." + propName + "=" + "'" + value + "'");
} else if (propName.equals("id")) {
}
hqlStr.append(",");
}
}
hqlStr = hqlStr.deleteCharAt(hqlStr.length() - 1);
hqlStr = hqlStr.append(" WHERE User.id.userId=:userId AND
User.id.account=:account");
return hqlStr.toString();
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.218.51.133
※ 文章網址: https://www.ptt.cc/bbs/java/M.1479196850.A.57B.html
→
11/15 17:21, , 1F
11/15 17:21, 1F
→
11/15 18:02, , 2F
11/15 18:02, 2F
→
11/16 11:08, , 3F
11/16 11:08, 3F
java 近期熱門文章
PTT數位生活區 即時熱門文章