0

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...

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Asker
  • 424
  • 3
  • 5
  • 16
  • What do you mean with "I think I've installed spring"? Have you added the dependencies to your pom or copied the jars? If so, you should check out some of the many great spring tutorials available. – Christian Jul 18 '17 at 10:49
  • I found spring on the eclipse marketplace, and installed the extension. I made one project that worked, but am not sure how to access a database, and the projects I do, I just read and copy, trying to understand bits and pieces. – Asker Jul 18 '17 at 10:53
  • Don't forget to accept/upvote answers that helped you... – Cepr0 Jul 19 '17 at 06:01

2 Answers2

1
  1. First, setup Maven in Eclipse (you can find instructions in Google, here for example).

  2. 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.

  3. 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()
}
  1. Then create a 'Repository' class which provide access to your database:
public interface EmployeeRepository extends JpaRepository<Employee, Long> {}
  1. Find in your project the 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:

Spring Data JPA Project

Spring Data JPA Reference

Getting started guide

Cepr0
  • 28,144
  • 8
  • 75
  • 101
  • So I did everything up to step 4. I created an interface class in eclipse at the same spot where the Employee.java is. It still doesn't seem to see Entity, Id nor GeneratedValue as existent. I added the get/set ad toString, but when I run the pom file it still crashes. – Asker Jul 19 '17 at 12:32
  • @Asker First: **'Thank you'** equals to **accepting/upvoting the answer** here, don't forget about this! Second, if you don't know how to create project and java-classes in Eclipse you should ask about this in another question. Or just find answer in Google, for [example](https://www.google.com.ua/search?q=my+first+maven+project+in+eclipse). About your last questions - just unpack the project you created at **start.spring.io** in some dir, then open it as **new project** in Eclipse. Then create your entity class and repo class in the same package (folder) of application class. – Cepr0 Jul 19 '17 at 12:33
  • @Asker You don't need to 'run pom file'. You cannot 'run' pom file. You can run Application or some test. In my answer I suggest you just run the test class. – Cepr0 Jul 19 '17 at 12:53
  • [Run test in Eclipse](https://www.google.com/search?q=run+test+in+eclipse&tbm=vid), [Open maven project in Eclipse](https://www.google.com/search?q=open+maven+project+in+eclipse&tbm=vid). [Why Intellij IDEA is better than Eclipse](https://www.google.com/search?q=why+intellij+idea+is+better+than+eclipse) ;) – Cepr0 Jul 19 '17 at 13:07
0

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.

Danikov
  • 735
  • 3
  • 10
  • Thank you for the comment, I work in a company where interns aren't common, and always have my hands full, trying to figure out what this all means... I think I will get better over time, as we have all gone through the same things I am going right now. It's just that I thought this community might offer some readings :) Thank you again! – Asker Jul 18 '17 at 11:13