问题
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 */;
回答1:
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"
来源:https://stackoverflow.com/questions/19211725/jpa-creating-a-trimmed-down-entity-version-of-an-existing-entity