Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ------------------------------------------------------ */
- @Entity
- @Table(name="HELLO")
- @NamedQuery(name="Hello.findAll", query="SELECT c FROM Hello c")
- public class Hello implements Serializable {
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(unique=true, nullable=false)
- private int id;
- @ManyToOne
- @JoinColumn(name="world_id", nullable=false)
- private World world;
- @Lob
- private String description;
- public Hello() {
- }
- // getters and setters...
- }
- /* ------------------------------------------------------ */
- @Entity
- @Table(name = "WORLD")
- @NamedQuery(name = "World.findAll", query = "SELECT c FROM World c")
- public class World implements Serializable {
- private static final long serialVersionUID = 1L;
- @Id
- @Column(unique = true, nullable = false)
- private int id;
- @Lob
- private String description;
- @OneToMany(mappedBy = "World")
- private List<Hello> hello;
- public World() {
- }
- // getters and setters...
- }
- /* ------------------------------------------------------ */
- @Repository
- public class DaoHello {
- @Autowired
- private SessionFactory sessionFactory;
- public Hello findById(int id) {
- return (Hello) this.sessionFactory.getCurrentSession().
- get(Hello.class, id);
- }
- public Hello findByIdMy(int id) {
- return (Hello) this.sessionFactory.getCurrentSession()
- .createQuery("SELECT n FROM Hello n JOIN FETCH n.world WHERE n.id = :id").setInteger("id", id)
- .uniqueResult();
- }
- }
- /* ------------------------------------------------------ */
- @Controller
- @RequestMapping("/hello")
- public class RestController {
- @Autowired
- private Hellomanager manager;
- @RequestMapping(value="/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
- public @ResponseBody Hello get1(@PathVariable("id") int id) {
- return manager.findById(id);
- }
- @RequestMapping(value="/abc/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
- public @ResponseBody Hello get2(@PathVariable("id") int id) {
- return manager.findByIdMy(id);
- }
- }
- /* ------------------------------------------------------ */
Advertisement
Add Comment
Please, Sign In to add comment