2016-04-19 3 views
1

私のプロジェクトでチュートリアルと私はspring dataを使っています。data to databaseを追加しようとしています。イムは、私が言ってエラーを取得することをやろうとし、次の. BTを使用してCrudRepository.save(java.lang.Object)を修正するには、springbootにアクセサメソッドがありませんか?

呼び出されたメソッドパブリック抽象java.lang.Objectの org.springframework.data.repository.CrudRepository.save(java.lang.Object上位) にアクセサーメソッドはありません!ここ

誰かが私におよそspringdata 他より知るためのリンクを提供することができれば は感謝し、

//my controller 

@RequestMapping("/mode") 
    public String showProducts(ModeRepository repository){ 
     Mode m = new Mode(); 
     m.setSeats(2); 
     repository.save(m); //this is where the error getting from 
     return "product"; 
    } 


//implementing crud with mode repository 
@Repository 
public interface ModeRepository extends CrudRepository<Mode, Long> { 

} 

//my mode class 
@Entity 
@Table(name="mode") 
public class Mode implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    @Column(unique=true, nullable=false) 
    private int idMode; 


    @Column(nullable=false) 
    private int seats; 

    //assume that there are getters and setters 
} 

springbootへイム新しい、誰かが私が間違っているの何Tellmeのことができ、私のコードですspring documentation

+0

どのようにリポジトリをコントローラに挿入しますか? – WeMakeSoftware

+0

'showProducts(ModeRepository repository)' - 私がautowiredと思うメソッドの中の引数として – Priyamal

答えて

11

ModeRepositoryがプライベートautowiredフィールドになるようにコントローラコードを変更してください。

@Autowired //don't forget the setter 
    private ModeRepository repository; 

    @RequestMapping("/mode") 
    public String showProducts(){ 
     Mode m = new Mode(); 
     m.setSeats(2); 
     repository.save(m); //this is where the error getting from 
     return "product"; 
    } 
+0

うわー、それは実際に呼び出されました、なぜ私は 'メソッドパラメータ'から渡された '依存性'を得るべきではありません! – Priyamal

+3

通常コントローラメソッドの引数は、モデル(パースされたhttp要求)をコントローラロジック – WeMakeSoftware

+0

に渡すために使用されます。それが助けになった – Priyamal