weixin_37467879
weixin_37467879
2017-02-07 02:41

求助:hibernate联合主键 (注解),测试时一直报null

5
  • 测试
  • hibernate

@Entity
@Table(name="SysRoleResource")
@IdClass(value=RoleResourcePK.class)
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler"})
public class RoleResource implements Serializable{
private static final long serialVersionUID = 6109494575468275047L;
private Integer RoleId;
private Integer ResourceId;
@Id
@Column(length=10,name="RoleId")
public Integer getRoleId() {
return RoleId;
}
public void setRoleId(Integer roleId) {
RoleId = roleId;
}
@Id
@Column(length=10,name="ResourceId")
public Integer getResourceId() {
return ResourceId;
}
public void setResourceId(Integer resourceId) {
ResourceId = resourceId;
}
@Override
public String toString() {
return "RoleResource [RoleId=" + RoleId + ", ResourceId=" + ResourceId
+ "]";
}
}//实体类

//主键类
public class RoleResourcePK implements Serializable{

private static final long serialVersionUID = -3149726451195304787L;
private Integer RoleId;
private Integer ResourceId;
public Integer getRoleId() {
    return RoleId;
}
public void setRoleId(Integer roleId) {
    RoleId = roleId;
}
public Integer getResourceId() {
    return ResourceId;
}
public void setResourceId(Integer resourceId) {
    ResourceId = resourceId;
}
@Override
public String toString() {
    return "RoleResourcePK [RoleId=" + RoleId + ", ResourceId="
            + ResourceId + "]";
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((ResourceId == null) ? 0 : ResourceId.hashCode());
    result = prime * result + ((RoleId == null) ? 0 : RoleId.hashCode());
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    RoleResourcePK other = (RoleResourcePK) obj;
    if (ResourceId == null) {
        if (other.ResourceId != null)
            return false;
    } else if (!ResourceId.equals(other.ResourceId))
        return false;
    if (RoleId == null) {
        if (other.RoleId != null)
            return false;
    } else if (!RoleId.equals(other.RoleId))
        return false;
    return true;
}

}
//测试
@Test
public void add(){
RoleResource rr=new RoleResource();
rr.setResourceId(2);
rr.setRoleId(1);
try {
rs.save(rr);
System.out.println("添加成功"+JSON.toJSONString(rr));
} catch (Exception e) {
System.out.println("添加失败"+e.getMessage());
}
}
测试时一直报null
org.springframework.orm.hibernate4.HibernateTransactionManager.afterPropertiesSet(360) | Using DataSource [com.mchange.v2.c3p0.ComboPooledDataSource[ identityToken -> 1hgeest9m680zit10ue8ko|72534f6f, dataSourceName -> 1hgeest9m680zit10ue8ko|72534f6f ]] of Hibernate SessionFactory for HibernateTransactionManager
null
添加失败null

这是我的数据源配置及 sessionFactory配置

destroy-method="close">

<!-- 请求超时时间 -->
<property name="checkoutTimeout" value="30000" />
<!-- 每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 -->
<property name="idleConnectionTestPeriod" value="30" />
<!-- 连接数据库连接池最大空闲时间 -->
<property name="maxIdleTime" value="30" />
<!-- 连接池初始化连接数 -->
<property name="initialPoolSize" value="5" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 -->
<property name="acquireIncrement" value="5" />

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入数据源 相关信息看源码 -->

<!-- hibernate配置信息 -->


${hibernate.dialect}
${hibernate.show_sql}
${hibernate.format_sql}
${hibernate.hbm2ddl.auto}
true

        <!-- 开启二级缓存 ehcache -->
        <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
        <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
        <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
        <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.cache.provider_configuration_file_resource_path}
        </prop>
    </props>
</property>
<!-- 扫描hibernate注解配置的entity -->
<property name="packagesToScan" value="cn.com.topsoft.entity" />

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答