hibernate 中关联表,提取数据的问题
初学者,person表中的userpower关联Code表的id,我读取了person表中的内容,怎么从它读取code表对应的内容entity中person的主要代码pu...
初学者,person表中的userpower关联Code表的id,我读取了person表中的内容,怎么从它读取code表对应的内容
entity 中person的主要代码
public Person(Code codeByUserpower, Code codeByUsersection,
String username, String userpassword, String userage,
String usersex, String useraddress, String useremail,
String userphone, String userposition) {
this.codeByUserpower = codeByUserpower;
this.codeByUsersection = codeByUsersection;
this.username = username;
this.userpassword = userpassword;
this.userage = userage;
this.usersex = usersex;
this.useraddress = useraddress;
this.useremail = useremail;
this.userphone = userphone;
this.userposition = userposition;
}
我看有getCodeByUserPower的方法调用了后还是空。。
personDAO的主要代码
public static final String USERNAME = "username";
public static final String USERPASSWORD = "userpassword";
public static final String USERAGE = "userage";
public static final String USERSEX = "usersex";
public static final String USERADDRESS = "useraddress";
public static final String USEREMAIL = "useremail";
public static final String USERPHONE = "userphone";
public static final String USERPOSITION = "userposition";
public abstract void save(Person transientInstance);
public abstract void delete(Person persistentInstance);
public abstract Person findById(java.lang.Integer id);
public abstract List findByExample(Person instance);
public abstract List findByProperty(String propertyName, Object value);
public abstract List findByUsername(Object username);
....(下面是findyByUserpassword,findByUserage,findeByUsersex...)
其实就是子结点访问父结点,得到父结点的数据 展开
entity 中person的主要代码
public Person(Code codeByUserpower, Code codeByUsersection,
String username, String userpassword, String userage,
String usersex, String useraddress, String useremail,
String userphone, String userposition) {
this.codeByUserpower = codeByUserpower;
this.codeByUsersection = codeByUsersection;
this.username = username;
this.userpassword = userpassword;
this.userage = userage;
this.usersex = usersex;
this.useraddress = useraddress;
this.useremail = useremail;
this.userphone = userphone;
this.userposition = userposition;
}
我看有getCodeByUserPower的方法调用了后还是空。。
personDAO的主要代码
public static final String USERNAME = "username";
public static final String USERPASSWORD = "userpassword";
public static final String USERAGE = "userage";
public static final String USERSEX = "usersex";
public static final String USERADDRESS = "useraddress";
public static final String USEREMAIL = "useremail";
public static final String USERPHONE = "userphone";
public static final String USERPOSITION = "userposition";
public abstract void save(Person transientInstance);
public abstract void delete(Person persistentInstance);
public abstract Person findById(java.lang.Integer id);
public abstract List findByExample(Person instance);
public abstract List findByProperty(String propertyName, Object value);
public abstract List findByUsername(Object username);
....(下面是findyByUserpassword,findByUserage,findeByUsersex...)
其实就是子结点访问父结点,得到父结点的数据 展开
1个回答
展开全部
如果你用的是hibernate的话,就有可能是: 映射文件中有fetch属性,它有两个可选值:join和select
这个属性决定了你在查询的时候,是先查主表记录再查关联记录,还是要把关联表的记录一起查询出来。
select方式时先查询返回要查询的主体对象(列表),再根据关联外键id,每一个对象发一个select查询,获取关联的对象,形成n+1次查询
如果lazy=true(延迟加载),select在查询时只会查出主表记录,用到了关联数据时再自动在执行查询
而join方式,主体对象和关联对象用一句外键关联的sql同时查询出来,不会形成多次查询。
这个属性决定了你在查询的时候,是先查主表记录再查关联记录,还是要把关联表的记录一起查询出来。
select方式时先查询返回要查询的主体对象(列表),再根据关联外键id,每一个对象发一个select查询,获取关联的对象,形成n+1次查询
如果lazy=true(延迟加载),select在查询时只会查出主表记录,用到了关联数据时再自动在执行查询
而join方式,主体对象和关联对象用一句外键关联的sql同时查询出来,不会形成多次查询。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询