使用Sping Boot 数据生成自定义生成器值JPA postgresql数据库

91zkwejq  于 5个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(54)

我正在使用JAVA,Sping Boot ,Spring data JPA和PostgreSQL数据库。试图为empid列生成自定义String值。如emp1,emp2,emp3等
但无法创建值。面临的问题:org.postgresql.util.PSQLException:ERROR:column employee_seq.next_val does not exist位置:9
第一个月
` public class MyClass实现MyClass {

@Override
public Object generate(SharedSessionContractImplementor sharedSessionContractImplementor, Object o) {
    String  prefix ="EMP";
    String suffix="";
    String finalVal = null;
    try {

        Connection connection= sharedSessionContractImplementor.getJdbcConnectionAccess().obtainConnection();
        Statement statement=connection.createStatement();
        String sql = " select employee_seq.next_val from  employee_seq";
        ResultSet rs= statement.executeQuery(sql);
        if (rs.next()){
            Long value=rs.getLong(1);
            suffix=value.toString();
        }

        finalVal=prefix+suffix;

        System.out.println("Final value is "+ finalVal);
    }catch (Exception e){
        e.printStackTrace();
    }

    return finalVal ;
}

字符串
}'的一个例子

0pizxfdo

0pizxfdo1#

您的SQL查询在PostgreSQL中是错误的。像这样使用nextval函数:

String sql = "select nextval('employee_seq')";

字符串

相关问题