hibernate oracle标识符太长ORA-00972

时间:2012-07-10 13:14:15

标签: java oracle hibernate oracle11g

我坚持这个问题。数据库架构由其他人提供,所以我不能简单地更改名称。我尝试添加适当的注释,也许我错过了一些东西(显而易见)?

这是我的完整映射(很多classess),我会省略getter / setters。

问题是当hibernate试图获得所有List<ControlRuleAttrib> controlRuleAttribs

控制规则

@Entity
@Table(name = "CONTROL_RULE")
public class ControlRule implements Serializable {
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "CONTROL_RULE_ID")
 private Long id;
 @ManyToOne(fetch = FetchType.LAZY)
 @Cascade(CascadeType.ALL)
 @JoinColumn(name = "CONTROL_RULE_TYPE_ID")
 @ForeignKey(name = "CONTROL_RULE_TYPE_ID")
 private ControlRuleType controlRuleType;
 @Column(name = "JOB_NM")
 private String jobname;
 @Column(name = "LIBRARY_NM")
 private String libraryname;
 @Column(name = "TABLE_NM")
 private String tablename;
 @Column(name = "COLUMN_NM")
 private String columnname;

 @OneToMany(fetch = FetchType.LAZY)
 @Cascade(CascadeType.ALL)
 @JoinTable(name = "CONTROL_RULE_ATTRIB", joinColumns = {
  @JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false)
 })
 private List < ControlRuleAttrib > controlRuleAttribs;
}

ControlRuleAttrib

@Table(name = "CONTROL_RULE_ATTRIB")
@Entity
public class ControlRuleAttrib {
 @EmbeddedId
 private ControlRuleAttribPK controlRuleAttribPK;

 @Column(name = "ATTRIBUTE_VALUE")
 private String attributeValue;
}

ControleRuleAttribPK 这里的问题是,是否有可能以某种方式从ControlRuleAttribType获取实体ControlRuleAttrib?正如您在下面看到的,ControlRuleAttribTypeIdControleRuleAttribType的ID。我想得到整个对象isteand的整数。

@Embeddable
public class ControlRuleAttribPK implements Serializable {
 @Column(name = "CONTROL_RULE_ID")
 private Long controlRuleId;

 @Column(name = "ATTRIBUTE_SEQ_NUM")
 private Integer attributeSeqNum;

 @Column(name = "CONTROL_RULE_ATTRIB_TYPE_ID")
 private Integer controlRuleAttribTypeId;
}

ControleRuleAttribType

@Entity
@Table(name = "CONTROL_RULE_ATTRIB_TYPE")
public class ControlRuleAttribType implements Serializable {
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "CONTROL_RULE_ATTRIB_TYPE_ID")
 private Integer id;
 @Column(name = "CONTROL_RULE_ATTRIB_TYPE_NM")
 private String typename;
 @Column(name = "CONTROL_RULE_ATTRIB_TYPE_DESC")
 private String typedesc;

 @ManyToOne(fetch = FetchType.LAZY)
 @Cascade(CascadeType.ALL)
 @JoinColumn(name = "CONTROL_RULE_TYPE_ID")
 @ForeignKey(name = "CONTROL_RULE_TYPE_ID")
 private ControlRuleType controlruletype;
}

ControleRuleType

@Entity
@Table(name = "CONTROL_RULE_TYPE")
public class ControlRuleType implements Serializable {
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "CONTROL_RULE_TYPE_ID")
 private Integer id;
 @Column(name = "CONTROL_RULE_TYPE_NM")
 private String typename;
 @Column(name = "CONTROL_RULE_TYPE_DESC")
 private String typedesc;
}

修改

这是stacktrace:

https://gist.github.com/a30dd9ce534d96bb9a97

你会发现,它在这里失败了:

  

