jublikon
jublikon

Reputation: 3447

spring data serialization with relational tables

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

Upvotes: 2

Views: 2579

Answers (1)

e2rabi
e2rabi

Reputation: 4838

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

Upvotes: 4

Related Questions