JPA注解:根据实体生成数据表和字段的注释(正向工程)
简介:
JPA注解:根据实体生成数据表和字段的注释(正向工程)
1.JPA常见注解
请移步:http://blog.csdn.net/fly910905/article/details/78140411
2.JPA注解:表注释
@org.hibernate.annotations.Table(appliesTo = "TableName",comment="表注释")
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License(LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
importstatic java.lang.annotation.ElementType.TYPE;
importstatic java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Complementary information to a table either primary or secondary.
*
* @author Emmanuel Bernard
*/
@Target({TYPE})
@Retention(RUNTIME)
public@interface Table {
/**
* name of the targeted table.
*/
String appliesTo();
/**
* Indexes.
*/
Index[] indexes() default {};
/**
* define a table comment.
*/
String comment()default"";
/**
* Defines the Foreign Key name of a secondary table pointing back to the primary table.
*/
ForeignKey foreignKey()default@ForeignKey( name="" );
/**
* If set to JOIN, the default, Hibernate will use an inner join to retrieve a
* secondary table defined by a classor its superclasses and an outer join for a
* secondary table defined by a subclass.
* If set to select then Hibernate will use a
* sequential select for a secondary table defined on a subclass, which will be issued only if a row
* turns out to represent an instance of the subclass. Inner joins will still be used to retrieve a
* secondary defined by the classand its superclasses.
*
* <b>Only applies to secondary tables</b>
*/
FetchMode fetch()default FetchMode.JOIN;
/**
* If true, Hibernate will not try to insert or update the properties defined by this join.
*
* <b>Only applies to secondary tables</b>
*/
booleaninverse()defaultfalse;
/**
* If enabled, Hibernate will insert a row only if the properties defined by this join are non-null
* and will always use an outer join to retrieve the properties.
*
* <b>Only applies to secondary tables</b>
*/
booleanoptional()defaulttrue;
/**
* Defines a custom SQL insert statement.
*
* <b>Only applies to secondary tables</b>
*/
SQLInsert sqlInsert()default@SQLInsert(sql="");
/**
* Defines a custom SQL update statement.
*
* <b>Only applies to secondary tables</b>
*/
SQLUpdate sqlUpdate()default@SQLUpdate(sql="");
/**
* Defines a custom SQL delete statement.
*
* <b>Only applies to secondary tables</b>
*/
SQLDelete sqlDelete()default@SQLDelete(sql="");
}
3.JPA注解:字段注释
@Column(name="columnComment",columnDefinition="varchar(200) COMMENT '字段注释'")
/*
* Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution. The Eclipse Public License is available
* at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
* is available at http://www.eclipse.org/org/documents/edl-v10.php.
*/
package javax.persistence;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
importstatic java.lang.annotation.ElementType.FIELD;
importstatic java.lang.annotation.ElementType.METHOD;
importstatic java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Is used to specify the mapped column for a persistent property or field.
* If no <code>Column</code> annotation is specified, the default values apply.
*
* <blockquote><pre>
* Example 1:
*
* @Column(name="DESC", nullable=false, length=512)
* public String getDescription() { return description; }
*
* Example 2:
*
* @Column(name="DESC",
* columnDefinition="CLOB NOT NULL",
* table="EMP_DETAIL")
* @Lob
* public String getDescription() { return description; }
*
* Example 3:
*
* @Column(name="ORDER_COST", updatable=false, precision=12, scale=2)
* public BigDecimal getCost() { return cost; }
*
* </pre></blockquote>
*
*
* @since Java Persistence 1.0
*/
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public@interface Column {
/**
* (Optional) The name of the column. Defaults to
* the property or field name.
*/
String name()default"";
/**
* (Optional) Whether the column is a unique key. This is a
* shortcut for the <code>UniqueConstraint</code> annotation at the table
* level and is useful for when the unique key constraint
* corresponds to only a single column. This constraint applies
* in addition to any constraint entailed by primary key mapping and
* to constraints specified at the table level.
*/
booleanunique()defaultfalse;
/**
* (Optional) Whether the database column is nullable.
*/
booleannullable()defaulttrue;
/**
* (Optional) Whether the column is included in SQL INSERT
* statements generated by the persistence provider.
*/
booleaninsertable()defaulttrue;
/**
* (Optional) Whether the column is included in SQL UPDATE
* statements generated by the persistence provider.
*/
booleanupdatable()defaulttrue;
/**
* (Optional) The SQL fragment that is used when
* generating the DDL for the column.
* <p> Defaults to the generated SQL to create a
* column of the inferred type.
*/
String columnDefinition()default"";
/**
* (Optional) The name of the table that contains the column.
* If absent the column is assumed to be in the primary table.
*/
String table()default"";
/**
* (Optional) The column length. (Applies only if a
* string-valued column is used.)
*/
intlength()default255;
/**
* (Optional) The precision for a decimal(exact numeric)
* column. (Applies only if a decimal column is used.)
* Value must be set by developer if used when generating
* the DDL for the column.
*/
intprecision()default0;
/**
* (Optional) The scale for a decimal(exact numeric) column.
* (Applies only if a decimal column is used.)
*/
intscale()default0;
}
4.示例:
4.1实体(实体中使用了Swagger,不需要的可去掉):
package com.newcapec.dao.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
importstatic javax.persistence.GenerationType.AUTO;
/**
* @Title: 用户报名缴费信息实体
* @ClassName: com.newcapec.dao.domain.UserEnrollPay.java
* @Description: 位于校内的报名系统--中间库上
*
* @Copyright2016-2017 新开普 - Powered By 研发中心
* @author: 王延飞
* @date: 2017-12-1315:31
* @version V1.0
*/
@Entity
@org.hibernate.annotations.Table(appliesTo = "user_enroll_pay",comment="用户报名缴费信息")
@ApiModel("Person(用户报名缴费信息)")
publicclassUserEnrollPay {
@Id
@GeneratedValue(strategy = AUTO)
@Column(name = "id", unique = true, nullable = false,columnDefinition="bigint(20) COMMENT '主键id'")
@ApiModelProperty("主键id")
private Long id;
/**
* 证件号
*/
@Column(columnDefinition="varchar(20) COMMENT '证件号'")
@ApiModelProperty("证件号")
private String idserial;
/**
* 订单金额
*/
@Column(columnDefinition = "Decimal(10,2) COMMENT '订单金额'", scale = 2 ,precision=10)
@ApiModelProperty("订单金额")
privatedouble orderamt;
/**
* 支付时间
*/
@Column(columnDefinition="datetime COMMENT '支付时间'")
@ApiModelProperty("支付时间")
private Date paytime;
/**
* 支付标志
* 0-未支付
* 1-支付中
* 2-支付成功
* 3-支付失败
* 4-已过期
* 5-已取消
*/
@Column(columnDefinition="int(2) COMMENT '支付标志 0-未支付 1-支付中 2-支付成功 3-支付失败 4-已过期 5-已取消'")
@ApiModelProperty("支付标志 0-未支付 1-支付中 2-支付成功 3-支付失败 4-已过期 5-已取消")
private Integer payflag;
public Long getId() {
return id;
}
publicvoidsetId(Long id) {
this.id = id;
}
public String getIdserial() {
return idserial;
}
publicvoidsetIdserial(String idserial) {
this.idserial = idserial;
}
publicdoublegetOrderamt() {
return orderamt;
}
publicvoidsetOrderamt(double orderamt) {
this.orderamt = orderamt;
}
public Date getPaytime() {
return paytime;
}
publicvoidsetPaytime(Date paytime) {
this.paytime = paytime;
}
public Integer getPayflag() {
return payflag;
}
publicvoidsetPayflag(Integer payflag) {
this.payflag = payflag;
}
@Override
public String toString() {
return"UserEnrollPay{" +
"id=" + id +
", idserial='" + idserial + '\'' +
", orderamt=" + orderamt +
", paytime=" + paytime +
", payflag=" + payflag +
'}';
}
}
4.2生成的数据表
CREATETABLE `user_enroll_pay` (
`id` bigint(20) NOTNULL AUTO_INCREMENT COMMENT '主键id',
`idserial` varchar(20) COLLATE utf8_unicode_ci DEFAULTNULL COMMENT '证件号',
`orderamt` decimal(10,2) DEFAULTNULL COMMENT '订单金额',
`payflag` int(2) DEFAULTNULL COMMENT '支付标志 0-未支付 1-支付中 2-支付成功 3-支付失败 4-已过期 5-已取消',
`paytime` datetime DEFAULTNULL COMMENT '支付时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='用户报名缴费信息';