JPA注解:根据实体生成数据表和字段的注释(正向工程)

1.JPA常见注解
2.JPA注解:表注释
   
   
  1. @org.hibernate.annotations.Table(appliesTo = "TableName",comment="表注释")
    
    
  1. /*
  2. * Hibernate, Relational Persistence for Idiomatic Java
  3. *
  4. * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  5. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  6. */
  7. package org.hibernate.annotations;
  8. import java.lang.annotation.Retention;
  9. import java.lang.annotation.Target;
  10. import static java.lang.annotation.ElementType.TYPE;
  11. import static java.lang.annotation.RetentionPolicy.RUNTIME;
  12. /**
  13. * Complementary information to a table either primary or secondary.
  14. *
  15. * @author Emmanuel Bernard
  16. */
  17. @Target({TYPE})
  18. @Retention(RUNTIME)
  19. public @interface Table {
  20. /**
  21. * name of the targeted table.
  22. */
  23. String appliesTo();
  24. /**
  25. * Indexes.
  26. */
  27. Index[] indexes() default {};
  28. /**
  29. * define a table comment.
  30. */
  31. String comment() default "";
  32. /**
  33. * Defines the Foreign Key name of a secondary table pointing back to the primary table.
  34. */
  35. ForeignKey foreignKey() default @ForeignKey( name="" );
  36. /**
  37. * If set to JOIN, the default, Hibernate will use an inner join to retrieve a
  38. * secondary table defined by a class or its superclasses and an outer join for a
  39. * secondary table defined by a subclass.
  40. * If set to select then Hibernate will use a
  41. * sequential select for a secondary table defined on a subclass, which will be issued only if a row
  42. * turns out to represent an instance of the subclass. Inner joins will still be used to retrieve a
  43. * secondary defined by the class and its superclasses.
  44. *
  45. * <b>Only applies to secondary tables</b>
  46. */
  47. FetchMode fetch() default FetchMode.JOIN;
  48. /**
  49. * If true, Hibernate will not try to insert or update the properties defined by this join.
  50. *
  51. * <b>Only applies to secondary tables</b>
  52. */
  53. boolean inverse() default false;
  54. /**
  55. * If enabled, Hibernate will insert a row only if the properties defined by this join are non-null
  56. * and will always use an outer join to retrieve the properties.
  57. *
  58. * <b>Only applies to secondary tables</b>
  59. */
  60. boolean optional() default true;
  61. /**
  62. * Defines a custom SQL insert statement.
  63. *
  64. * <b>Only applies to secondary tables</b>
  65. */
  66. SQLInsert sqlInsert() default @SQLInsert(sql="");
  67. /**
  68. * Defines a custom SQL update statement.
  69. *
  70. * <b>Only applies to secondary tables</b>
  71. */
  72. SQLUpdate sqlUpdate() default @SQLUpdate(sql="");
  73. /**
  74. * Defines a custom SQL delete statement.
  75. *
  76. * <b>Only applies to secondary tables</b>
  77. */
  78. SQLDelete sqlDelete() default @SQLDelete(sql="");
  79. }


