String columnDefinition The SQL fragment that is used when generating the DDL for the column. boolean insertable Whether the column is included in SQL INSERT statements generated by the persistence provider. int length The column length. String name The name of the column. boolean nullable Whether the database column is nullable. int precision The precision for a decimal (exact numeric) column. int scale The scale for a decimal (exact numeric) column. String table The name of the table that contains the column. boolean unique Whether the property is a unique key. boolean updatable Whether the column is included in SQL UPDATE statements generated by the persistence provider.
@Temporal(TemporalType.DATE)//设置为时间类型 private Date joinDate;
/** API:An orderby_list, specified as follows: orderby_list::= orderby_item [,orderby_item]* orderby_item::= property_or_field_name [ASC | DESC] If ASC or DESC is not specified, ASC (ascending order) is assumed. * 6.字段排序 在加载数据的时候可以为其指定顺序,使用@OrderBy注解实现 这个个人感觉用的还是比较少的。所以就没深入。如果有人用的比较好。希望补充 */
@Table(name = "USERS") public class User { @OrderBy(name = "group_name ASC, name DESC") private List books = new ArrayList(); }
/** * ManyToOne API: * CascadeType[] cascade The operations that must be cascaded to the target of the association. FetchType fetch Whether the association should be lazily loaded or must be eagerly fetched. boolean optional Whether the association is optional. Class targetEntity The entity class that is the target of the association. * 一对多映射关系 * 有Group和User两个表。先是来映射一对多。GROUP是一,User是多。一个组里面可以包含多个用户。但是一个用户只能属于一个组。 * 为了方便。其他属性就简单映射了 */ /** * @ManyToOne 默认情况下,JPA 为指向具有多对一多重性的其他实体类的单值关联自动定义一个 ManyToOne 映射。 使用 @ManyToOne 批注: 将获取类型配置为 LAZY 如果空值不适合于应用程序,则将映射配置为禁止空值(针对非基元类型) 配置关联的目标实体(如果无法从被引用的对象类型推断出它) 配置必须层叠到关联目标的操作:例如,如果删除了拥有实体,则确保还删除关联的目标
public class Group { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; @Column private String groupName; @OneToMany(mappedBy="group")//fetch及cascade都有默认值,如果不需要设置,可以不写 private List<User> users; }
public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; @Column private String userName; @Temporal(TemporalType.DATE) private Date birthday; @ManyToOne @JoinColumn(name="groupId") private Group group; }