求高手看看哪里配错了,数据库设计如图:代码:@Entity
@Table(name = "TBL_MS_SQLMANAGED", schema = "UDEP3")
public class TblMsSqlmanaged implements java.io.Serializable { private Long sql_id;
private String name;
private String desced;
private String leveled;
private String sql;
private String alarmDesc;
private Date updatetime;
private String updateAuthor;
private Date createtime;
private String createAuthor; private Set<TblMsWatchdog> tblMsWatchdog;

private Set<TblMsSqlTheresholdLink> tblMsSqlTheresholdLink; public TblMsSqlmanaged() {
} @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "SQLID", unique = true, nullable = false, scale = 0)
public Long getSql_id() {
return sql_id;
} public void setSql_id(Long sqlId) {
sql_id = sqlId;
} @Column(name = "SQLNAME", length = 32)
public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} @Column(name = "DESCED")
public String getDesced() {
return this.desced;
} public void setDesced(String desced) {
this.desced = desced;
} @Column(name = "SQL_LEVEL", nullable = false, length = 32)
public String getLeveled() {
return this.leveled;
} public void setLeveled(String leveled) {
this.leveled = leveled;
} @Column(name = "SQL", nullable = false, length = 128)
public String getSql() {
return this.sql;
} public void setSql(String sql) {
this.sql = sql;
} @Column(name = "ALARM_DESC", nullable = false)
public String getAlarmDesc() {
return this.alarmDesc;
} public void setAlarmDesc(String alarmDesc) {
this.alarmDesc = alarmDesc;
} @Column(name = "UPDATETIME")
public Date getUpdatetime() {
return this.updatetime;
} public void setUpdatetime(Date updatetime) {
this.updatetime = updatetime;
} @Column(name = "UPDATE_AUTHOR", length = 100)
public String getUpdateAuthor() {
return this.updateAuthor;
} public void setUpdateAuthor(String updateAuthor) {
this.updateAuthor = updateAuthor;
} @Column(name = "CREATETIME")
public Date getCreatetime() {
return this.createtime;
} public void setCreatetime(Date createtime) {
this.createtime = createtime;
} @Column(name = "CREATE_AUTHOR", length = 100)
public String getCreateAuthor() {
return this.createAuthor;
} public void setCreateAuthor(String createAuthor) {
this.createAuthor = createAuthor;
} @OneToMany(mappedBy = "sqlmanaged", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public Set<TblMsWatchdog> getTblMsWatchdog() {
return tblMsWatchdog;
} public void setTblMsWatchdog(Set<TblMsWatchdog> tblMsWatchdog) {
this.tblMsWatchdog = tblMsWatchdog;
} @OneToMany(mappedBy = "tblmsSqlManaged", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public Set<TblMsSqlTheresholdLink> getTblMsSqlTheresholdLink() {
return tblMsSqlTheresholdLink;
} public void setTblMsSqlTheresholdLink(
Set<TblMsSqlTheresholdLink> tblMsSqlTheresholdLink) {
this.tblMsSqlTheresholdLink = tblMsSqlTheresholdLink;
}}
@Entity
@Table(name = "tbl_ms_threshold", schema = "UDEP3")
public class TblMsTheshold implements java.io.Serializable { private Long threshold_id;
private String name;
private String desc;
private String paramName;
private String alarmCondition;
private String alarmDesc;
private Long actionId;
private Date updatetime;
private String updateAuthor;
private Date createtime;
private String createAuthor; private Set<TblMsSqlTheresholdLink> tblMsSqlTheresholdLink2; private Boolean checked; public TblMsTheshold() {
} @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "THRESHOLD_ID", unique = true, nullable = false, scale = 0)
public Long getThreshold_id() {
return threshold_id;
} public void setThreshold_id(Long thresholdId) {
threshold_id = thresholdId;
} @Column(name = "NAME", length = 100)
public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} @Column(name = "THRESHOLD_DESC", length = 500)
public String getDesc() {
return this.desc;
} public void setDesc(String desc) {
this.desc = desc;
} @Column(name = "PARAM_NAME", length = 100)
public String getParamName() {
return this.paramName;
} public void setParamName(String paramName) {
this.paramName = paramName;
} @Column(name = "ALARM_CONDITION", length = 500)
public String getAlarmCondition() {
return this.alarmCondition;
} public void setAlarmCondition(String alarmCondition) {
this.alarmCondition = alarmCondition;
} @Column(name = "ALARM_DESC", length = 500)
public String getAlarmDesc() {
return this.alarmDesc;
} public void setAlarmDesc(String alarmDesc) {
this.alarmDesc = alarmDesc;
} @Column(name = "ACTION_ID", scale = 0)
public Long getActionId() {
return this.actionId;
} public void setActionId(Long actionId) {
this.actionId = actionId;
} @Column(name = "UPDATETIME")
public Date getUpdatetime() {
return this.updatetime;
} public void setUpdatetime(Date updatetime) {
this.updatetime = updatetime;
} @Column(name = "UPDATE_AUTHOR", length = 100)
public String getUpdateAuthor() {
return this.updateAuthor;
} public void setUpdateAuthor(String updateAuthor) {
this.updateAuthor = updateAuthor;
} @Column(name = "CREATETIME")
public Date getCreatetime() {
return this.createtime;
} public void setCreatetime(Date createtime) {
this.createtime = createtime;
} @Column(name = "CREATE_AUTHOR", length = 100)
public String getCreateAuthor() {
return this.createAuthor;
} public void setCreateAuthor(String createAuthor) {
this.createAuthor = createAuthor;
} @OneToMany(mappedBy = "tblMsTheshold", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public Set<TblMsSqlTheresholdLink> getTblMsSqlTheresholdLink2() {
return tblMsSqlTheresholdLink2;
}
public void setTblMsSqlTheresholdLink2(Set<TblMsSqlTheresholdLink> tblMsSqlTheresholdLink2) {
this.tblMsSqlTheresholdLink2 = tblMsSqlTheresholdLink2;
}

@Transient
public Boolean getChecked() {
return checked;
} public void setChecked(Boolean checked) {
this.checked = checked;
}}
@Entity
@Table(name = "TBL_MS_SQL_THERESHOLD_LINK", schema = "UDEP3")
public class TblMsSqlTheresholdLink implements java.io.Serializable { private Long id; private TblMsSqlmanaged tblmsSqlManaged; private TblMsTheshold tblMsTheshold; public TblMsSqlTheresholdLink() {
}
@Id
@Column(name = "ID", unique = true, nullable = false, scale = 0)
public Long getId() {
return this.id;
} public void setId(Long id) {
this.id = id;
} @ManyToOne(optional = true)
@JoinColumn(name = "SQLID")
public TblMsSqlmanaged getTblmsSqlManaged() {
return tblmsSqlManaged;
} public void setTblmsSqlManaged(TblMsSqlmanaged tblmsSqlManaged) {
this.tblmsSqlManaged = tblmsSqlManaged;
} @ManyToOne(optional = true)
@JoinColumn(name = "THRESHOLD_ID")
public TblMsTheshold getTblMsTheshold() {
return tblMsTheshold;
} public void setTblMsTheshold(TblMsTheshold tblMsTheshold) {
this.tblMsTheshold = tblMsTheshold;
}
  编译没有错误,运行         @Override
public List<TblMsWatchdog> getWatchDogAll() {
List<TblMsWatchdog> list = this.getHibernateTemplate().find(" from  TblMsWatchdog ");
return list;
}   时候提示“THRESHOLD_ID” 标识符无效,我觉得没有错误,还请各位大侠帮忙看看,不胜感激!

解决方案 »

