@Column写在属性和写在get方法上面的区别

问题背景:我在数据库里加了一个叫 ab_name_6 的字段,但是在调用接口的时候,控制台一直报错,报错内容如下:

即在相应的数据库表里找不到ab_name6字段。我觉得很奇怪。请教了大佬,然后查了一些别人的文章,现在做一下总结。

1、首先,我的字段名虽然起的符合一般规范,但应该是不符合JPA的命名规范的,不然也不会在映射时将“abName6”映射成“ab_name6”。

2、当时,我的@Id字段时写在id的get方法上,而我的@Column(name = "ab_name_6")写在了abName6上,即属性上,如下图:

请教过大佬以后,才知道:虽然@Column可以写在属性上,也可以写在get方法上,但是它必须和@Id在同样的位置才会起效。即@Id在字段属性上,@Column也必须写在字段属性上,写在get方法上是不生效的。

有人可能会问,为什么我有的写在字段属性上,有的写在get方法上也不会报错,都会生效呢?

这大概是因为你的字段命名都是标准的驼峰式,所以jpa会按照既定的规则对你标准驼峰式的字段名进行拆解,虽然和@Id不在同样位置的@Column没有生效,但是因为你的字段名是标准驼峰式,那么JPA拆解后就能顺利找到你在数据库的字段了。但是我这个abName6的名字估计是因为不符合JPA要求的命名规则,而且@Column和@Id不在同一位置上,所以导致@Column没有生效,然后JPA按照它自己的一套规则对我的abName6进行转换,转成了“ab_name6”,自然就在数据库找不到对应的字段了。

