Java – use annotations in hibernate to define default column values

I know there are many questions about so and network, but all the answers suggest using column definition, which is database specific, so it is not suitable for me, because the system I am studying needs to run on different databases

I found that someone in the hibernate issue requested to annotate this function The question is over, saying that another issue will cover the function The second problem obviously adds the @ generated and other comments, but I can't find any documentation on how to use these new comments to define the default column values

So my question is: who knows how to use annotations to define default column values (instead of columndefinition)?

Editor: to further clarify my question: when I add a new non empty column, I need hibernate to update the existing schema (add the new column to the corresponding table) However, because the column is non null, the database cannot create a column without specifying a default value (if some rows already exist in the table) So I need to instruct hibernate to issue the following DDL statement: alter table my_ table ADD COLUMN new_ Column varchar (3) default 'def', but it must be independent of the database used

Solution

I don't think you need any documentation. Java documentation is self - explanatory

@Entity
@Table(name = "my_entity")
public class SomeEntity extends BaseEntity {

public static final class MyValueGenerator implements
        ValueGenerator<String> {
    @Override
    public String generateValue(Session session,Object owner) {
        return "This is my default name";
    }
}

@Basic
@Column(name = "name",insertable = true,updatable = true,nullable = false,length = 255)
// This will add a DDL default
@ColumnDefault("'This is my default name'")
// This will add a runtime default.
@GeneratorType(type = MyValueGenerator.class)
private String name;

// getters and setters

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>