在   com.execon.controllers.main.MainPageController.getMainPage(MainPageController.java:33)   [类:

就是这样:

List<ControlRule> list = SessionFactoryUtils.openSession(
    sessionFactory ).createQuery( "from ControlRule" ).list();
System.out.println( list );

我添加的映射的每个对象都有toString()方法声明如下:

@Override
public String toString()
{
    String s = "ControlRule{";
    s += "id=" + id.toString();
    s += ", controlRuleType=" + controlRuleType;
    s += ", jobname='" + jobname + '\'';
    s += ", libraryname='" + libraryname + '\'';
    s += ", tablename='" + tablename + '\'';
    s += ", columnname='" + columnname + '\'';
    s += ", controlRuleAttribs=" + controlRuleAttribs;
    s += '}';
    return s;
}

和hibernate请求:

https://gist.github.com/c8584113522757a4e0d8/4f31dc03e7e842eef693fa7ba928e19d27b3ca26

请帮助:)

编辑2

在阅读@Jens回答之后,我对代码进行了一些更改。首先,我按照你的写作做了,它给出了错误:

  

org.hibernate.AnnotationException:外键引用   来自的com.execon.models.controlrules.ControlRuleAttrib   com.execon.models.controlrules.ControlRule的编号错误   柱。应该是3

我想这是对的,因为我有复合主键。

然后我就这样试了:

@OneToMany(fetch = FetchType.LAZY)
@Cascade(CascadeType.ALL)
@JoinTable(name = "CONTROL_RULE_ATTRIB",
        joinColumns = {
                @JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false)
        },
        inverseJoinColumns = {
                @JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false),
                @JoinColumn(name = "CONTROL_RULE_ATTRIB_TYPE_ID", nullable = false, updatable = false),
                @JoinColumn(name = "ATTRIBUTE_SEQ_NUM", nullable = false, updatable = false)
        })
private List<ControlRuleAttrib> controlRuleAttribs;

非常接近,但它给了我以下例外:

  

映射集合中的重复列..

所以最后我删除了

joinColumns = 
{
    @JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false)
}

除了当我尝试访问集合时,Hibernate正在进行以下查询:

https://gist.github.com/c88684392f0b7a62bea5

最后一行是controlrul0_.CONTROL_RULE_CONTROL_RULE_ID=?,而应该是controlrul0_.CONTROL_RULE_ID=?

无论如何我能使它运作吗? :/

3 个答案:

答案 0 :(得分:2)

在最近几个小时的努力之后,我终于让它在我的项目中工作了。我做的是这个:

<强> ControlRule

@OneToMany(fetch = FetchType.LAZY, mappedBy = "controlRuleAttribPK.controlRuleId")
@Cascade(CascadeType.ALL)
private List<ControlRuleAttrib> controlRuleAttribs;

基本上指出集合应该使用复合主键中的controlRuleId。到目前为止它的工作很棒!

答案 1 :(得分:0)

我不清楚哪个标识符太长,但我建议您可以尝试将@Id带注释的字段类型从Integer更改为Long。获得有关您问题的详细信息(堆栈跟踪等)会很棒。

答案 2 :(得分:0)

问题是专栏

CONTROL_RULE_ATTRIB.controlRuleAttribs_CONTROL_RULE_ATTRIB_TYPE_ID

这是多久,它不可能存在于您的数据库中。我没有在问题中看到它是如何实际命名的,但我假设正确的名称是

CONTROL_RULE_ATTRIB.CONTROL_RULE_ATTRIB_TYPE_ID

所以问题是:为什么hibernate试图访问这个列。它所在的表是一个映射表,它在hibernate中映射为:

@OneToMany(fetch = FetchType.LAZY)
@Cascade(CascadeType.ALL)
@JoinTable(name = "CONTROL_RULE_ATTRIB", joinColumns = {@JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false)})
private List<ControlRuleAttrib> controlRuleAttribs;

如果查看@JoinTable注释,您会注意到它定义了使用的表名和连接列。但是映射表有两组连接列。第二个未指定。因此,使用您配置的NamingStrategy或未配置任何默认值的NamingStrategy。只需使用属性inverseJoinColumns指定另一组连接列,所有都应该没问题。

完整注释应如下所示:

@JoinTable(name = "CONTROL_RULE_ATTRIB", joinColumns = {@JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false)}, inverseJoinColumns = {@JoinColumn(name = "CONTROL_RULE_ATTRIB_TYPE_ID")})

注意:我不知道你是否需要该列上任何可以为空的可更新内容。

另请参阅documentation of the JoinTable annotation