这里有一个点就是,如果你的命名是标准的符合JPA命名规范的驼峰式,那么你不写@Column也可以。我想@Column最大的作用就是,当你的实体类里某个属性的名字和数据库对应的字段名不一致时,可以用来告诉JPA这个属性在数据库表中的真实名字吧。例如: ,当你在实体类里叫use的属性,对应的是数据库表中的enable字段时,生效的@Column会告诉JPA这个use属性在数据库中的真实名字。

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
先给出完整代码,再一步步解释。 ```java import java.lang.annotation.*; import java.util.Scanner; public class PersonInput implements PersonAction { private static Scanner input = new Scanner(System.in); @Override public Person process(Person person) { Class cls = person.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { boolean isAccess = field.isAccessible(); if (!isAccess) { field.setAccessible(true); } Label label = field.getAnnotation(Label.class); Column column = field.getAnnotation(Column.class); if (label != null) { System.out.print(label.value() + ": "); } if (column != null) { String inputStr = input.nextLine(); if (!column.nullable() && (inputStr == null || inputStr.length() == 0)) { System.out.println(column.label() + "不能为空"); inputStr = input.nextLine(); } if (column.maxLength() > 0 && inputStr.length() > column.maxLength()) { System.out.println(column.label() + "长度不能超过" + column.maxLength() + "个字符"); inputStr = input.nextLine(); } if (column.minLength() > 0 && inputStr.length() < column.minLength()) { System.out.println(column.label() + "长度不能少于" + column.minLength() + "个字符"); inputStr = input.nextLine(); } if (column.maxValue() > Long.MIN_VALUE) { long value = Long.parseLong(inputStr); if (value > column.maxValue()) { System.out.println(column.label() + "不能大于" + column.maxValue()); inputStr = input.nextLine(); } } if (column.minValue() < Long.MAX_VALUE) { long value = Long.parseLong(inputStr); if (value < column.minValue()) { System.out.println(column.label() + "不能小于" + column.minValue()); inputStr = input.nextLine(); } } try { if (field.getType() == String.class) { field.set(person, inputStr); } else if (field.getType() == Integer.class) { int value = Integer.parseInt(inputStr); field.set(person, value); } else if (field.getType() == Long.class) { long value = Long.parseLong(inputStr); field.set(person, value); } else if (field.getType() == Boolean.class) { boolean value = Boolean.parseBoolean(inputStr); field.set(person, value); } } catch (Exception e) { e.printStackTrace(); } } if (!isAccess) { field.setAccessible(false); } } return person; } } import java.lang.annotation.*; public class PersonDisplay implements PersonAction { @Override public Person process(Person person) { Class cls = person.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { boolean isAccess = field.isAccessible(); if (!isAccess) { field.setAccessible(true); } Label label = field.getAnnotation(Label.class); if (label != null) { try { System.out.println(label.value() + ": " + field.get(person)); } catch (Exception e) { e.printStackTrace(); } } if (!isAccess) { field.setAccessible(false); } } return person; } } @Target({ElementType.TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @interface Label { String value(); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface Column { String label(); boolean nullable() default true; int maxLength() default 0; int minLength() default 0; long maxValue() default Long.MIN_VALUE; long minValue() default Long.MAX_VALUE; } interface PersonAction { Person process(Person person); } class Person { @Label("姓名") @Column(label = "姓名", nullable = false, maxLength = 32) private String name; @Label("性别") @Column(label = "性别") private String sex; @Label("年龄") @Column(label = "年龄", minValue = 0, maxValue = 200) private Integer age; @Label("身份证号") @Column(label = "身份证号", maxLength = 18) private String idNo; @Label("是否已婚") @Column(label = "是否已婚") private Boolean isMarried; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getIdNo() { return idNo; } public void setIdNo(String idNo) { this.idNo = idNo; } public Boolean getMarried() { return isMarried; } public void setMarried(Boolean married) { isMarried = married; } } public class Main { private static final String CREATE_TABLE_SQL = "CREATE TABLE `person` (\n" + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + " `name` varchar(32) NOT NULL,\n" + " `sex` varchar(8) DEFAULT NULL,\n" + " `age` int(11) DEFAULT NULL,\n" + " `id_no` varchar(18) DEFAULT NULL,\n" + " `married` tinyint(1) DEFAULT NULL,\n" + " PRIMARY KEY (`id`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"; private static final String DROP_TABLE_SQL = "DROP TABLE IF EXISTS `person`;"; private static final String INSERT_SQL = "INSERT INTO `person` (`name`, `sex`, `age`, `id_no`, `married`) VALUES (?, ?, ?, ?, ?);"; private static final String UPDATE_SQL = "UPDATE `person` SET `name`=?,`sex`=?,`age`=?,`id_no`=?,`married`=? WHERE `id`=?;"; private static final String DELETE_SQL = "DELETE FROM `person` WHERE `id`=?;"; public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "123456"); Statement stmt = conn.createStatement(); stmt.executeUpdate(DROP_TABLE_SQL); stmt.executeUpdate(CREATE_TABLE_SQL); PersonInput input = new PersonInput(); PersonDisplay display = new PersonDisplay(); Person person = new Person(); input.process(person); display.process(person); ps = conn.prepareStatement(INSERT_SQL); ps.setString(1, person.getName()); ps.setString(2, person.getSex()); ps.setInt(3, person.getAge()); ps.setString(4, person.getIdNo()); ps.setBoolean(5, person.getMarried()); ps.executeUpdate(); ps = conn.prepareStatement(UPDATE_SQL); ps.setString(1, person.getName()); ps.setString(2, person.getSex()); ps.setInt(3, person.getAge()); ps.setString(4, person.getIdNo()); ps.setBoolean(5, person.getMarried()); ps.setInt(6, 1); ps.executeUpdate(); ps = conn.prepareStatement(DELETE_SQL); ps.setInt(1, 1); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { e.printStackTrace(); } } } } ``` 首先,我们定义了两个注解,@Label和@Column。其中,@Label是用来给类和属性添加中文名称的,@Column是用来给属性添加数据库字段的一些配置信息的。 ```java @Target({ElementType.TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @interface Label { String value(); } @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface Column { String label(); boolean nullable() default true; int maxLength() default 0; int minLength() default 0; long maxValue() default Long.MIN_VALUE; long minValue() default Long.MAX_VALUE; } ``` 然后,我们定义了一个Person类,它有姓名、性别、年龄、身份证号、是否已婚等属性,每个属性都添加了@Label和@Column注解。我们使用注解处理器,可以根据@Column注解的配置信息来进行输入校验,这样就可以避免输入错误的数据。同时,我们还定义了PersonInput和PersonDisplay两个类,它们都实现了PersonAction接口,分别用于输入和输出一个Person对象。 ```java class Person { @Label("姓名") @Column(label = "姓名", nullable = false, maxLength = 32) private String name; @Label("性别") @Column(label = "性别") private String sex; @Label("年龄") @Column(label = "年龄", minValue = 0, maxValue = 200) private Integer age; @Label("身份证号") @Column(label = "身份证号", maxLength = 18) private String idNo; @Label("是否已婚") @Column(label = "是否已婚") private Boolean isMarried; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getIdNo() { return idNo; } public void setIdNo(String idNo) { this.idNo = idNo; } public Boolean getMarried() { return isMarried; } public void setMarried(Boolean married) { isMarried = married; } } interface PersonAction { Person process(Person person); } public class PersonInput implements PersonAction { @Override public Person process(Person person) { Class cls = person.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { boolean isAccess = field.isAccessible(); if (!isAccess) { field.setAccessible(true); } Label label = field.getAnnotation(Label.class); Column column = field.getAnnotation(Column.class); if (label != null) { System.out.print(label.value() + ": "); } if (column != null) { String inputStr = input.nextLine(); if (!column.nullable() && (inputStr == null || inputStr.length() == 0)) { System.out.println(column.label() + "不能为空"); inputStr = input.nextLine(); } if (column.maxLength() > 0 && inputStr.length() > column.maxLength()) { System.out.println(column.label() + "长度不能超过" + column.maxLength() + "个字符"); inputStr = input.nextLine(); } if (column.minLength() > 0 && inputStr.length() < column.minLength()) { System.out.println(column.label() + "长度不能少于" + column.minLength() + "个字符"); inputStr = input.nextLine(); } if (column.maxValue() > Long.MIN_VALUE) { long value = Long.parseLong(inputStr); if (value > column.maxValue()) { System.out.println(column.label() + "不能大于" + column.maxValue()); inputStr = input.nextLine(); } } if (column.minValue() < Long.MAX_VALUE) { long value = Long.parseLong(inputStr); if (value < column.minValue()) { System.out.println(column.label() + "不能小于" + column.minValue()); inputStr = input.nextLine(); } } try { if (field.getType() == String.class) { field.set(person, inputStr); } else if (field.getType() == Integer.class) { int value = Integer.parseInt(inputStr); field.set(person, value); } else if (field.getType() == Long.class) { long value = Long.parseLong(inputStr); field.set(person, value); } else if (field.getType() == Boolean.class) { boolean value = Boolean.parseBoolean(inputStr); field.set(person, value); } } catch (Exception e) { e.printStackTrace(); } } if (!isAccess) { field.setAccessible(false); } } return person; } } public class PersonDisplay implements PersonAction { @Override public Person process(Person person) { Class cls = person.getClass(); Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { boolean isAccess = field.isAccessible(); if (!isAccess) { field.setAccessible(true); } Label label = field.getAnnotation(Label.class); if (label != null) { try { System.out.println(label.value() + ": " + field.get(person)); } catch (Exception e) { e.printStackTrace(); } } if (!isAccess) { field.setAccessible(false); } } return person; } } ``` 最后,我们在Main类中使用JDBC,将Person对象保存到数据库中。在保存之前,先执行了一次删除表和创建表的操作。 ```java public class Main { private static final String CREATE_TABLE_SQL = "CREATE TABLE `person` (\n" + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + " `name` varchar(32) NOT NULL,\n" + " `sex` varchar(8) DEFAULT NULL,\n" + " `age` int(11) DEFAULT NULL,\n" + " `id_no` varchar(18) DEFAULT NULL,\n" + " `married` tinyint(1) DEFAULT NULL,\n" + " PRIMARY KEY (`id`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"; private static final String DROP_TABLE_SQL = "DROP TABLE IF EXISTS `person`;"; private static final String INSERT_SQL = "INSERT INTO `person` (`name`, `sex`, `age`, `id_no`, `married`) VALUES (?, ?, ?, ?, ?);"; private static final String UPDATE_SQL = "UPDATE `person` SET `name`=?,`sex`=?,`age`=?,`id_no`=?,`married`=? WHERE `id`=?;"; private static final String DELETE_SQL = "DELETE FROM `person` WHERE `id`=?;"; public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "123456"); Statement stmt = conn.createStatement(); stmt.executeUpdate(DROP_TABLE_SQL); stmt.executeUpdate(CREATE_TABLE_SQL); PersonInput input = new PersonInput(); PersonDisplay display = new PersonDisplay(); Person person = new Person(); input.process(person); display.process(person); ps = conn.prepareStatement(INSERT_SQL); ps.setString(1, person.getName()); ps.setString(2, person.getSex()); ps.setInt(3, person.getAge()); ps.setString(4, person.getIdNo()); ps.setBoolean(5, person.getMarried()); ps.executeUpdate(); ps = conn.prepareStatement(UPDATE_SQL); ps.setString(1, person.getName()); ps.setString(2, person.getSex()); ps.setInt(3, person.getAge()); ps.setString(4, person.getIdNo()); ps.setBoolean(5, person.getMarried()); ps.setInt(6, 1); ps.executeUpdate(); ps = conn.prepareStatement(DELETE_SQL); ps.setInt(1, 1);

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值