如何在实体类entity添加表中没有对应的字段

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Mwangsheng/article/details/83996086

@Transient表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性. 
如果一个属性并非数据库表的字段映射,务必将其标示为@Transient,否则,ORM框架默认其注解为@Basic

比如现在有实体类Message,但是我想在使用它时想要多用一个字段来接收未读消息的数量 unReadCount,此时只要在字段前加上@Transient注解就可以了。记住要引入  import javax.persistence.Transient;


@Entity
@Table(name = "message")
@DynamicUpdate
@DynamicInsert
@Data
public class Message extends BaseEntity {

	/*接收人id*/
	private Long receiveCustomerId;
	/*发送人id*/
	private Long sendCustomerId;
	/*标题*/
	private String title;
	/*内容*/
	private String content;
	/*通知信息*/
	private Integer msgType;
	/*0未读 1已读*/
	private Integer status;
        @Transient
	private Long unReadCount;

}

加上注解后,重启服务时:2018-11-12 17:46:53.130  INFO 972 --- [  restartedMain] o.h.t.h.SchemaUpdate                     : HHH000228: Running hbm2ddl schema update 就不会改变表结构了。

2018-11-12 17:46:52.151  INFO 972 --- [  restartedMain] o.h.Version                              : HHH000412: Hibernate Core {5.0.12.Final}
2018-11-12 17:46:52.152  INFO 972 --- [  restartedMain] o.h.c.Environment                        : HHH000206: hibernate.properties not found
2018-11-12 17:46:52.153  INFO 972 --- [  restartedMain] o.h.c.Environment                        : HHH000021: Bytecode provider name : javassist
2018-11-12 17:46:52.200  INFO 972 --- [  restartedMain] o.h.a.c.Version                          : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-11-12 17:46:52.346  INFO 972 --- [  restartedMain] o.h.d.Dialect                            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-11-12 17:46:53.130  INFO 972 --- [  restartedMain] o.h.t.h.SchemaUpdate                     : HHH000228: Running hbm2ddl schema update

猜你喜欢

转载自blog.csdn.net/Mwangsheng/article/details/83996086