﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-&lt;h3 style="font-family: Comic Sans MS"&gt;&lt;font color="#FA1A0A" size="10"&gt;︻┳═一Java&lt;/font&gt;&lt;/h3&gt;-随笔分类-EJB</title><link>http://www.blogjava.net/rain1102/category/37636.html</link><description>&lt;br/&gt;&lt;font color="green" style="font-family: 华文行楷;font-size:16px;"&gt;子曰：危邦不入，乱邦不居。天下有道则见，无道则隐。&lt;/font&gt;&lt;font color="#3C1435"&gt;&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Sat, 21 Feb 2009 02:39:46 GMT</lastBuildDate><pubDate>Sat, 21 Feb 2009 02:39:46 GMT</pubDate><ttl>60</ttl><item><title>[转] 常用 JPA annotation 参考</title><link>http://www.blogjava.net/rain1102/archive/2009/02/09/253913.html</link><dc:creator>Eric.Zhou</dc:creator><author>Eric.Zhou</author><pubDate>Mon, 09 Feb 2009 07:38:00 GMT</pubDate><guid>http://www.blogjava.net/rain1102/archive/2009/02/09/253913.html</guid><wfw:comment>http://www.blogjava.net/rain1102/comments/253913.html</wfw:comment><comments>http://www.blogjava.net/rain1102/archive/2009/02/09/253913.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/rain1102/comments/commentRss/253913.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rain1102/services/trackbacks/253913.html</trackback:ping><description><![CDATA[<p><strong>Table <br />
</strong>Table用来定义entity主表的name，catalog，schema等属性。 <br />
元数据属性说明： <br />
&#183;&nbsp;name: 表名 <br />
&#183;&nbsp;catalog: 对应关系数据库中的catalog <br />
&#183;&nbsp;schema：对应关系数据库中的schema <br />
&#183;&nbsp;UniqueConstraints:定义一个UniqueConstraint数组，指定需要建唯一约束的列 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Entity<br />
@Table(name="CUST")<br />
public class Customer { ... }<br />
　　<br />
<strong>SecondaryTable</strong> <br />
一个entity class可以映射到多表，SecondaryTable用来定义单个从表的名字，主键名字等属性。 <br />
元数据属性说明： <br />
&#183;&nbsp;name: 表名 <br />
&#183;&nbsp;catalog: 对应关系数据库中的catalog <br />
&#183;&nbsp;schema：对应关系数据库中的schema <br />
&#183;&nbsp;pkJoin: 定义一个PrimaryKeyJoinColumn数组，指定从表的主键列 <br />
&#183;&nbsp;UniqueConstraints:定义一个UniqueConstraint数组，指定需要建唯一约束的列 <br />
下面的代码说明Customer类映射到两个表，主表名是CUSTOMER，从表名是CUST_DETAIL，从表的主键列和主表的主键列类型相同，列名为CUST_ID。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Entity<br />
@Table(name="CUSTOMER")<br />
@SecondaryTable(name="CUST_DETAIL",pkJoin=@PrimaryKeyJoinColumn(name="CUST_ID"))<br />
public class Customer { ... }</p>
<p><strong>SecondaryTables </strong><br />
当一个entity class映射到一个主表和多个从表时，用SecondaryTables来定义各个从表的属性。 <br />
元数据属性说明： <br />
&#183;&nbsp;value： 定义一个SecondaryTable数组，指定每个从表的属性。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Table(name = "CUSTOMER")<br />
@SecondaryTables( value = {<br />
@SecondaryTable(name = "CUST_NAME", pkJoin = { @PrimaryKeyJoinColumn(name = "STMO_ID", referencedColumnName = "id") }),<br />
@SecondaryTable(name = "CUST_ADDRESS", pkJoin = { @PrimaryKeyJoinColumn(name = "STMO_ID", referencedColumnName = "id") }) })<br />
public class Customer {}</p>
<p>UniqueConstraint <br />
UniqueConstraint定义在Table或SecondaryTable元数据里，用来指定建表时需要建唯一约束的列。 <br />
元数据属性说明： <br />
&#183;&nbsp;columnNames:定义一个字符串数组，指定要建唯一约束的列名。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Entity<br />
@Table(name="EMPLOYEE",<br />
uniqueConstraints={@UniqueConstraint(columnNames={"EMP_ID", "EMP_NAME"})}<br />
)<br />
public class Employee { ... }</p>
<p><strong>Column <br />
</strong>Column元数据定义了映射到数据库的列的所有属性：列名，是否唯一，是否允许为空，是否允许更新等。 <br />
元数据属性说明： <br />
&#183;&nbsp;name:列名。 <br />
&#183;&nbsp;unique: 是否唯一 <br />
&#183;&nbsp;nullable: 是否允许为空 <br />
&#183;&nbsp;insertable: 是否允许插入 <br />
&#183;&nbsp;updatable: 是否允许更新 <br />
&#183;&nbsp;columnDefinition: 定义建表时创建此列的DDL <br />
&#183;&nbsp;secondaryTable: 从表名。如果此列不建在主表上（默认建在主表），该属性定义该列所在从表的名字。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
public class Person {<br />
@Column(name = "PERSONNAME", unique = true, nullable = false, updatable = true)<br />
private String name;<br />
@Column(name = "PHOTO", columnDefinition = "BLOB NOT NULL", secondaryTable="PER_PHOTO")<br />
private byte[] picture;</p>
<p><strong>JoinColumn <br />
</strong>如果在entity class的field上定义了关系（one2one或one2many等），我们通过JoinColumn来定义关系的属性。JoinColumn的大部分属性和Column类似。 <br />
元数据属性说明： <br />
&#183;&nbsp;name:列名。 <br />
&#183;&nbsp;referencedColumnName:该列指向列的列名（建表时该列作为外键列指向关系另一端的指定列） <br />
&#183;&nbsp;unique: 是否唯一 <br />
&#183;&nbsp;nullable: 是否允许为空 <br />
&#183;&nbsp;insertable: 是否允许插入 <br />
&#183;&nbsp;updatable: 是否允许更新 <br />
&#183;&nbsp;columnDefinition: 定义建表时创建此列的DDL <br />
&#183;&nbsp;secondaryTable: 从表名。如果此列不建在主表上（默认建在主表），该属性定义该列所在从表的名字。 <br />
下面的代码说明Custom和Order是一对一关系。在Order对应的映射表建一个名为CUST_ID的列，该列作为外键指向Custom对应表中名为ID的列。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
public class Custom {<br />
@OneToOne<br />
@JoinColumn(<br />
name="CUST_ID", referencedColumnName="ID", unique=true, nullable=true, updatable=true)<br />
public Order getOrder() {<br />
return order;<br />
}</p>
<p><strong>JoinColumns</strong> <br />
如果在entity class的field上定义了关系（one2one或one2many等），并且关系存在多个JoinColumn，用JoinColumns定义多个JoinColumn的属性。 <br />
元数据属性说明： <br />
&#183;&nbsp;value: 定义JoinColumn数组，指定每个JoinColumn的属性。 <br />
下面的代码说明Custom和Order是一对一关系。在Order对应的映射表建两列，一列名为CUST_ID，该列作为外键指向Custom对应表中名为ID的列,另一列名为CUST_NAME，该列作为外键指向Custom对应表中名为NAME的列。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
public class Custom {<br />
@OneToOne<br />
@JoinColumns({<br />
@JoinColumn(name="CUST_ID", referencedColumnName="ID"),<br />
@JoinColumn(name="CUST_NAME", referencedColumnName="NAME")<br />
})<br />
public Order getOrder() {<br />
return order;<br />
}</p>
<p><strong>Id </strong><br />
声明当前field为映射表中的主键列。id值的获取方式有五种：TABLE, SEQUENCE, IDENTITY, AUTO, NONE。Oracle和DB2支持SEQUENCE，SQL Server和Sybase支持IDENTITY,mysql支持AUTO。所有的数据库都可以指定为AUTO，我们会根据不同数据库做转换。NONE (默认)需要用户自己指定Id的值。元数据属性说明： <br />
&#183;&nbsp;generate():主键值的获取类型 <br />
&#183;&nbsp;generator():TableGenerator的名字（当generate=GeneratorType.TABLE才需要指定该属性） <br />
下面的代码声明Task的主键列id是自动增长的。(Oracle和DB2从默认的SEQUENCE取值，SQL Server和Sybase该列建成IDENTITY，mysql该列建成auto increment。) <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Entity<br />
@Table(name = "OTASK")<br />
public class Task {<br />
@Id(generate = GeneratorType.AUTO)<br />
public Integer getId() {<br />
return id;<br />
}<br />
}</p>
<p><strong>IdClass</strong> <br />
当entity class使用复合主键时，需要定义一个类作为id class。id class必须符合以下要求:类必须声明为public，并提供一个声明为public的空构造函数。必须实现Serializable接，覆写 equals()和hashCode（）方法。entity class的所有id field在id class都要定义，且类型一样。 <br />
元数据属性说明： <br />
&#183;&nbsp;value: id class的类名 <br />
&nbsp;&nbsp; public class EmployeePK implements <a title="Java爱好者" href="http://www.blogjava.net/rain1102">Java</a>.io.Serializable{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String empName;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Integer empAge;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public EmployeePK(){}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public boolean equals(Object obj){ ......}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int hashCode(){......}<br />
&nbsp;&nbsp;&nbsp; }</p>
<p>&nbsp;&nbsp;&nbsp; @IdClass(value=com.acme.EmployeePK.class)<br />
&nbsp;&nbsp;&nbsp; @Entity(access=FIELD)<br />
&nbsp;&nbsp;&nbsp; public class Employee {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @Id String empName;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @Id Integer empAge;<br />
&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
<strong>MapKey</strong> <br />
在一对多，多对多关系中，我们可以用Map来保存集合对象。默认用主键值做key，如果使用复合主键，则用id class的实例做key，如果指定了name属性，就用指定的field的值做key。 <br />
元数据属性说明： <br />
&#183;&nbsp;name: 用来做key的field名字 <br />
下面的代码说明Person和Book之间是一对多关系。Person的books字段是Map类型，用Book的isbn字段的值作为Map的key。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p>@Table(name = "PERSON")<br />
public class Person {<br />
@OneToMany(targetEntity = Book.class, cascade = CascadeType.ALL, mappedBy = "person")<br />
@MapKey(name = "isbn")<br />
private Map books = new HashMap();<br />
}</p>
<p><br />
<strong>OrderBy <br />
</strong>在一对多，多对多关系中，有时我们希望从数据库加载出来的集合对象是按一定方式排序的，这可以通过OrderBy来实现，默认是按对象的主键升序排列。 <br />
元数据属性说明： <br />
&#183;&nbsp;value: 字符串类型，指定排序方式。格式为"fieldName1 [ASC|DESC],fieldName2 [ASC|DESC],......",排序类型可以不指定，默认是ASC。 <br />
下面的代码说明Person和Book之间是一对多关系。集合books按照Book的isbn升序，name降序排列。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p>@Table(name = "MAPKEY_PERSON")<br />
public class Person {<br />
@OneToMany(targetEntity = Book.class, cascade = CascadeType.ALL, mappedBy = "person")<br />
@OrderBy(name = "isbn ASC, name DESC")<br />
private List books = new ArrayList();<br />
}</p>
<p><br />
<strong>PrimaryKeyJoinColumn</strong> <br />
在三种情况下会用到PrimaryKeyJoinColumn。 <br />
&#183;&nbsp;继承。 <br />
&#183;&nbsp;entity class映射到一个或多个从表。从表根据主表的主键列（列名为referencedColumnName值的列），建立一个类型一样的主键列，列名由name属性定义。 <br />
&#183;&nbsp;one2one关系，关系维护端的主键作为外键指向关系被维护端的主键，不再新建一个外键列。 <br />
元数据属性说明： <br />
&#183;&nbsp;name:列名。 <br />
&#183;&nbsp;referencedColumnName:该列引用列的列名 <br />
&#183;&nbsp;columnDefinition: 定义建表时创建此列的DDL <br />
下面的代码说明Customer映射到两个表，主表CUSTOMER,从表CUST_DETAIL，从表需要建立主键列CUST_ID，该列和主表的主键列id除了列名不同，其他定义一样。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @Entity<br />
@Table(name="CUSTOMER")<br />
@SecondaryTable(name="CUST_DETAIL",pkJoin=@PrimaryKeyJoinColumn(name="CUST_ID"，referencedColumnName="id"))<br />
public class Customer {<br />
@Id(generate = GeneratorType.AUTO)<br />
public Integer getId() {<br />
return id;<br />
}<br />
}</p>
<p>下面的代码说明Employee和EmployeeInfo是一对一关系，Employee的主键列id作为外键指向EmployeeInfo的主键列INFO_ID。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Table(name = "Employee")<br />
public class Employee {<br />
@OneToOne<br />
@PrimaryKeyJoinColumn(name = "id", referencedColumnName="INFO_ID")<br />
EmployeeInfo info;<br />
}</p>
<p><strong>PrimaryKeyJoinColumns</strong> <br />
如果entity class使用了复合主键，指定单个PrimaryKeyJoinColumn不能满足要求时，可以用PrimaryKeyJoinColumns来定义多个PrimaryKeyJoinColumn。 <br />
元数据属性说明： <br />
&#183;&nbsp;value: 一个PrimaryKeyJoinColumn数组，包含所有PrimaryKeyJoinColumn。 <br />
下面的代码说明了Employee和EmployeeInfo是一对一关系。他们都使用复合主键，建表时需要在Employee表建立一个外键，从Employee的主键列id,name指向EmployeeInfo的主键列INFO_ID和INFO_NAME. <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Entity<br />
@IdClass(EmpPK.class)<br />
@Table(name = "EMPLOYEE")<br />
public class Employee {<br />
private int id;<br />
private String name;<br />
private String address;<br />
@OneToOne(cascade = CascadeType.ALL)<br />
@PrimaryKeyJoinColumns({<br />
@PrimaryKeyJoinColumn(name="id", referencedColumnName="INFO_ID"),<br />
@PrimaryKeyJoinColumn(name="name" , referencedColumnName="INFO_NAME")})<br />
EmployeeInfo info;<br />
}<br />
@Entity<br />
@IdClass(EmpPK.class)<br />
@Table(name = "EMPLOYEE_INFO")<br />
public class EmployeeInfo {<br />
@Id<br />
@Column(name = "INFO_ID")<br />
private int id;<br />
@Id<br />
@Column(name = "INFO_NAME")<br />
private String name;<br />
}</p>
<p><strong>Transient <br />
</strong>Transient用来注释entity的属性，指定的这些属性不会被持久化，也不会为这些属性建表。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Transient<br />
private String name;</p>
<p><strong>Version </strong><br />
Version指定实体类在乐观事务中的version属性。在实体类重新由EntityManager管理并且加入到乐观事务中时，保证完整性。每一个类只能有一个属性被指定为version，version属性应该映射到实体类的主表上。 <br />
下面的代码说明versionNum属性作为这个类的version，映射到数据库中主表的列名是OPTLOCK。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Version<br />
@Column("OPTLOCK")<br />
protected int getVersionNum() { return versionNum; }</p>
<p><strong>Lob</strong> <br />
Lob指定一个属性作为数据库支持的大对象类型在数据库中存储。使用LobType这个枚举来定义Lob是二进制类型还是字符类型。 <br />
LobType枚举类型说明： <br />
&#183;&nbsp;BLOB 二进制大对象，Byte[]或者Serializable的类型可以指定为BLOB。 <br />
&#183;&nbsp;CLOB 字符型大对象，char[]、Character[]或String类型可以指定为CLOB。 <br />
元数据属性说明： <br />
&#183;&nbsp;fetch： 定义这个字段是lazy loaded还是eagerly fetched。数据类型是FetchType枚举，默认为LAZY,即lazy loaded. <br />
&#183;&nbsp;type： 定义这个字段在数据库中的JDBC数据类型。数据类型是LobType枚举，默认为BLOB。 <br />
下面的代码定义了一个BLOB类型的属性和一个CLOB类型的属性。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Lob<br />
@Column(name="PHOTO" columnDefinition="BLOB NOT NULL")<br />
protected JPEGImage picture;<br />
@Lob(fetch=EAGER, type=CLOB)<br />
@Column(name="REPORT")<br />
protected String report;</p>
<p><strong>JoinTable</strong> <br />
JoinTable在many-to-many关系的所有者一边定义。如果没有定义JoinTable，使用JoinTable的默认值。 <br />
元数据属性说明： <br />
&#183;&nbsp;table:这个join table的Table定义。 <br />
&#183;&nbsp;joinColumns:定义指向所有者主表的外键列，数据类型是JoinColumn数组。 <br />
&#183;&nbsp;inverseJoinColumns:定义指向非所有者主表的外键列，数据类型是JoinColumn数组。 <br />
下面的代码定义了一个连接表CUST和PHONE的join table。join table的表名是CUST_PHONE，包含两个外键，一个外键是CUST_ID，指向表CUST的主键ID，另一个外键是PHONE_ID，指向表PHONE的主键ID。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@JoinTable(<br />
table=@Table(name=CUST_PHONE),<br />
joinColumns=@JoinColumn(name="CUST_ID", referencedColumnName="ID"),<br />
inverseJoinColumns=@JoinColumn(name="PHONE_ID", referencedColumnName="ID")<br />
)</p>
<p><strong>TableGenerator</strong> <br />
TableGenerator定义一个主键值生成器，在Id这个元数据的generate＝TABLE时，generator属性中可以使用生成器的名字。生成器可以在类、方法或者属性上定义。 <br />
生成器是为多个实体类提供连续的ID值的表，每一行为一个类提供ID值，ID值通常是整数。 <br />
元数据属性说明： <br />
&#183;&nbsp;name:生成器的唯一名字，可以被Id元数据使用。 <br />
&#183;&nbsp;table:生成器用来存储id值的Table定义。 <br />
&#183;&nbsp;pkColumnName:生成器表的主键名称。 <br />
&#183;&nbsp;valueColumnName:生成器表的ID值的列名称。 <br />
&#183;&nbsp;pkColumnValue:生成器表中的一行数据的主键值。 <br />
&#183;&nbsp;initialValue:id值的初始值。 <br />
&#183;&nbsp;allocationSize:id值的增量。 <br />
下面的代码定义了两个生成器empGen和addressGen，生成器的表是ID_GEN。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Entity public class Employee {<br />
...<br />
@TableGenerator(name="empGen",<br />
table=@Table(name="ID_GEN"),<br />
pkColumnName="GEN_KEY",<br />
valueColumnName="GEN_VALUE",<br />
pkColumnValue="EMP_ID",<br />
allocationSize=1)<br />
@Id(generate=TABLE, generator="empGen")<br />
public int id;<br />
...<br />
}<br />
@Entity public class Address {<br />
...<br />
@TableGenerator(name="addressGen",<br />
table=@Table(name="ID_GEN"),<br />
pkColumnValue="ADDR_ID")<br />
@Id(generate=TABLE, generator="addressGen")<br />
public int id;<br />
...<br />
}</p>
<p><strong>SequenceGenerator</strong> <br />
SequenceGenerator定义一个主键值生成器，在Id这个元数据的generator属性中可以使用生成器的名字。生成器可以在类、方法或者属性上定义。生成器是数据库支持的sequence对象。 <br />
元数据属性说明： <br />
&#183;&nbsp;name:生成器的唯一名字，可以被Id元数据使用。 <br />
&#183;&nbsp;sequenceName:数据库中，sequence对象的名称。如果不指定，会使用提供商指定的默认名称。 <br />
&#183;&nbsp;initialValue:id值的初始值。 <br />
&#183;&nbsp;allocationSize:id值的增量。 <br />
下面的代码定义了一个使用提供商默认名称的sequence生成器。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@SequenceGenerator(name="EMP_SEQ", allocationSize=25)</p>
<p><strong>DiscriminatorColumn </strong><br />
DiscriminatorColumn定义在使用SINGLE_TABLE或JOINED继承策略的表中区别不继承层次的列。 <br />
元数据属性说明： <br />
&#183;&nbsp;name:column的名字。默认值为TYPE。 <br />
&#183;&nbsp;columnDefinition:生成DDL的sql片断。 <br />
&#183;&nbsp;length:String类型的column的长度，其他类型使用默认值10。 <br />
下面的代码定义了一个列名为DISC，长度为20的String类型的区别列。 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
@Entity<br />
@Table(name="CUST")<br />
@Inheritance(strategy=SINGLE_TABLE,<br />
discriminatorType=STRING,<br />
discriminatorValue="CUSTOMER")<br />
@DiscriminatorColumn(name="DISC", length=20)<br />
public class Customer { ... }</p>
<p>&nbsp;</p><img src ="http://www.blogjava.net/rain1102/aggbug/253913.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rain1102/" target="_blank">Eric.Zhou</a> 2009-02-09 15:38 <a href="http://www.blogjava.net/rain1102/archive/2009/02/09/253913.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>EQL中使用case when..then..else..end和exists语句, 以及True和False</title><link>http://www.blogjava.net/rain1102/archive/2008/08/29/225667.html</link><dc:creator>Eric.Zhou</dc:creator><author>Eric.Zhou</author><pubDate>Fri, 29 Aug 2008 15:38:00 GMT</pubDate><guid>http://www.blogjava.net/rain1102/archive/2008/08/29/225667.html</guid><wfw:comment>http://www.blogjava.net/rain1102/comments/225667.html</wfw:comment><comments>http://www.blogjava.net/rain1102/archive/2008/08/29/225667.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/rain1102/comments/commentRss/225667.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rain1102/services/trackbacks/225667.html</trackback:ping><description><![CDATA[public List&lt;Meeting&gt; getRecentlyMeetingsByAudit(Long candidateId, Long ownerId) {<br />
&nbsp;&nbsp;&nbsp; List&lt;Meeting&gt; meetings = new ArrayList&lt;Meeting&gt;();<br />
&nbsp;&nbsp; &nbsp;meetings = mgr.createQuery("select new com.integral7.ejb3.investigations.Meeting(m.id, m.scheduledDate, m.type.name, m.description, " +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "<span style="color: red"><span style="color: #800080"><strong>case when</strong> </span><span style="color: #0000ff"><strong>exists</strong></span>(select id from MeetingAudit ma where ma.meeting = m and ma.audit.id = ?0) <span style="color: #800080"><strong>then</strong> </span><span style="color: #008000"><strong>True</strong></span> <span style="color: #800080"><strong>else </strong></span><span style="color: #008000"><strong>False</strong> </span><span style="color: #800080"><strong>end</strong></span></span>) " +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " from Meeting m " +<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " where m.owner.id = ?1 and m.scheduledDate &gt;= ?2 order by m.scheduledDate asc")<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .setParameter(0, candidateId)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .setParameter(1, ownerId)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .setParameter(2, DateUtils.convertToBeginningOfDay(new Date())).getResultList();<br />
&nbsp;&nbsp; &nbsp;return meetings;<br />
&nbsp;}<br />
<br />
这里主要注意<span style="color: #008000">True</span> 和<span style="color: #008000">False </span>首字母必须大写其余小写.<img src ="http://www.blogjava.net/rain1102/aggbug/225667.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rain1102/" target="_blank">Eric.Zhou</a> 2008-08-29 23:38 <a href="http://www.blogjava.net/rain1102/archive/2008/08/29/225667.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>配置Entity beans为缓存</title><link>http://www.blogjava.net/rain1102/archive/2008/06/13/207622.html</link><dc:creator>Eric.Zhou</dc:creator><author>Eric.Zhou</author><pubDate>Fri, 13 Jun 2008 03:55:00 GMT</pubDate><guid>http://www.blogjava.net/rain1102/archive/2008/06/13/207622.html</guid><wfw:comment>http://www.blogjava.net/rain1102/comments/207622.html</wfw:comment><comments>http://www.blogjava.net/rain1102/archive/2008/06/13/207622.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/rain1102/comments/commentRss/207622.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rain1102/services/trackbacks/207622.html</trackback:ping><description><![CDATA[<div><span style="font-size: 10.5pt; font-family: 宋体"  AR-SA New Times .5pt;>
<div><span style="font-size: 10.5pt; font-family: 宋体"  AR-SA New Times .5pt;>你用正常方式定义你的 entity bean 类。JBoss EJB 3.0 将来的版本将支持 annotating entities 和所缓存的它们的关系的集合，但是现在你不得不直接配置底层的 hibernate 引擎。让我们看看通过可选的property元素配置 hibernate 缓存选项的persistence.xml文件。下面persistence.xml 里的定义缓存的元素应该被启用：</span></div>
<span style="font-size: 10.5pt; font-family: 宋体"  AR-SA New Times .5pt;>
<div>
<div style="border-right: #cccccc 1px solid; padding-right: 4px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 10pt; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; color: #000000; word-break: break-all; line-height: 16px; padding-top: 4px; border-bottom: #cccccc 1px solid; font-family: verdana,宋体; background-color: #eeeeee">&lt;!--<font color="#008000"> Clustered cache with TreeCache </font>--&gt; <br />
<font color="#0000ff">&lt;</font><font color="#800000">property</font> <font color="#ff0000">name</font><font color="#0000ff">="cache.provider_class"</font><font color="#0000ff">&gt;</font> <br />
org.jboss.ejb3.entity.TreeCacheProviderHook <br />
<font color="#0000ff">&lt;/</font><font color="#800000">property</font><font color="#0000ff">&gt;</font></div>
</div>
<div>&nbsp;</div>
<div><span style="font-size: 10.5pt; font-family: 宋体"  AR-SA New Times .5pt;>下面的属性元素定义了所使用的缓存对象名和 MBean 名。</span></div>
<span style="font-size: 10.5pt; font-family: 宋体"  AR-SA New Times .5pt;>
<div>
<div style="border-right: #cccccc 1px solid; padding-right: 4px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 10pt; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; color: #000000; word-break: break-all; line-height: 16px; padding-top: 4px; border-bottom: #cccccc 1px solid; font-family: verdana,宋体; background-color: #eeeeee"><font color="#0000ff">&lt;</font><font color="#800000">property</font> <font color="#ff0000">name</font><font color="#0000ff">="treecache.mbean.object_name"</font><font color="#0000ff">&gt;</font> <br />
jboss.cache:service=EJB3EntityTreeCache <br />
<font color="#0000ff">&lt;/</font><font color="#800000">property</font><font color="#0000ff">&gt;</font></div>
</div>
<div>&nbsp;</div>
<div><span style="font-size: 10.5pt; font-family: 宋体"  AR-SA New Times .5pt;>下一步我们需要配置 entities 被缓存的内容。就像上面所展示的样，缺省是什么都不缓存。我们使用@Cache 注解来标记需要缓存的 entity beans。</span></div>
<span style="font-size: 10.5pt; font-family: 宋体"  AR-SA New Times .5pt;>
<div>
<div style="border-right: #cccccc 1px solid; padding-right: 4px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 10pt; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; color: #000000; word-break: break-all; line-height: 16px; padding-top: 4px; border-bottom: #cccccc 1px solid; font-family: verdana,宋体; background-color: #eeeeee">@Entity <br />
@Cache(usage=CacheConcurrencyStrategy.TRANSACTIONAL) <br />
<font color="#0000ff">public</font> <font color="#0000ff">class</font> Customer <font color="#0000ff">implements</font> Serializable { <br />
<font color="#008000">// ... ... </font><br />
}</div>
</div>
<div>&nbsp;</div>
<div><span style="font-size: 10.5pt; font-family: 宋体"  AR-SA New Times .5pt;>&nbsp;&nbsp;&nbsp; 一个简单的原则就是，你应该对很少变动和频繁使用的对象进行缓存.你可以在ejb3-entity-cache-service.xml配置文件里为每个 entity bean 微调缓存设置。例如，你可以指定缓存的大小。如果缓存里的对象太多，缓存有可能挤掉最老的对象（或者最少用的对象，依你的配置而定）来给新对象留出空间。mycompany.Customer entity bean 的缓存区（cache region）是/mycompany/Customer。</span></div>
<span style="font-size: 10.5pt; font-family: 宋体"  AR-SA New Times .5pt;>
<div>
<div style="border-right: #cccccc 1px solid; padding-right: 4px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 10pt; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; color: #000000; word-break: break-all; line-height: 16px; padding-top: 4px; border-bottom: #cccccc 1px solid; font-family: verdana,宋体; background-color: #eeeeee"><font color="#0000ff">&lt;</font><font color="#800000">server</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;</font><font color="#800000">mbean</font> <font color="#ff0000">code</font><font color="#0000ff">="org.jboss.cache.TreeCache"</font> <br />
<font color="#ff0000">name</font><font color="#0000ff">="jboss.cache:service=EJB3EntityTreeCache"</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;</font><font color="#800000">depends</font><font color="#0000ff">&gt;</font>jboss:service=Naming <br />
<font color="#0000ff">&lt;</font><font color="#800000">depends</font><font color="#0000ff">&gt;</font>jboss:service=TransactionManager <br />
... ... <br />
<font color="#0000ff">&lt;</font><font color="#800000">attribute</font> <font color="#ff0000">name</font><font color="#0000ff">="EvictionPolicyConfig"</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;</font><font color="#800000">config</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;</font><font color="#800000">attribute</font> <font color="#ff0000">name</font><font color="#0000ff">="wakeUpIntervalSeconds"</font><font color="#0000ff">&gt;</font>5<font color="#0000ff">&lt;/</font><font color="#800000">attribute</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;</font><font color="#800000">region</font> <font color="#ff0000">name</font><font color="#0000ff">="/_default_"</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;</font><font color="#800000">attribute</font> <font color="#ff0000">name</font><font color="#0000ff">="maxNodes"</font><font color="#0000ff">&gt;</font>5000<font color="#0000ff">&lt;/</font><font color="#800000">attribute</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;</font><font color="#800000">attribute</font> <font color="#ff0000">name</font><font color="#0000ff">="timeToLiveSeconds"</font><font color="#0000ff">&gt;</font>1000<font color="#0000ff">&lt;/</font><font color="#800000">attribute</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;/</font><font color="#800000">region</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;</font><font color="#800000">region</font> <font color="#ff0000">name</font><font color="#0000ff">="/mycompany/Customer"</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;</font><font color="#800000">attribute</font> <font color="#ff0000">name</font><font color="#0000ff">="maxNodes"</font><font color="#0000ff">&gt;</font>10<font color="#0000ff">&lt;/</font><font color="#800000">attribute</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;</font><font color="#800000">attribute</font> <font color="#ff0000">name</font><font color="#0000ff">="timeToLiveSeconds"</font><font color="#0000ff">&gt;</font>5000<font color="#0000ff">&lt;/</font><font color="#800000">attribute</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;/</font><font color="#800000">region</font><font color="#0000ff">&gt;</font> <br />
... ... <br />
<font color="#0000ff">&lt;/</font><font color="#800000">config</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;/</font><font color="#800000">attribute</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;/</font><font color="#800000">mbean</font><font color="#0000ff">&gt;</font> <br />
<font color="#0000ff">&lt;/</font><font color="#800000">server</font><font color="#0000ff">&gt;</font></div>
</div>
</span></span></span></span>
<div style="margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 150%"><span style="font-family: 宋体"><font style="font-size: 10pt" size="3">如果你没有为 entity bean 类指定缓存区（cache region），这个类的所有实例将象上面定义的一样缓存在/_default区里。EJB3 Query API 提供了让你在指定的缓存区里保存或载入查询结果（就是 entity beans 的集合）的方法。</font></span></span></div>
</div><img src ="http://www.blogjava.net/rain1102/aggbug/207622.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rain1102/" target="_blank">Eric.Zhou</a> 2008-06-13 11:55 <a href="http://www.blogjava.net/rain1102/archive/2008/06/13/207622.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>EJB3.0初步之环境配置</title><link>http://www.blogjava.net/rain1102/archive/2006/11/23/82956.html</link><dc:creator>Eric.Zhou</dc:creator><author>Eric.Zhou</author><pubDate>Thu, 23 Nov 2006 01:45:00 GMT</pubDate><guid>http://www.blogjava.net/rain1102/archive/2006/11/23/82956.html</guid><wfw:comment>http://www.blogjava.net/rain1102/comments/82956.html</wfw:comment><comments>http://www.blogjava.net/rain1102/archive/2006/11/23/82956.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/rain1102/comments/commentRss/82956.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/rain1102/services/trackbacks/82956.html</trackback:ping><description><![CDATA[<p>开发EJB3.0，当然要先配置环境，那我们就来吧！</p>
		<p>先下载JBoss4.0.3RC2版本，他们说已经包含了EJB3.0的容器，可是我没看到。也罢!我们就先配置一下。</p>
		<p>如果在这之前版本，肯定是要配置的！</p>
		<p>1、首先要保证你已经下载了jboss和ejb3.0，先装jboss，这个不用教吧？</p>
		<p>2、解压缩ejb包，然后坐一下动作：</p>
		<p>从ejb的lib/目录下把ejb3.deployer 、ejb3-clustered-sfsbcache-service.xml 、ejb3-entity-cache-service.xml 、ejb3-entity-cache-service.xml 、ejb3-interceptors-aop.xml 、jboss-aop-jdk50.deployer 复制到jboss-4.0.x/server/all/deploy 目录下；</p>
		<p>把jboss-xb.jar 复制到jboss-4.0.x/server/all/lib 目录下；</p>
		<p>把jboss-aop.deployer/目录从jboss-4.0.x/server/all/deploy 中删除。</p>
		<p>3、启动jboss：run -c all </p>
		<p>4、打开ejb3.0的目录中docs/tutorials目录，里面有mdb目录，打开，有build.xml文件，哈哈，就是这个地方！</p>
		<p>装了ANT了吗？没有，那你下一个吧！然后，设置path，让系统能够找到他！</p>
		<p>然后，在mdb目录下运行ANT，如果没问题，那说明你的环境变量没问题。</p>
		<p>下一步，别急着干别的，先运行jboss，接着再运行ANT&#160; run。</p>
		<p>没问题吧</p><img src ="http://www.blogjava.net/rain1102/aggbug/82956.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/rain1102/" target="_blank">Eric.Zhou</a> 2006-11-23 09:45 <a href="http://www.blogjava.net/rain1102/archive/2006/11/23/82956.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>