title: JPA creating a trimmed down entity version of an existing Entity
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>The problem is that I have another object "simplecost" that has the following structure</p>
  
  <pre><code>public class SimpleCost {
   private Long id;
   private Long userId;
   private Long cost;
  }
  </code></pre>
  
  <p>A collection of above objects is utilized by the frontend to generate a view like this</p>
  
  <pre><code>userId  cost
  1        5.5
  2        7.5
  3        10.00
  </code></pre>
  
  <p>But what the view is supposed to be is </p>
  
  <pre><code>Name                cost
  John Doe            5.5
  Jane Doe            7.5
  Rusty Shackleford   10.00
  </code></pre>
  
  <p>So instead of sending just the userId with the SimpleCost object, I also need to send the fname and lname. </p>
  
  <p>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.</p>
  
  <p>I added "UserBare" as a new member of SimpleCost object</p>
  
  <p>New proposed structure of SimpleCost</p>
  
  <pre><code>public class SimpleCost {
   private Long id;
   private Long userId;
   private Long cost;
   private UserBare user;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Here is my database structure</p>
  
  <p>User</p>
  
  <pre><code>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 */;
  </code></pre>
  
  <p>simplecost</p>
  
  <pre><code>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 */;
  </code></pre>