3.JPA注解:字段注释
    
    
  1. @Column(name="columnComment",columnDefinition="varchar(200) COMMENT '字段注释'")
     
     
  1. /*
  2. * Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved.
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
  6. * which accompanies this distribution. The Eclipse Public License is available
  7. * at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
  8. * is available at http://www.eclipse.org/org/documents/edl-v10.php.
  9. */
  10. package javax.persistence;
  11. import java.lang.annotation.Retention;
  12. import java.lang.annotation.Target;
  13. import static java.lang.annotation.ElementType.FIELD;
  14. import static java.lang.annotation.ElementType.METHOD;
  15. import static java.lang.annotation.RetentionPolicy.RUNTIME;
  16. /**
  17. * Is used to specify the mapped column for a persistent property or field.
  18. * If no <code>Column</code> annotation is specified, the default values apply.
  19. *
  20. * <blockquote><pre>
  21. * Example 1:
  22. *
  23. * &#064;Column(name="DESC", nullable=false, length=512)
  24. * public String getDescription() { return description; }
  25. *
  26. * Example 2:
  27. *
  28. * &#064;Column(name="DESC",
  29. * columnDefinition="CLOB NOT NULL",
  30. * table="EMP_DETAIL")
  31. * &#064;Lob
  32. * public String getDescription() { return description; }
  33. *
  34. * Example 3:
  35. *
  36. * &#064;Column(name="ORDER_COST", updatable=false, precision=12, scale=2)
  37. * public BigDecimal getCost() { return cost; }
  38. *
  39. * </pre></blockquote>
  40. *
  41. *
  42. * @since Java Persistence 1.0
  43. */
  44. @Target({METHOD, FIELD})
  45. @Retention(RUNTIME)
  46. public @interface Column {
  47. /**
  48. * (Optional) The name of the column. Defaults to
  49. * the property or field name.
  50. */
  51. String name() default "";
  52. /**
  53. * (Optional) Whether the column is a unique key. This is a
  54. * shortcut for the <code>UniqueConstraint</code> annotation at the table
  55. * level and is useful for when the unique key constraint
  56. * corresponds to only a single column. This constraint applies
  57. * in addition to any constraint entailed by primary key mapping and
  58. * to constraints specified at the table level.
  59. */
  60. boolean unique() default false;
  61. /**
  62. * (Optional) Whether the database column is nullable.
  63. */
  64. boolean nullable() default true;
  65. /**
  66. * (Optional) Whether the column is included in SQL INSERT
  67. * statements generated by the persistence provider.
  68. */
  69. boolean insertable() default true;
  70. /**
  71. * (Optional) Whether the column is included in SQL UPDATE
  72. * statements generated by the persistence provider.
  73. */
  74. boolean updatable() default true;
  75. /**
  76. * (Optional) The SQL fragment that is used when
  77. * generating the DDL for the column.
  78. * <p> Defaults to the generated SQL to create a
  79. * column of the inferred type.
  80. */
  81. String columnDefinition() default "";
  82. /**
  83. * (Optional) The name of the table that contains the column.
  84. * If absent the column is assumed to be in the primary table.
  85. */
  86. String table() default "";
  87. /**
  88. * (Optional) The column length. (Applies only if a
  89. * string-valued column is used.)
  90. */
  91. int length() default 255;
  92. /**
  93. * (Optional) The precision for a decimal (exact numeric)
  94. * column. (Applies only if a decimal column is used.)
  95. * Value must be set by developer if used when generating
  96. * the DDL for the column.
  97. */
  98. int precision() default 0;
  99. /**
  100. * (Optional) The scale for a decimal (exact numeric) column.
  101. * (Applies only if a decimal column is used.)
  102. */
  103. int scale() default 0;
  104. }

4.示例:

4.1实体(实体中使用了Swagger,不需要的可去掉):
     
     
  1. package com.newcapec.dao.domain;
  2. import io.swagger.annotations.ApiModel;
  3. import io.swagger.annotations.ApiModelProperty;
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.Id;
  8. import java.util.Date;
  9. import static javax.persistence.GenerationType.AUTO;
  10. /**
  11. * @Title: 用户报名缴费信息实体
  12. * @ClassName: com.newcapec.dao.domain.UserEnrollPay.java
  13. * @Description: 位于校内的报名系统--中间库上
  14. *
  15. * @Copyright 2016-2017 新开普 - Powered By 研发中心
  16. * @author: 王延飞
  17. * @date: 2017-12-13 15:31
  18. * @version V1.0
  19. */
  20. @Entity
  21. @org.hibernate.annotations.Table(appliesTo = "user_enroll_pay",comment="用户报名缴费信息")
  22. @ApiModel("Person(用户报名缴费信息)")
  23. public class UserEnrollPay {
  24. @Id
  25. @GeneratedValue(strategy = AUTO)
  26. @Column(name = "id", unique = true, nullable = false,columnDefinition="bigint(20) COMMENT '主键id'")
  27. @ApiModelProperty("主键id")
  28. private Long id;
  29. /**
  30. * 证件号
  31. */
  32. @Column(columnDefinition="varchar(20) COMMENT '证件号'")
  33. @ApiModelProperty("证件号")
  34. private String idserial;
  35. /**
  36. * 订单金额
  37. */
  38. @Column(columnDefinition = "Decimal(10,2) COMMENT '订单金额'", scale = 2 ,precision=10)
  39. @ApiModelProperty("订单金额")
  40. private double orderamt;
  41. /**
  42. * 支付时间
  43. */
  44. @Column(columnDefinition="datetime COMMENT '支付时间'")
  45. @ApiModelProperty("支付时间")
  46. private Date paytime;
  47. /**
  48. * 支付标志
  49. * 0-未支付
  50. * 1-支付中
  51. * 2-支付成功
  52. * 3-支付失败
  53. * 4-已过期
  54. * 5-已取消
  55. */
  56. @Column(columnDefinition="int(2) COMMENT '支付标志 0-未支付 1-支付中 2-支付成功 3-支付失败 4-已过期 5-已取消'")
  57. @ApiModelProperty("支付标志 0-未支付 1-支付中 2-支付成功 3-支付失败 4-已过期 5-已取消")
  58. private Integer payflag;
  59. public Long getId() {
  60. return id;
  61. }
  62. public void setId(Long id) {
  63. this.id = id;
  64. }
  65. public String getIdserial() {
  66. return idserial;
  67. }
  68. public void setIdserial(String idserial) {
  69. this.idserial = idserial;
  70. }
  71. public double getOrderamt() {
  72. return orderamt;
  73. }
  74. public void setOrderamt(double orderamt) {
  75. this.orderamt = orderamt;
  76. }
  77. public Date getPaytime() {
  78. return paytime;
  79. }
  80. public void setPaytime(Date paytime) {
  81. this.paytime = paytime;
  82. }
  83. public Integer getPayflag() {
  84. return payflag;
  85. }
  86. public void setPayflag(Integer payflag) {
  87. this.payflag = payflag;
  88. }
  89. @Override
  90. public String toString() {
  91. return "UserEnrollPay{" +
  92. "id=" + id +
  93. ", idserial='" + idserial + '\'' +
  94. ", orderamt=" + orderamt +
  95. ", paytime=" + paytime +
  96. ", payflag=" + payflag +
  97. '}';
  98. }
  99. }