  1.   


    //用户与角色,多对多关系
    //实体类两个,但数据表是三个
    package gsy.comm.auth;
    ..
    @Entity
    @Table(name="D_USER")
    public class User  implements Serializable{
      static final long serialVersionUID = 1L;
      public User() {}  private Integer _userId;
      private String _userName;
      private String _password;  //角色列表
      private Set<Role> _roles = new HashSet<Role>();
      //角色名列表
      private List<String> _rolenameList = new ArrayList<String>();  @Id
      @Column(name="USERID")
      //@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_STORE")
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      public Integer getUserId(){
        return _userId;
      }
      public void setUserId(Integer userId){
        _userId = userId;
      }  @Basic
      @Column(name="USERNAME",unique=true,nullable=false,length=32)
      public String getUserName(){
        return _userName;
      }
      public void setUserName(String userName){
        _userName = userName;
      }  @Basic
      @Column(name="PASSWORD",nullable=false,length=32)
      public String getPassword(){
        return _password;
      }
      public void setPassword(String password){
        _password = password;
      }  //处理角色对象列表
      @ManyToMany(
        targetEntity=gsy.comm.auth.Role.class,
        cascade={CascadeType.PERSIST,CascadeType.MERGE},
        fetch=FetchType.EAGER
      )
      @JoinTable(
        name="D_USER_ROLE",
        joinColumns={@JoinColumn(name="USERID")},
        inverseJoinColumns={@JoinColumn(name="ROLEID")}
      )
      public Set<Role> getRoles(){
        return _roles;
      }
      public void setRoles(Set<Role> roles){
        _roles = roles;
      }  public User (String userName,
                   String password,
                   Set<Role> roles){
        _userName = userName;
        _password = password;
        _roles = roles;
      }
      public String toString(){
        return "user.class";
      }  public void addRole(Role role) {
        if (!_roles.contains(role)){
          _roles.add(role);
        }
      }
      public void removeRole(Role role){
        _roles.remove(role);
      }  //处理角色名列表
      @Transient
      public List<String> getRolenameList(){
        return _rolenameList;
      }
      public void setRolenameList(List<String> rolenameList){
        _rolenameList = rolenameList;
      }  public void resert(){
        _userName = null;
        _password = null;
      }
    }
    //////////////////////////////
    package gsy.comm.auth;
    ...
    @Entity
    @Table(name="D_ROLE")
    public class Role implements java.io.Serializable{
      static final long serialVersionUID = 1L;  private Integer _roleId;
      private String _roleName;
      private Set<User> _users= new HashSet<User>();  public Role() {}  @Id
      @Column(name="ROLEID")
      //@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_STORE")
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      public Integer getRoleId(){
        return _roleId;
      }
      public void setRoleId(Integer roleId){
        _roleId = roleId;
      }  @Basic
      @Column(name="ROLENAME",unique=true,nullable=false,length=32)
      public String getRoleName(){
        return _roleName;
      }
      public void setRoleName(String roleName){
        _roleName = roleName;
      }  @ManyToMany(
        cascade={CascadeType.PERSIST,CascadeType.MERGE},
        mappedBy="roles",
        targetEntity=gsy.comm.auth.User.class
      )
      public Set<User> getUsers(){
        return _users;
      }
      public void setUsers(Set<User> users){
        _users = users;
      }  public String toString(){
        return "role.class";
      }}供参考
      

  2.   

    manyToOne是否该指定fetch ?
      

  3.   

    3楼的答案,我之前用过,如果用manytomany的话,级联删除会不会有麻烦?当我只想删除关联表的信息时,Hibernate所有关连表全部删除。这并不是我想要的,所以我拆开来了,在manyToOne这边选择Cascade的删除和修改,这样应该只会删除多的这方,而一的一方不会删除。不知道这样理解对不对?
      

  4.   

    终于搞定了,两边的OneToMany 配制成fetch = FetchType.LAZY 就可以了,谢谢两位!