In the previous post we saw several of the JPA annotations. The one that occurs most commonly is the Column annotation can be used without any attributes.
The unique attribute if true indicates that the property is a unique key. It is actually a shortcut to the UniqueConstraint attribute that we saw in the Table annotation. You cannot use the unique attribute if the unique key is a combination of two columns. In that case UniqueConstraint is the only option. This attribute only affected the DDL generated.
The insertable and updatable attributes indicate if a column must be included in the SQL insert and SQL update queries generated for the entity.
The length attribute is column length, it is used with string valued columns. There are some more such attributes which are closely associated with the sql definition. The java code is :
The last is a table attribute. It is the table name in the database.
@Column public String getTagId() {Consider the name property in the Pet class:
@Column(name = "NAME", length = 12, nullable = false, insertable = true, unique = false, updatable = true) public String getName() { return name; }The name attribute is the name of the column. The nullable attribute indicates if a column allows null values. If this attribute is set to true, then the DDL generated will include a not-null column constraint. Also just as we saw for optional, the entity will be checked before saving to ensure that the not null constraint is satisfied. Hibernate provided a java side check for the column before the insert query was fired.
The unique attribute if true indicates that the property is a unique key. It is actually a shortcut to the UniqueConstraint attribute that we saw in the Table annotation. You cannot use the unique attribute if the unique key is a combination of two columns. In that case UniqueConstraint is the only option. This attribute only affected the DDL generated.
The insertable and updatable attributes indicate if a column must be included in the SQL insert and SQL update queries generated for the entity.
The length attribute is column length, it is used with string valued columns. There are some more such attributes which are closely associated with the sql definition. The java code is :
@Entity() @Table(name = "SAMPLE", schema="firstOne") public class Creation { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "salary", scale = 2, precision = 6) private BigDecimal salary; @Column(name = "name", columnDefinition = "char(25)") private String name; @Column(name = "code", length =30) private String code; //...more code }The DDL generated is :
5141 [main] DEBUG org.hibernate.tool.hbm2ddl.SchemaExport - create table firstOne.SAMPLE ( id bigserial not null, code varchar(30), name char(25), salary numeric(6, 2), primary key (id) )As seen the scale and precision attributes were used with the decimal columns. columnDefinition attribute allows to specify the actual database specific data type to be used. This goes directly into the DDL.
The last is a table attribute. It is the table name in the database.
No comments:
Post a Comment