2017-03-17 9 views
1
PlayerService 

これは私のブートアプリケーションのsrviceクラスです。データベースが上書きされています。 jsonの最後のエントリはデータベースに書き込まれ、他のすべてのエントリは上書きされます。 Jsonファイルの最後のエントリを表示

パッケージio.anuj.springbootquickstart.topic;

import java.util.ArrayList; 
import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

@Service 
public class PlayerService { 

    @Autowired 
    private PlayerRespository playerRespository; 
    // private List<Topic> topics = new ArrayList<> (Arrays.asList(
    //   new Topic("Spring","Spring Framework","SpringFramework Description"), 
    //   new Topic("Core","Core Framework","CoreFramework Description"), 
    //   new Topic("JavaScript","JavaScript Framework","JavaScript Description"))); 
    public List<Player> getAllPlayer(){ 
     List<Player> player = new ArrayList(); 
     playerRespository.findAll() 
     .forEach(player::add); //lambda expressions 
     return player; 
    } 
    public Player getPlayer(Long id){ 
     //return topics.stream().filter(t -> t.getId().equals(id).findFirst().get()); 
     return playerRespository.findOne(id); 
    } 
    public void addPlayer(Player player) { 

     playerRespository.save(player); 

    } 
    public void updatePlayer(Long id, Player player) { 
     playerRespository.save(player); 

    } 
    public void deletePlayer(Long id) { 
     //topics.removeIf(t -> t.getId().equals(id)); 
     playerRespository.delete(id); 
    } 

} 



PLayerRepository 

これは私のアプリのプレイヤーリポジトリです。 パッケージio.anuj.springbootquickstart.topic;

import org.springframework.data.repository.CrudRepository; 

public interface PlayerRespository extends CrudRepository <Player, Long>{ 




    //crud repository-logic of any entity class 
    //getallTopic() 
    //gettopic(string id) 
    //update topic(topic t) 
    //deletetopic(string id) 


} 



PLayerController 

これは、プレイヤーのコントローラ

package io.anuj.springbootquickstart.topic; 


import java.io.FileReader; 
import java.util.List; 

import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 


@RestController 
//wherever rest controller is written it will give json as output send back as a HTTP response 
public class PlayerController { 

    @Autowired 
    private PlayerService playerService; 
    //get request by default 
     @RequestMapping("/player") 
     public List<Player> getallPlayer(){ 
      return playerService.getAllPlayer(); 
      } 

    //get request 
    @RequestMapping("/player/{id}") 
    public Player getPlayer(@PathVariable Long id){ 
     return playerService.getPlayer(id); 
    } 
    // 
    @RequestMapping(method=RequestMethod.GET, value= "/getplayer") 
    public void addAllPlayer(){ 

     Player player = new Player(); 
     JSONParser parser = new JSONParser(); 

     try { 
      Object ob = parser.parse(new FileReader("/home/bridgeit/Desktop/P.D-anuj/Json/newPlayerInfo.json")); 
      JSONObject object = (JSONObject) ob; 

      JSONArray data = (JSONArray) object.get("Playersinfo"); 

      for (int i = 0; i < data.size(); i++) 
      { 
       JSONObject itemObj = (JSONObject) data.get(i); 


       Object nameObj = itemObj.get("player_name"); 
       String playerName = (String) nameObj; 
       player.setPlayer_name(playerName); 

       Object imgObject = itemObj.get("player_img_url"); 
       String playerPic = (String) imgObject; 
       player.setPlayer_img_url(playerPic); 

       Object roleObj = itemObj.get("player_role"); 
       String roleName = (String) roleObj; 
       player.setPlayer_role(roleName); 

       Object battingStyleObj = itemObj.get("player_batting_style"); 
       String battingStyleName = (String) battingStyleObj; 
       player.setPlayer_batting_style(battingStyleName); 

       Object bowlingObj = itemObj.get("player_bowling_style"); 
       String bowlingName = (String) bowlingObj; 
       player.setPlayer_bowling_style(bowlingName); 

       Object nationalityObj = itemObj.get("player_nationality"); 
       String nationalityName = (String) nationalityObj; 
       player.setPlayer_nationality(nationalityName); 

       Object dobObj = itemObj.get("player_dob"); 
       String dobName = (String) dobObj; 
       player.setPlayer_dob(dobName); 

       Object teamIdObj = itemObj.get("team_id"); 
       String teamIdName = (String) teamIdObj; 
       player.setTeam_id(teamIdName); 


       playerService.addPlayer(player); 
      } 


     } catch (Exception e) { 
      System.out.println(e); 
     } 

    } 
    /*@RequestMapping(method=RequestMethod.PUT, value= "/player/{id}") 
    public void updatePlayer(@RequestBody Player player,@PathVariable String id){ 
     playerService.updatePlayer(id,player); 
    } 
    @RequestMapping(method=RequestMethod.DELETE, value= "/player/{id}") 
    public void deletePlayer(@PathVariable String id){ 
     playerService.deletePlayer(id); 
    }*/ 
} 

