tuxmartin

Spring @ManyToOne

Oct 11th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. /* ------------------------------------------------------ */
  2. @Entity
  3. @Table(name="HELLO")
  4. @NamedQuery(name="Hello.findAll", query="SELECT c FROM Hello c")
  5. public class Hello implements Serializable {
  6.     private static final long serialVersionUID = 1L;
  7.  
  8.     @Id
  9.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  10.     @Column(unique=true, nullable=false)
  11.     private int id;
  12.  
  13.     @ManyToOne
  14.     @JoinColumn(name="world_id", nullable=false)
  15.     private World world;
  16.  
  17.     @Lob
  18.     private String description;
  19.  
  20.     public Hello() {
  21.     }
  22.     // getters and setters...
  23.  
  24. }
  25. /* ------------------------------------------------------ */
  26. @Entity
  27. @Table(name = "WORLD")
  28. @NamedQuery(name = "World.findAll", query = "SELECT c FROM World c")
  29. public class World implements Serializable {
  30.     private static final long serialVersionUID = 1L;
  31.  
  32.     @Id
  33.     @Column(unique = true, nullable = false)
  34.     private int id;
  35.  
  36.     @Lob
  37.     private String description;
  38.  
  39.     @OneToMany(mappedBy = "World")
  40.     private List<Hello> hello;
  41.  
  42.     public World() {
  43.     }
  44.     // getters and setters...
  45. }
  46. /* ------------------------------------------------------ */
  47. @Repository
  48. public class DaoHello {    
  49.     @Autowired
  50.     private SessionFactory sessionFactory;
  51.  
  52.     public Hello findById(int id) {
  53.         return (Hello) this.sessionFactory.getCurrentSession().
  54.                 get(Hello.class, id);
  55.     }
  56.  
  57.     public Hello findByIdMy(int id) {
  58.         return (Hello) this.sessionFactory.getCurrentSession()
  59.                 .createQuery("SELECT n FROM Hello n JOIN FETCH n.world WHERE n.id = :id").setInteger("id", id)
  60.                 .uniqueResult();
  61.     }
  62. }
  63. /* ------------------------------------------------------ */
  64. @Controller
  65. @RequestMapping("/hello")
  66. public class RestController {
  67.  
  68.     @Autowired
  69.     private Hellomanager manager;
  70.  
  71.     @RequestMapping(value="/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
  72.     public @ResponseBody Hello get1(@PathVariable("id") int id) {
  73.         return manager.findById(id);
  74.     }
  75.  
  76.     @RequestMapping(value="/abc/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
  77.     public @ResponseBody Hello get2(@PathVariable("id") int id) {
  78.         return manager.findByIdMy(id);
  79.     }
  80. }
  81. /* ------------------------------------------------------ */
Advertisement
Add Comment
Please, Sign In to add comment