oracle 休眠预言机标识符太长 ORA-00972
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11414094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate oracle identifier is too long ORA-00972
提问by kamil
Im stuck with this problem. The database schema is provided by someone else so I cant simply change names. I tried add everywhere proper annotations, maybe I'm missing something (obvious)?
我被这个问题困住了。数据库架构是由其他人提供的,所以我不能简单地更改名称。我尝试在任何地方添加适当的注释,也许我遗漏了一些东西(很明显)?
Here is my full mapping (quite many classess), I'll ommit getter/setters.
这是我的完整映射(相当多的类),我将省略 getter/setter。
The problem is when hibernate is trying to get all List<ControlRuleAttrib> controlRuleAttribs
问题是当休眠试图获得所有 List<ControlRuleAttrib> controlRuleAttribs
Controle Rule
控制规则
@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;
}
ControleRuleAttribPKQuestion here is, is it possible to somehow get Entity ControlRuleAttribType
from ControlRuleAttrib
? As you can see below ControlRuleAttribTypeId
is the id of ControleRuleAttribType
. I'd like to get whole object isteand of integer.
ControleRuleAttribPK这里的问题是,是否有可能以某种方式得到实体ControlRuleAttribType
的ControlRuleAttrib
?如您所见,下面ControlRuleAttribTypeId
是ControleRuleAttribType
. 我想获得整数的整个对象。
@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;
}
EDIT
编辑
Here is stacktrace:
这是堆栈跟踪:
https://gist.github.com/a30dd9ce534d96bb9a97
https://gist.github.com/a30dd9ce534d96bb9a97
As you'll find out, it fails here:
你会发现,它在这里失败了:
at com.execon.controllers.main.MainPageController.getMainPage(MainPageController.java:33) [classes:]
在 com.execon.controllers.main.MainPageController.getMainPage(MainPageController.java:33) [类:]
and this is it:
就是这样:
List<ControlRule> list = SessionFactoryUtils.openSession(
sessionFactory ).createQuery( "from ControlRule" ).list();
System.out.println( list );
every object which mapping I added, has toString()
method declared like this:
我添加的每个映射对象都具有如下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;
}
And hibernate request:
和休眠请求:
https://gist.github.com/c8584113522757a4e0d8/4f31dc03e7e842eef693fa7ba928e19d27b3ca26
https://gist.github.com/c8584113522757a4e0d8/4f31dc03e7e842eef693fa7ba928e19d27b3ca26
Help please :)
请帮忙 :)
EDIT 2
编辑 2
Well after reading @Jens answer, I did some changes in the code. First I did as you wrote and it gave error:
阅读@Jens 的回答后,我对代码做了一些更改。首先我按照你写的做了,但出现错误:
org.hibernate.AnnotationException: A Foreign key refering com.execon.models.controlrules.ControlRuleAttrib from com.execon.models.controlrules.ControlRule has the wrong number of column. should be 3
org.hibernate.AnnotationException:从 com.execon.models.controlrules.ControlRule 引用 com.execon.models.controlrules.ControlRuleAttrib 的外键具有错误的列数。应该是 3
I guess this is right, as I have composite primary key.
我想这是对的,因为我有复合主键。
Then I tried it this way:
然后我这样试了一下:
@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;
Quite close but it gives me the following exception:
非常接近,但它给了我以下例外:
Repeated column in mapping for collection..
集合映射中的重复列..
So finally I removed
所以最后我删除了
joinColumns =
{
@JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false)
}
And everything compiled except that when I try to reach collection, Hibernate is doing following query:
除了当我尝试访问集合时编译的所有内容,Hibernate 正在执行以下查询:
https://gist.github.com/c88684392f0b7a62bea5
https://gist.github.com/c88684392f0b7a62bea5
The last line, is controlrul0_.CONTROL_RULE_CONTROL_RULE_ID=?
while it should be controlrul0_.CONTROL_RULE_ID=?
.
最后一行,是controlrul0_.CONTROL_RULE_CONTROL_RULE_ID=?
while 它应该是controlrul0_.CONTROL_RULE_ID=?
。
Is there anyway I can make it work? :/
无论如何我可以让它工作吗?:/
采纳答案by kamil
After struggling with this for last few hours I finally make it working within my project. The thing I did was this:
经过最后几个小时的努力,我终于让它在我的项目中工作。我做的事情是这样的:
ControlRule
控制规则
@OneToMany(fetch = FetchType.LAZY, mappedBy = "controlRuleAttribPK.controlRuleId")
@Cascade(CascadeType.ALL)
private List<ControlRuleAttrib> controlRuleAttribs;
Basically pointing that the collection should use controlRuleId from composite primary key. So far its working great!
基本上指出集合应该使用来自复合主键的 controlRuleId。到目前为止它的工作很棒!
回答by gkuzmin
I do not clearly understand which identifier is too long, but I can propose that you can try to change @Id
annotated field types from Integer
to Long
. And it will be great to get some detailed info (stack trace etc.) about your problem.
我不太清楚哪个标识符太长,但我建议您可以尝试将带@Id
注释的字段类型从更改Integer
为Long
。获得有关您的问题的一些详细信息(堆栈跟踪等)会很棒。
回答by Jens Schauder
The problem is the column
问题是列
CONTROL_RULE_ATTRIB.controlRuleAttribs_CONTROL_RULE_ATTRIB_TYPE_ID
Which is so long it can't possibly exist in your database. I don't see in the question any hint how it is actually named, but I am assuming the proper name is
太长了,它不可能存在于您的数据库中。我在问题中没有看到任何暗示它实际上是如何命名的,但我假设正确的名称是
CONTROL_RULE_ATTRIB.CONTROL_RULE_ATTRIB_TYPE_ID
So the question is: Why is hibernate trying to access this column. The table it is in is a mapping table which is mapped in hibernate as:
所以问题是:为什么 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;
If you look at the @JoinTable annotation you'll notice that it defines the table name as it is used and a join column. But a mapping table has two sets of join columns. The second on is not specified. Therefore the NamingStrategy you configured or if you didn't configured any the default is used. Just specify the other set of join columns using the attribute inverseJoinColumns and all should be fine.
如果您查看@JoinTable 注释,您会注意到它定义了使用时的表名和连接列。但是映射表有两组连接列。未指定第二个。因此,您配置的 NamingStrategy 或如果您没有配置任何默认值将被使用。只需使用属性 inverseJoinColumns 指定另一组连接列,一切都应该没问题。
The complete annotation should look like:
完整的注释应如下所示:
@JoinTable(name = "CONTROL_RULE_ATTRIB", joinColumns = {@JoinColumn(name = "CONTROL_RULE_ID", nullable = false, updatable = false)}, inverseJoinColumns = {@JoinColumn(name = "CONTROL_RULE_ATTRIB_TYPE_ID")})
Note: I have no idea if you need any of the nullable, updatable stuff on that column as well.
注意:我不知道您是否还需要该列上的任何可为空、可更新的内容。
See also the documentation of the JoinTable annotation
另请参阅JoinTable 注释的文档