JPA创建现有实体的下调实体版本 [英] JPA creating a trimmed down entity version of an existing Entity

查看:154
本文介绍了JPA创建现有实体的下调实体版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户角度,并通过REST春/ MVC一个REST后端。该数据库是MySQL和我使用JPA与Hibernate的ORM。

我的一些前端的意见要求我去正常化在Java方面的一些领域的数据。
我有一个典型的用户域的对象与ID,FNAME,LNAME等。该用户对象指向数据库中的用户表。没有什么不寻常。

问题是,我有另一个对象simplecost具有以下结构

 公共类SimpleCost {
 私人长期身份证;
 私人长期用户id;
 私人长期成本;
}

以上对象的集合,由前端用来产生这样的看法

  userId的成本
1 5.5
2 7.5
3 10.00

但认为应该是为

 名称成本
李四5.5
李四7.5
生锈的沙克尔福10.00

因此​​,而不是与SimpleCost对象仅发送了用户标识,我也需要发送的FNAME LNAME和。

要解决这个问题,我想(惨遭失败)以下办法。
我创建了一个名为UserBare一个新的域对象。这是用户的下调版本。我的意思是说,UserBare只有一个FNAME LNAME和

我补充说:UserBare作为SimpleCost对象的一个​​新成员

提出了新的SimpleCost结构

 公共类SimpleCost {
 私人长期身份证;
 私人长期用户id;
 私人长期成本;
 私人UserBare用户;
}

但我无法填充使用JPA的用户对象。我想加盟一对一其中没有工作。我也尝试添加字符串FNAME,LNAME字符串代替领域向SimpleCost再@SecondaryTable注解与@Column(表=用户,名字=FNAME)填充这些值。这并没有正常工作。

下面是我的数据库结构

用户

  CREATE TABLE`user`(
`id` BIGINT(20)NOT NULL AUTO_INCREMENT,
`fName` VARCHAR(100)DEFAULT NULL,
`lName` VARCHAR(100)DEFAULT NULL,
`mName` VARCHAR(100)DEFAULT NULL,
`title` VARCHAR(100)DEFAULT NULL,
`email` VARCHAR(100)DEFAULT NULL,
 PRIMARY KEY(`id`)
 UNIQUE KEY`id_UNIQUE`(`id`)
 )ENGINE = InnoDB的AUTO_INCREMENT = 9默认字符集= latin1的;
 / * 40101等价:character_set_connection = @saved_cs_client * /;

simplecost

  CREATE TABLE`simplecost`(
`id` BIGINT(20)NOT NULL AUTO_INCREMENT,
`userId` BIGINT(20)DEFAULT NULL,
`liableCost`十进制(10,2)DEFAULT NULL,
`isActive` TINYINT(1)DEFAULT NULL,
PRIMARY KEY(`id`)
UNIQUE KEY`id_UNIQUE`(`id`)
KEY`fk_simplecost_user1_idx`(`userId`)
约束`fk_simplecost_user`外键(`userId`)参考`user`(`id`)ON DELETE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB的AUTO_INCREMENT = 13默认字符集= latin1的;
/ * 40101等价:character_set_connection = @saved_cs_client * /;


解决方案

如果您希望加入到工作,你必须要做到这一点:

 公共类SimpleCost {
 私人长期身份证;
 私人用户的用户;
 私人长期成本;
}公共类用户{
    私人长期身份证;
    私人字符串FNAME;
    私人字符串LNAME;
    priavte字符串MNAME;
    priavate字符串称号;
    私人字符串电子邮件;
}

另外,如果我是你,我会列身份证重命名为用户idsimpleCostId

I have a Angular Client and a REST backend through Spring REST/MVC. The database is MySQL and I'm using JPA with Hibernate for ORM.

Some of my front end views require me to de-normalize some domain-data on the Java side. I have a typical User Domain object with id,fname,lname etc.. This User object points to 'user' table in the database. Nothing unusual.

The problem is that I have another object "simplecost" that has the following structure

public class SimpleCost {
 private Long id;
 private Long userId;
 private Long cost;
}

A collection of above objects is utilized by the frontend to generate a view like this

userId  cost
1        5.5
2        7.5
3        10.00

But what the view is supposed to be is

Name                cost
John Doe            5.5
Jane Doe            7.5
Rusty Shackleford   10.00

So instead of sending just the userId with the SimpleCost object, I also need to send the fname and lname.

To solve this problem I tried (failing miserably) the following approach. I created a new domain object called "UserBare". This is a trimmed down version of the "User". What I mean by this is that "UserBare" only has an fname and lname.

I added "UserBare" as a new member of SimpleCost object

New proposed structure of SimpleCost

public class SimpleCost {
 private Long id;
 private Long userId;
 private Long cost;
 private UserBare user;
}

But I'm unable to populate the user object using JPA. I tried OnetoOne join which didn't work. I also tried adding String fname, String lname fields to SimpleCost instead and then a @SecondaryTable annotation to populate those values with @Column(table="user", name="fName"). This didn't work as well.

Here is my database structure

User

CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`fName` varchar(100) DEFAULT NULL,
`lName` varchar(100) DEFAULT NULL,
`mName` varchar(100) DEFAULT NULL,
`title` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `id_UNIQUE` (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
 /*!40101 SET character_set_client = @saved_cs_client */;

simplecost

CREATE TABLE `simplecost` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`userId` bigint(20) DEFAULT NULL,
`liableCost` decimal(10,2) DEFAULT NULL,
`isActive` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `fk_simplecost_user1_idx` (`userId`),
CONSTRAINT `fk_simplecost_user` FOREIGN KEY (`userId`) REFERENCES `user`      (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

解决方案

if you want the join to work you have to do this:

public class SimpleCost {
 private Long id;
 private User user;
 private Long cost;
}

public class User{
    private Long id;
    private String fName;
    private String lName;
    priavte String mName;
    priavate String title;
    private String email;
}

also if i were you i would rename the columns "id" to "userId" and "simpleCostId"

这篇关于JPA创建现有实体的下调实体版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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