Reputation: 474
I just started my intern position and am a bit overwhelmed by all the work I need to do. I never worked with databases and don't know how to start. My supervisor asked me to connect Spring data with an in-memory database, to write and delete objects (really anything). I am using eclipse and installed Spring (I think), but am stuck and don't know where to start and make it simple. I never worked in databases before, and my supervisor seems very busy all the time.
I came across this example, but don't know where to write which code, as it seems to be explained for seasoned programmers:
http://projects.spring.io/spring-data/#quick-start
Here is some of the code from the guide:
@Entity
public class Employee {
private @Id @GeneratedValue Long id;
private String firstName, lastName, description;
private Employee() {}
public Employee(String firstName, String lastName, String description) {
this.firstName = firstName;
this.lastName = lastName;
this.description = description;
}
}
This is at the Spring Data front page, and it's about getting started. But I am not sure what any of this means, nor how to run the code or what classes to build. If I try to write their code at the pam file it just shows errors and the entity doesn't work...
Upvotes: 0
Views: 592
Reputation: 30289
First, setup Maven in Eclipse (you can find instructions in Google, here for example).
Then go to start.spring.io and generate your project template. You need to choose just JPA and H2 (H2 - is in-memory database) as dependencies, set your Group ('com.example' by default) and Name ('demo' by default). Then click to 'Generate Project'. After saving and unpacking the file into the some directory on your computer, open this project in your IDE.
You will find an application class - DemoApplication
. Beside it create your entity class, for example - Employee
. You will be saving to and loading from the database its data. Autogenerate getters, setters and toString in this class.
@Entity
public class Employee {
@Id
@GeneratedValue
private Long id;
private String firstName, lastName, description;
private Employee() {}
public Employee(String firstName, String lastName, String description) {
this.firstName = firstName;
this.lastName = lastName;
this.description = description;
}
// Autogenerate here getters, setters and toString()
}
public interface EmployeeRepository extends JpaRepository<Employee, Long> {}
DemoApplicationTests
class, edit it, then run:@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
// Injecting your repository
@Autoware
private EmployeeRepository repo;
@Test
public void dbTest() {
// Create two employees
Employee gandalf = new Employee("Gandalf", "Grey", "Wizard");
Employee frodo = new Employee("Frodo", "Baggins", "Hobbit");
// Save them to DB
repo.save(Arrays.asList(gandalf, frodo));
// Read them from DB
List<Employee> employees = repo.findAll();
// Print them
employees.forEach(System.out::println);
}
}
That's all!
More info:
Upvotes: 1
Reputation: 725
If you haven't used Spring before, you're in for a treat. A lot of developers new to Spring will be thrown by its inversion of control containers and dependency injection. There is a lot of historical tutorial material that works inside the XML configuration files whereas most developers will want to work programmatically. And the worst bit is that Spring tends to do a lot of Magic that is not always obvious when looking at the code.
The first thing to do is ensure that your IDE has the right plugins included. Some of the confusion with Spring can be lessened when you can follow things like injected classes to their definition, which is what the right plugin can do for you.
In terms of the code that you have in front of you, it sounds like your setup is where you're having a problem and that maybe you should be looking at a tutorial for Spring Boot or similar to get a working application/container. This Spring Data starter is presuming you already have a working application and 'starts' at the point of integrating that into the application. Try something like the Sprint Boot quickstart and maybe look into Actuators for nice, off-the-shelf functionality too.
As for having never worked with databases before, that's a huge subject to cover. In some ways Spring Data may save you from some of this as the object relational mapping (ORM for short) will help in terms of automatically converting class instances into database rows. But you also may find yourself starting to struggle with concepts like indexes and referential integrity that are auto-magically done on your behalf when you use annotations like @Id.
I'd be upfront with your supervisor about your weaknesses in the area rather than struggling in silence. Be proactive and find some Database 101 material and read up on the subject when you can, especially if you're sitting idle, and respect that your supervisor probably is having to juggle a number of other priorities. A good developer is never without things to do as personal improvement is a constant background task and Databases is a pretty core skill.
Upvotes: 0