Arkadaşlar merhaba. Kısaca durumu şöyle anlatayım. Service sınıfında, repository interface instance edip, @autowired annotasyonu ile Spring'i otomatik bağlamasını istedim.
Belki repository interface'sini bir sınıf implemente edip kullanabilirdim, denemedim ama @autowired daha kullanışlı gibi duruyor.
Uygulamanın verdiği hata bu;
java.lang.NullPointerException: Cannot invoke "com.tensa.proje.repository.TestModelRepository.save(Object)" because "this.testModelRepository" is null
Ama veritabanında veriler olmadan tabloları oluşturuyor.
Kodlarım budur.
Model Sınıfları;
Repository interfaceleri;
Service sınıfı
Main sınıfı
Belki repository interface'sini bir sınıf implemente edip kullanabilirdim, denemedim ama @autowired daha kullanışlı gibi duruyor.
Uygulamanın verdiği hata bu;
java.lang.NullPointerException: Cannot invoke "com.tensa.proje.repository.TestModelRepository.save(Object)" because "this.testModelRepository" is null
Ama veritabanında veriler olmadan tabloları oluşturuyor.
Kodlarım budur.
Model Sınıfları;
Java:
package com.tensa.proje.model;
import java.util.List;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TestModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "testModel")
private List<TestModel2> testModel2s;
}
Java:
package com.tensa.proje.model;
import java.util.List;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TestModel2 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@ManyToOne
@JoinColumn(name = "testModelID")
private TestModel testModel;
}
Repository interfaceleri;
Java:
package com.tensa.proje.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.tensa.proje.model.TestModel;
@Repository
public interface TestModelRepository extends JpaRepository<TestModel, Integer> {
}
Java:
package com.tensa.proje.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.tensa.proje.model.TestModel2;
@Repository
public interface TestModel2Repository extends JpaRepository<TestModel2, Integer> {
}
Service sınıfı
Java:
package com.tensa.proje.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tensa.proje.model.TestModel;
import com.tensa.proje.repository.TestModelRepository;
@Service
public class TestModelService {
@Autowired
TestModelRepository testModelRepository;
public void save(TestModel testModel) {
testModelRepository.save(testModel);
}
}
Main sınıfı
Java:
package com.tensa.proje;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.tensa.proje.model.TestModel;
import com.tensa.proje.model.TestModel2;
import com.tensa.proje.service.TestModelService;
@SpringBootApplication
public class ProjeApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ProjeApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
TestModel2 testModel21 = new TestModel2();
testModel21.setName("1. Test Model 2");
TestModel2 testModel22 = new TestModel2();
testModel22.setName("2. Test Model 2");
List<TestModel2> list = new ArrayList<TestModel2>();
list.add(testModel22);
list.add(testModel21);
TestModel testModel = new TestModel();
testModel.setName("Test Model");
testModel.setTestModel2s(list);
TestModelService modelService = new TestModelService();
modelService.save(testModel);
}
}
Son düzenleyen: Moderatör: