将一个值映射到JPA中的多个列 [英] Map one value to multiple columns in JPA

查看:154
本文介绍了将一个值映射到JPA中的多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从用户那里得到一个输入值,并将其存储到数据库中. 我使用JPA写入数据库. 如果需要将输入值存储到数据库的2列中,我必须使用什么注释?

I get an input value from the user and store it into the DB. I use JPA to write into DB. If I need to store the input value into 2 columns in the DB, what is the annotation I must use?

下面的代码.

我需要将用户输入-(存储在Bean字段UserOrderDetails.noOfItemsSelected中)存储到数据库表NoOfItemsSelected和NoOfItemsDispatched中的2列中. 我应该使用什么注释?

I need to store user input - (stored in Bean field UserOrderDetails.noOfItemsSelected) into 2 columns in the DB table NoOfItemsSelected and NoOfItemsDispatched. What annotation should I use?

问题在于,我不想在实体Bean中为NoOfItemsDispatched添加字段.

The catch is that I do not want to add a field for NoOfItemsDispatched in the Entity Bean.

数据库表:

create table UserOrderDetails(
TransactionId varchar(255),
CreationDateTime datetime2(),
NoOfItemsSelected int,
NoOfItemsDispatched int
)
GO

我的实体Bean:

@Entity
@Table(name="UserOrderDetails")
public class UserOrderDetails {

    @Id
    private String transactionId;   
    private Timestamp CreationDateTime; 
    private int noOfItemsSelected;
    //Getters and Setters
}

供参考的控制器类:

class UserOrderCreationController {
@RequestMapping(value = "/createUserOrder", method = RequestMethod.POST)
    public ResponseEntity<?> createUserOrder(@RequestBody UserOrderDetails userOrderDetails , @RequestHeader HttpHeaders headers) throws Exception{

    //Some business logic to handle the user Order

    dbrepository.save(UserOrderDetails);

    return new ResponseEntity<UserOrderDetails>(userOrderDetails, HttpStatus.CREATED);
    }
}

谢谢

推荐答案

据我所知,如果您具有相同的字段名称和列名称,则只需在DAO图层实体类的字段中添加以下注释.这些注释是可选的.

As far as i know you just need to add annotations as below on the fields in the DAO layer entity class.if you have the same field names and column names these annotations are optional.

由于问题将一个值声明给多列,因此在调用DAO.save之前,您应该在两个字段中都填充该值.

As the question states one value to multiple columns,before the the DAO.save is called you should be populating the value in both the fields.

@Entity
@Table(name="UserOrderDetails")
public class UserOrderDetails {

   //Need to have identity generator of your wish

    @Id
    @Column(name = "TRANSACTION_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String transactionId;   
    private Timestamp CreationDateTime; 

   **@Column(name = "NO_OF_ITEMS_SELECTED")**
    private int noOfItemsSelected; 

   **@Column(name = "NO_OF_ITEMS_DISP")** 
    private int noOfItemsDispatched;
    //Getters and Setters

}

或者我们可以使用Hibernate的@Formula注释,将来自用户请求的一个字段填充到相应的字段中,对于另一列,您可以使用@Formula保留相同的值.公式

Alternatively we can use Hibernate's @Formula annotation.Populate the one field which is coming from user request to the corresponding field and For the other column for which you can keep the same value by using @Formula.Look for some examples of @Formula

 @Formula("noOfItemsSelected")
 private float noOfItemsDispatched;

这篇关于将一个值映射到JPA中的多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