问题描述
我有一个带有无参数构造函数的对象类型,但是当我将其指定为该类型列的默认值时,会收到ORA-00904:无效的标识符错误.
I have an object type with no-args constructor, but when I specify it as default value for a column of that type, I get ORA-00904: invalid identifier error.
示例:
CREATE OR REPLACE TYPE test_t AS OBJECT
(
val NUMBER(10),
CONSTRUCTOR FUNCTION test_t return self as result
)
CREATE OR REPLACE TYPE BODY test_t AS
CONSTRUCTOR FUNCTION test_t RETURN SELF AS RESULT IS
BEGIN
val := 1;
RETURN;
END;
END;
CREATE TABLE test_table (
test_attr test_t DEFAULT new test_t()
)
Error: ORA-00904: "INKA"."TEST_T"."TEST_T": invalid identifier
如果我将DEFAULT替换为例如test_t(1),它可以工作,但是这种方式打破了OO封装范例,我希望所有相同类型的字段都具有相同的默认默认值"(希望您知道我的意思是:-)
If I replace DEFAULT with e.g. test_t(1), it works, but that sort of breaks the OO encapsulation paradigm, I want all fields of same type to have same default "default values" (hope you know what I mean :-)
我是否在这里遗漏了一些东西,或者这是正常现象,并且无法使用像这样的非默认构造函数吗?
Am I missing something here, or is this normal and it is not possible to use non-default constructors like this?
推荐答案
看起来不可能这样.
一种解决方法是使用触发器:
One workaround would be to use a trigger:
CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT OR UPDATE
ON test_table
FOR EACH ROW
WHEN ( new.test_attr IS NULL )
BEGIN
:new.test_attr := NEW test_t();
END test_trigger;
/
顺便说一句,它不会完全忽略非默认构造函数,而会覆盖默认构造函数
It does not completely ignore non-default constructors by the way, overriding the default constructor
CONSTRUCTOR FUNCTION test_t(in_val NUMBER)
RETURN SELF AS RESULT
尝试使用DEFAULT NEW test_t(1)
定义表时,
导致异常:
leads to an exception when trying to define the table with DEFAULT NEW test_t(1)
:
这篇关于Oracle:为对象类型列指定默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!