4.2生成的数据表
      
      
  1. CREATE TABLE `user_enroll_pay` (
  2. `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  3. `idserial` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '证件号',
  4. `orderamt` decimal(10,2) DEFAULT NULL COMMENT '订单金额',
  5. `payflag` int(2) DEFAULT NULL COMMENT '支付标志 0-未支付 1-支付中 2-支付成功 3-支付失败 4-已过期 5-已取消',
  6. `paytime` datetime DEFAULT NULL COMMENT '支付时间',
  7. PRIMARY KEY (`id`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='用户报名缴费信息';
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
可以按照以下步骤生成JPA实体类: 1. 定义实体类 首先,在Java Package中创建一个新类,该类将成为您的数据表实体类。这个类应该包含所有的字段,以及它们的getter和setter方法。 例如,如果您要创建一个名为“Person”的实体类,可以包含以下字段: ``` @Column(name="id") private Long id; @Column(name="name") private String name; @Column(name="age") private int age; ``` 2. 定义主键 在JPA中,每个实体类都必须有一个主键。您可以使用@Id注解来定义主键字段。 例如: ``` @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") private Long id; ``` 这个例子中,我们使用@Id注解来标识id字段作为主键,并使用@GeneratedValue注解来指定主键的生成策略。 3. 定义关联关系 如果您的数据表与其他有关联关系,例如一对多或多对多关系,您需要使用@OneToMany、@ManyToOne或@ManyToMany注解来定义它们。 例如,如果您的Person实体类需要与Address实体类建立一对多关系,可以使用以下代码: ``` @OneToMany(mappedBy = "person", cascade = CascadeType.ALL) private List<Address> addresses; ``` 这个例子中,使用@OneToMany注解来定义一对多关系,并使用mappedBy属性来指定关系的反方向属性。 4. 定义其他注解 您还可以使用其他JPA注解来定义实体类,例如@Table、@Column、@Temporal等。 例如,如果您要指定数据表的名称和日期字段的类型,可以使用以下代码: ``` @Entity @Table(name = "person") public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") private Long id; @Column(name="name") private String name; @Column(name="age") private int age; @Temporal(TemporalType.DATE) @Column(name="date_of_birth") private Date dateOfBirth; // ... } ``` 这个例子中,我们使用@Entity和@Table注解指定实体类对应的数据表和名称,使用@Temporal注解指定日期字段的类型。 5. 生成数据库 最后,您可以使用JPA提供的自动创建功能来生成数据库。只需在应用程序启动时添加以下代码即可: ``` EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-persistence-unit"); EntityManager em = emf.createEntityManager(); ``` 这个例子中,我们使用Persistence.createEntityManagerFactory()方法创建一个EntityManagerFactory对象,并使用它来创建一个EntityManager对象。您可以使用EntityManager对象来执行各种数据库操作,例如插入、更新、删除和查询数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

琦彦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值