dimanche 25 février 2018

How to define one to many relation in java spring.?

I am trying to create an api to save locations for a user. I defined User entity and a Location entity. Defined it as a one to many relation to user entity and location entity.

But when I try to create a location for a user, I am getting this response :

url : http://localhost:8080/Locations/create

Request :

    {
"latitude" : 15645.00,
"longitude" : 154645.00,
"location" : "miraroad",
"user": {
    "id" : 6
}
}

Response :

    {
    "id": null,
    "location": "miraroad",
    "latitude": 15645,
    "user": null,
    "longitude": 154645
}

User id is not getting mapped to location created.

Code :

User :

    @Entity // This tells Hibernate to make a table out of this class
public class User {


      public User() {}

      public User(String email, String name) {
            this.email = email;
            this.name = name;
          }

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)

    @Column(name = "user_id")
    private Integer id;

    @Column(name = "name")
    private String name;

    private String email;

    private String number;


    @OneToMany(mappedBy="user")
    private List <Location> locations;

    public List<Location> getLocations() {
        return locations;
    }

    public void setLocations(List<Location> locations) {
        this.locations = locations;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}

Location :

   @Entity
public class Location {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)

    @Column(name ="loc_id")
    private Integer id;


    private String location;
    private double latitude;

    public Location() {}

    @ManyToOne(fetch = FetchType.LAZY)
    private User user;


       @Override
    public String toString() {
        return "Location [id=" + id + ", location=" + location + ", latitude=" + latitude + ", longitude=" + longitude
                + "]";
    }
    public double getLatitude() {
        return latitude;
    }
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }
    public double getLongitude() {
        return longitude;
    }
    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }
    private double longitude;
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public char[] getId() {
        // TODO Auto-generated method stub
        return null;
    }
}

LocationController :

@Controller    // This means that this class is a Controller
@RequestMapping(path="/Locations") // This means URL's start with /demo (after Application path)

public class LocationController {

        @Autowired // This means to get the bean called userRepository
                   // Which is auto-generated by Spring, we will use it to handle the data
        private LocationRepository locationRepository;
        private UserRepository userRepository;

         @RequestMapping("/create")
         @ResponseBody
         public Location create(@RequestBody Location location) {
           String locId = "";
           Location newLocation = new Location();
           try {
               User user = userRepository(location.getUser()); //Get the parent Object

               newLocation = new Location(); //Create a new Many object
               newLocation.setLatitude(location.getLatitude());
               newLocation.setLongitude(location.getLongitude());
               newLocation.setLocation(location.getLocation());
               newLocation.setUser(user);

               locationRepository.save(newLocation);
               locId = String.valueOf(newLocation.getId());

           }
           catch (Exception ex) {
            // return "Error creating the user: " + ex.toString();
               return newLocation;
           }
           return locationRepository.save(newLocation);
         }

        private User userRepository(User user) {
            // TODO Auto-generated method stub
            return null;
        }

        @GetMapping(path="/all")
        public @ResponseBody Iterable<Location> getAllLocations() {
            // This returns a JSON or XML with the users
            return locationRepository.findAll();
        }

}

What is hoing wrong here? I am new to web development and java. Can anyone help me out please?

Thank you..




Aucun commentaire:

Enregistrer un commentaire