@Entity 类中取得被映射成主键的属性

EM265 2009-09-12 11:22:56

package com.feinno.model.user;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "USER_INFO", schema = "CDB")
public class User implements java.io.Serializable {

private Long id;
private String username;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="ID")
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}

@Column(name = "USERNAME", length = 20)
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
}

在上面的这样一个实体类中,我想取得被映射成主键的是哪个属性? 请各位大侠赐教 ,谢谢
...全文
280 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
上面有个方法写错了一个地方,订正一下:


    private static boolean isPrimaryKeyProperty(PropertyDescriptor pd) {
Method getter = pd.getReadMethod();
if(getter != null && getter.isAnnotationPresent(Id.class)) {
return true;
}
Method setter = pd.getWriteMethod(); // 这里在上一楼写错了
if(setter != null && setter.isAnnotationPresent(Id.class)) {
return true;
}
return false;
}
  • 打赏
  • 举报
回复
通过 JavaBeans 规范进行检查:

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.persistence.Entity;
import javax.persistence.Id;

public class Test {

public static void main(String[] args) throws Exception {
PropertyDescriptor pd = getPrimaryKeyPropertyDescriptor(User.class);
System.out.println(pd.getDisplayName());
}

public static PropertyDescriptor getPrimaryKeyPropertyDescriptor(Class<?> clazz) {
if(clazz == null) {
return null;
}
if(!clazz.isAnnotationPresent(Entity.class)) {
throw new IllegalArgumentException("is not Entity");
}
try {
BeanInfo beanInfo = Introspector.getBeanInfo(clazz, Object.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
for(int i = 0; i < pds.length; i++) {
PropertyDescriptor pd = pds[i];

// Id 标注在方法上
if(isPrimaryKeyProperty(pd)) {
return pd;
}

// Id 标注在字段上
if(isPrimaryKeyField(pd, clazz)) {
return pd;
}
}
}catch(Exception e) {
e.printStackTrace();
}
return null;
}

private static boolean isPrimaryKeyProperty(PropertyDescriptor pd) {
Method getter = pd.getReadMethod();
if(getter != null && getter.isAnnotationPresent(Id.class)) {
return true;
}
Method setter = pd.getReadMethod();
if(setter != null && setter.isAnnotationPresent(Id.class)) {
return true;
}
return false;
}

private static boolean isPrimaryKeyField(PropertyDescriptor pd, Class<?> clazz)
throws Exception {
Field field = clazz.getDeclaredField(pd.getName());
if(field.isAnnotationPresent(Id.class)) {
return true;
}
return false;
}
}
Leiffort 2009-09-13
  • 打赏
  • 举报
回复
不知道你说的是什么意思啊?
如果你用的是hibernate自动生成的,看下映射文件啊 、、、如果不是的,看下数据库id是否逐渐
EM265 2009-09-13
  • 打赏
  • 举报
回复
bao110908 谢谢你了! 要的就是这种效果 !
TzSword 2009-09-12
  • 打赏
  • 举报
回复
这个代码看不出来的,
只看到id 是自增列而不一定是主键列,....
一般在配置文件里看
gesanri 2009-09-12
  • 打赏
  • 举报
回复
什么意思?通过编程取得?你这就是一个简单的javabean怎么取,一般主键都是id
windforcecn 2009-09-12
  • 打赏
  • 举报
回复
不是id吗?

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