this is POJO class 
Player.java 
package io.anuj.springbootquickstart.topic; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import org.hibernate.annotations.GenericGenerator; 

@Entity 
/*@Table(name = "Player")*/ 
public class Player { 
    /*@Id 
    @GenericGenerator(name = "gene", strategy = "increment") 
    @GeneratedValue(generator = "gene") 
    @Column(name = "id") 
    private Long id;*/ 

@Id 
@GenericGenerator(name = "gene", strategy = "increment") 
@GeneratedValue(generator = "gene") 
    private long id; 
    private String team_id; 
    /*@Column(name = "name")*/ 
    private String player_name; 
    /*@Column(name = "display_picture")*/ 
    private String player_img_url; 
    /*@Column(name = "role")*/ 
    private String player_role; 
public Player(long id, String team_id, String player_name, String player_img_url, String player_role, 
      String player_batting_style, String player_bowling_style, String player_nationality, String player_dob) { 
     super(); 
     this.id = id; 
     this.team_id = team_id; 
     this.player_name = player_name; 
     this.player_img_url = player_img_url; 
     this.player_role = player_role; 
     this.player_batting_style = player_batting_style; 
     this.player_bowling_style = player_bowling_style; 
     this.player_nationality = player_nationality; 
     this.player_dob = player_dob; 
    } 


    /* @Column(name = "batting_style")*/ 
    private String player_batting_style; 
    /*@Column(name = "bowling_style")*/ 
    private String player_bowling_style; 
    /*@Column(name = "nationality")*/ 
    private String player_nationality; 
    /*@Column(name = "dob")*/ 
    private String player_dob; 
    /*@Column(name = "teamId")*/ 


    public Player(){ 
    } 











    public String getTeam_id() { 
     return team_id; 
    } 




    public long getId() { 
     return id; 
    } 











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











    public void setTeam_id(String team_id) { 
     this.team_id = team_id; 
    } 


    public String getPlayer_name() { 
     return player_name; 
    } 


    public void setPlayer_name(String player_name) { 
     this.player_name = player_name; 
    } 


    public String getPlayer_img_url() { 
     return player_img_url; 
    } 


    public void setPlayer_img_url(String player_img_url) { 
     this.player_img_url = player_img_url; 
    } 


    public String getPlayer_role() { 
     return player_role; 
    } 


    public void setPlayer_role(String player_role) { 
     this.player_role = player_role; 
    } 


    public String getPlayer_batting_style() { 
     return player_batting_style; 
    } 


    public void setPlayer_batting_style(String player_batting_style) { 
     this.player_batting_style = player_batting_style; 
    } 


    public String getPlayer_bowling_style() { 
     return player_bowling_style; 
    } 


    public void setPlayer_bowling_style(String player_bowling_style) { 
     this.player_bowling_style = player_bowling_style; 
    } 


    public String getPlayer_nationality() { 
     return player_nationality; 
    } 


    public void setPlayer_nationality(String player_nationality) { 
     this.player_nationality = player_nationality; 
    } 


    public String getPlayer_dob() { 
     return player_dob; 
    } 


    public void setPlayer_dob(String player_dob) { 
     this.player_dob = player_dob; 
    } 

    /*public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    }*/ 








    /*public Player(Long id, String team_id, String player_name, String player_img_url, String player_role, 
      String player_batting_style, String player_bowling_style, String player_nationality, String player_dob) { 
     super(); 
     this.id = id; 
     this.team_id = team_id; 
     this.player_name = player_name; 
     this.player_img_url = player_img_url; 
     this.player_role = player_role; 
     this.player_batting_style = player_batting_style; 
     this.player_bowling_style = player_bowling_style; 
     this.player_nationality = player_nationality; 
     this.player_dob = player_dob; 
    } 
*/ 
} 

私はJSONファイルから実行すると、それはJSONファイルの最後のエントリのみを示しています。 JSONファイルには約150人のプレーヤーが含まれていますが、databseにはプレーヤーの最後のエントリのみが表示されます。私はすべてのデータが上書きされていると思う。そのjsonファイルの最後のエントリのみがデータベースに表示されます。それが最後の選手です。 jsonの最後の1人のプレイヤーのみがデータベースに存在します。

答えて

1

コメントするのが難しいコメントのコードがたくさんあるので、少し難しいですが、コントローラーにPlayerを作成する場所が原因です。ループの

Player player = new Player()

外側を、とJavaは、参照の値渡しである、あなたはaddPlayer初めての後、あなただけの新しいデータと同じプレイヤーを毎回更新している:あなたはこれをやっているので、 。コードをデバッグして、最初のセーブコール後にplayerにIDがあるかどうかを確認するだけで、これを確認できます。

あなたはおそらくそれを修正するためにこれをしたいと思います:

for (int i = 0; i < data.size(); i++) { 
    Player player = new Player(); 
    // rest of your code here 
} 
関連する問題