2

Problem: After a long research how I can serialize data from a database with two tables I have found that tutorial. But I need to get the data out of the database and I do not know how to get connect the data to the database. I have found only non-relational samples with one table.

Question: Does someone have a sample for the DAO class to get the data for the characteristics out of the database?

JSON structure needed:

[
   {
      "id":1,
      "name":"CNC",
      "beschreibung":"Metallverarbeitung",
      "characteristics":[
         "id_characteristic":1,
         "id_maschine":2,
         "name":"size",
         "description":"length of maschine",
         "type":1
      ]
   }
]

current JSON structure:

[
   {
      "id":1,
      "name":"CNC",
      "beschreibung":"Metallverarbeitung",
      "characteristics":[

      ]
},

...

]

DAO method (until now, does not fill the characteristics Array):

@Override
    public List<Maschine> list() {
        String selectMaschines = "SELECT * FROM maschine";


        List<Maschine> listMaschine = jdbcTemplate.query(selectMaschines, new RowMapper<Maschine>() {

            @Override
            public Maschine mapRow(ResultSet rs, int rowNum) throws SQLException {
                Maschine aMaschine = new Maschine();

                aMaschine.setId(rs.getInt("Maschine_id"));
                aMaschine.setName(rs.getString("name"));
                aMaschine.setBeschreibung(rs.getString("beschreibung"));


                return aMaschine;
            }

        });

        return listMaschine;
    }

Table structure:

Maschine table:

maschine_id || name || description

Characteristic table:

id_characteristic || id_maschine || name || description || type

1 Answer 1

4

If you want to work with spring data you have to add spring data dependency to your pom.xml

then add the annotation for your entities :

@Entity
public class Maschine implements Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int maschine_id  ;
  private String name ;
  private String description ;
  @OneToMany(mappedBy="maschine")
  private Collection<Characteristic> characteristics;
public Maschine() {
    super();
    // TODO Auto-generated constructor stub
}
public int getMaschine_id() {
    return maschine_id;
}
public void setMaschine_id(int maschine_id) {
    this.maschine_id = maschine_id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}

// Characteristic entity

 @Entity
    public class Characteristic implements Serializable {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int id_characteristic ;
        private String name;
        private String description;
        private String type ;
        @ManyToOne
        @JoinColumn(name="id_machine")
        private Maschine maschine;
        public int getId_characteristic() {
            return id_characteristic;
        }
        public void setId_characteristic(int id_characteristic) {
            this.id_characteristic = id_characteristic;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public Maschine getMaschine() {
            return maschine;
        }
        public void setMaschine(Maschine maschine) {
            this.maschine = maschine;
        }
        public Characteristic() {
            super();
            // TODO Auto-generated constructor stub
        }

    }

And in your DAO package you have to create a interface that extends JPARepository for exemple :

public interface MaschineRepository extends JpaRepository<Maschine,Integer> {

} 

then when you call MaschineRepository.findAll() you will get all the maschines with their Characteristics

Spring data dependency

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

don't forget to add the database configuration in your application.properties if you work with spring boot

Sign up to request clarification or add additional context in comments.

6 Comments

Your answer seems to be nice and correct but I have trouble to import the annotation for the Maschine pojo class. I have added org.springframework.data. Unoftunately eclipse suggests me to create my own annotation. can you post your imports either, please?
Yes of course : import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne;
I am sorry but I still have trouble.. My issue in Maschine class: The attribute strategy is undefined for the annotation type Generated. Eclipse advices me to add missing attributes and then adds value = { "" } which produces new issues
I think that your're problem is in the JPA dependency make sure that you add it in your pom.xml
I have added the following to my pom.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>1.0.0.RELEASE</version> </dependency> </dependency>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.