问题描述
您在带有 CF-ORM(休眠)的 ColdFusion 9 中观察到哪些应该注意的事情?
entity
init()
方法不能有必需的参数,否则EntityNew()
和其他 CF-ORM 操作将中断.您可能希望使用工厂来创建实体,并在那里强制执行所需的参数.ORMReload()
withormsettings.dbcreate = "drop create"
可能不会为您删除所有表.CF9 Cumulative Hot Fix 1 稍微改进了这一点,但您可能想放弃自己在数据库中的表.type="date"
(默认使用ormtype="date"
),只会存储日期而不是时间.如果您还想保留时间,请使用ormtype="timestamp"
type="string"
将默认为varchar(255)
type="numeric"
将默认为float
,而不是int
.如果需要,请使用 ormtype="int".如果
fieldtype="id"
并且生成器设置为某个生成器,ormtype 将默认为int
.type="string" length="10"
将使用varchar(10)
,而不是char(10)
ormtype="char" length="10"
仍将使用char(1)
.如果确实需要,请使用sqltype="char(10)"
.type="boolean"
默认使用tinyint
,如果需要,请使用sqltype="bit"
.p>应该在双向关系中使用
inverse=true
,通常在一对多"方面.在单向关系中不要使用
inverse="true"
!这种关系可能根本不会持久!如果您使用 MS-SQL,则一对一属性设置为 Null 的实体不能超过 1 个,因为 Null 被视为索引中的唯一值.使列不为空的好主意.(或使用链接表)
EntityLoad("entity", 1, true)
有效,但EntityLoadByPK("entity", 1)
更干净!EntityLoad()
、EntityLoadByPK()
和ORMExecuteQuery
和unique=true
,将如果未找到实体,则返回null
.在使用返回值之前使用isNull()
进行检查.ORMExecuteQuery
如果默认没有找到实体,将返回空数组.不要忘记在一对多"/多对多"中使用
singularname
属性以获得更好看的生成函数(例如addDog(Dogdog)
vsaddDogs(Dog Dogs)
.)将加载所有延迟加载属性.或者,您可以尝试
<cfdump var="#entityToQuery([entity])#">
或设置 top=1 以高效转储.存储在 Session 范围内的实体将与其 Hibernate 会话范围断开连接,并且不会加载延迟加载属性.要恢复休眠会话范围,请使用
entityLoadByExample()
或entitySave(entity)
.cascade="all-delete-orphan"
通常对于一对多"或多对多"关系更有意义.Hibernate 设置空然后删除,因此请确保该列可以为空.测试一下,看看这是否是你的欲望行为.设置
required="true"
每当notnull="true"
时,对于使用 CFCExplorer 浏览 CFC 的其他人来说更具可读性EntityNew('Y')
比new com.XY
稍微高效一些,如果实体要在以后持久化,根据一些 Adobe 工程师的说法.与继承实体的关系有时可能会因未修复的 Hibernate 错误而中断,请使用
linktable
作为解决方法.structKeyColumn
不能是目标实体的 PK.双向多对多不能使用结构
向struct添加新实体时,当CF保留父实体时,会忽略
structKeyColumn
.如果直接访问一对多/多对多数组或结构体,请在使用前确保对应的数组/结构体存在.生成的 addX()/hasX()/removeX() 随时可以安全使用.
在
postInsert()
,实体hibernate session不再可用,所以postInsert()的设置属性会被静默忽略,否则会抛出Session is Closed异常.p>通过
entityLoad()
或HQL从DB加载实体后,即使没有调用EntitySave()
,更改也会自动持久化.p>CF-ORM 事务的实现方式是启动一个新会话并在完成时关闭.
在事件内部(即 preLoad()/postInsert()),分配给变量可能会抛出关于类型的 Java 异常.使用 JavaCast() 解决该错误.
更新
- CF9.0.1+:使用
<cfquery dbtype="hql">
,更容易做cfqueryparam
,调试输出实际显示绑定值.
What are some of the things you've observed in ColdFusion 9 with CF-ORM (Hibernate) that one should watch out for?
entity
init()
method must not have required argument(s), otherwiseEntityNew()
and other CF-ORM actions will break. You may want to use a Factory to create the entity, and enforce the required arguments there.A bug regarding this limitation has been filed in the Adobe Bugbase.
ORMReload()
withormsettings.dbcreate = "drop create"
might not drop all tables for you. CF9 Cumulative Hot Fix 1 improves this a little bit, but you might want to drop the tables in DB yourself.type="date"
(default to useormtype="date"
), will only store date but not time. If you want to persisted time as well, useormtype="timestamp"
type="string"
will default tovarchar(255)
type="numeric"
will default tofloat
, notint
. Use ormtype="int" if needed.if
fieldtype="id"
and generator is set to some generator, ormtype will default toint
.type="string" length="10"
will usevarchar(10)
, notchar(10)
ormtype="char" length="10"
will usechar(1)
still. Usesqltype="char(10)"
if you really need to.type="boolean"
usetinyint
by default, usesqltype="bit"
if you need to.should use
inverse=true
in a bi-directional relationship, usually in "one-to-many" side.do NOT use
inverse="true"
in uni-directional relationship! The relationship might not be persisted at all!If you use MS-SQL, you cannot have more than 1 entity with one-to-one property set to Null, because Null is considered as an unique value in an index. Good idea to make column not null. (or use linktable)
EntityLoad("entity", 1, true)
works, butEntityLoadByPK("entity", 1)
is cleaner!EntityLoad()
,EntityLoadByPK()
, andORMExecuteQuery
withunique=true
, will returnnull
if entity is not found. UseisNull()
to check before you use the returned value.ORMExecuteQuery
will return empty array if no entity is found by default.don't forget to use
singularname
property in "one-to-many" / "many-to-many" for nicer looking generated functions (e.g.addDog(Dog dog)
vsaddDogs(Dog dogs)
.)<cfdump>
will load all the lazy-load properties. Alternatively you may try<cfdump var="#entityToQuery([entity])#">
or set top=1 to dump efficiently.entity stored in Session scope will be disconnected with its Hibernate session scope, and lazy load property will not be loaded. To restore the hibernate session scope, use
entityLoadByExample()
orentitySave(entity)
.cascade="all-delete-orphan"
usually make more sense for "one-to-many" or "many-to-many" relationship. Hibernate sets null then delete, so make sure the column is nullable. Test and see if that's your desire behaviour.set
required="true"
whenevernotnull="true"
, more readable for others browsing the CFC with CFCExplorerEntityNew('Y')
is slightly more efficient thannew com.X.Y
if the entity is to be persisted later according to some Adobe engineer.relationship with an inherited entity may break sometimes due to an unfixed Hibernate bug, use
linktable
as a workaround.structKeyColumn
cannot be the PK of the target entity.bi-directional many-to-many cannot use struct
When adding new entity to struct,
structKeyColumn
is ignored when CF persists the parent entity.If you access the one-to-many / many-to-many array or struct directly, make sure the corresponding array/struct exists before use. Generated addX()/hasX()/removeX() are safe to use anytime.
at
postInsert()
, the entity hibernate session is no longer available, so setting property at postInsert() will be silently ignore, or Session is Closed exception will be thrown.after entity is loaded by
entityLoad()
or HQL from DB, the changes will be automatically persisted even ifEntitySave()
is not called.transaction with CF-ORM is implemented in a way that it starts a new session and close when it's done.
inside the event (i.e. preLoad() / postInsert()), assigning to variables might throw Java exception about types. Use JavaCast() to work around the bug.
UPDATE
- CF9.0.1+: use
<cfquery dbtype="hql">
, easier to docfqueryparam
, and debug output actually shows you the binded values.
这篇关于在使用 CF-ORM 的 ColdFusion 9 中需要注意的事项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!