Java Persistence API中定义了两个注解@Entity与@Table
@Entity表明这是一个实体类,要与数据库做orm映射,默认表的名字就是类名,表中的字段就是类中的属性。它的定义如下:其中name属性表示用JPQL语句时写的表的名字,如果没有在@Table注解中指定表名,这个名字也将作为表名映射到数据库
@Documented
@Target(TYPE)
@Retention(RUNTIME)
public @interface Entity {
/**
* (Optional) The entity name. Defaults to the unqualified
* name of the entity class. This name is used to refer to the
* entity in queries. The name must not be a reserved literal
* in the Java Persistence query language.
*/
String name() default "";
}
@Table 就是改变某些默认的映射规则,如表名,schema等,可以添加索引和约束,看一下它的定义
@Target(TYPE)
@Retention(RUNTIME)
public @interface Table {
/**
* (Optional) The name of the table.
* <p/>
* Defaults to the entity name.
*/
String name() default "";
/**
* (Optional) The catalog of the table.
* <p/>
* Defaults to the default catalog.
*/
String catalog() default "";
/**
* (Optional) The schema of the table.
* <p/>
* Defaults to the default schema for user.
*/
String schema() default "";
/**
* (Optional) Unique constraints that are to be placed on
* the table. These are only used if table generation is in
* effect. These constraints apply in addition to any constraints
* specified by the <code>Column</code> and <code>JoinColumn</code>
* annotations and constraints entailed by primary key mappings.
* <p/>
* Defaults to no additional constraints.
*/
UniqueConstraint[] uniqueConstraints() default { };
/**
* (Optional) Indexes for the table. These are only used if table generation is in effect. Defaults to no
* additional indexes.
*
* @return The indexes
*/
Index[] indexes() default {};
}
name: 用来指定要映射的表名
catalog: 指定table数据库中的路径,这里指数据库名
schema: 映射到哪个schema,用户名
uniqueConstraints: 可以创建单个字段或者联合的唯一约束,只有在创建表成功时才执行
indexes:索引,可以指定联合索引,也是只有表生成成功才有效
我的实体类代码,(只为了演示注解效果,在表结构设计上是非常不规范的,没有实际意义)
package com.xhx.springboot.entity;
import javax.persistence.*;
/**
* @author xuhaixing
* @date 2018/4/28 10:29
*/
@Entity(name = "Acc")
@Table(name = "account", //表名称
catalog = "mytest2", //数据库名
schema = "xuhaixing", //用户名
uniqueConstraints = { //联合或者单个字段 唯一约束
@UniqueConstraint(columnNames = {"name","money"}), @UniqueConstraint(columnNames = {"name"})
},
indexes = {
@Index(columnList = "name,version")
}
)
public class Account {
@Id
private int id;
private String name;
private Double money;
@Version
private int version;
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
程序启动过程中执行的sql语句:
Hibernate: create table mytest2.account (id integer not null, money double precision, name varchar(255), version integer not null, primary key (id)) engine=MyISAM
Hibernate: create index IDXpdrwpyis5o4yn1f3n52oc756e on mytest2.account (name, version)
Hibernate: alter table mytest2.account drop index UKdqfeeghd5jfgxqfxae9urq0em
Hibernate: alter table mytest2.account add constraint UKdqfeeghd5jfgxqfxae9urq0em unique (name, money)
Hibernate: alter table mytest2.account drop index UKbb9lrmwswqvhcy1y430ki00ir
Hibernate: alter table mytest2.account add constraint UKbb9lrmwswqvhcy1y430ki00ir unique (name)
UniqueConstraint的源码如下,可以用name指定约束的名字
@Target({ })
@Retention(RUNTIME)
public @interface UniqueConstraint {
/**
* (Optional) Constraint name. A provider-chosen name will be chosen
* if a name is not specified.
*
* @since Java Persistence 2.0
*/
String name() default "";
/**
* (Required) An array of the column names that make up the constraint.
*/
String[] columnNames();
}
Index注解源码如下,还可以用name指定索引的名字
@Target({}) @Retention(RUNTIME)
public @interface Index {
/**
* (Optional) The name of the index. Defaults to a provider-generated value.
*
* @return The index name
*/
String name() default "";
/**
* (Required) The names of the columns to be included in the index.
*
* @return The names of the columns making up the index
*/
String columnList();
/**
* (Optional) Whether the index is unique. Default is false.
*
* @return Is the index unique?
*/
boolean unique() default false;
}
我的dao层代码:
package com.xhx.springboot.dao;
import com.xhx.springboot.entity.Account;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
/**
* @author xuhaixing
* @date 2018/5/2 11:19
*/
@Repository
public interface AccountDao extends JpaRepository<Account, Integer> {
@Modifying
@Query("update Acc set name=:name, money=:money where id=:id")
int updateAccount(@Param("id") int id,@Param("name") String name, @Param("money") double money);
@Modifying //通知jpa这是一个update或者delete操作,jpql不支持insert操作
@Query("update Acc set name=:name, money=:money,version=:version+1 where id=:id and version=:version")
int updateAccountByVersion(@Param("id") int id,@Param("name") String name, @Param("money") double money,@Param("version") int version);
@Query(value = "select count(*) from account",nativeQuery = true)//使用原生sql进行查询
long selectCount();
}
至此,@Table和@Entity的注解所有属性就讲解完了
实时内容请关注微信公众号,公众号与博客同时更新:程序员星星