Persistence de donnée JPA avec le pattern stratégy
Bonjour,
j'essaye de comprendre et surtout d'appliquer la persistance de donnée sue le pattern strategy.
Alors normalement, il faut une superclass abstract vehicule, puis vehicule et moto qui extends de voiture. Mais cela ne m'intéresse pas pour le moment.
J'ai surtout besoin de faire la persistance de donnée des interfaces. Et je n'y arrive pas du tout, j'aurais besoin de votre aide, voici le code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import java.io.Serializable;
@Entity
public class Nobody implements Serializable {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne
private Displacement displacement = new walk();
// how do you walk
public void sayWalking() {
displacement.move();
}
public Nobody() {
}
public Nobody(String name, Displacement displacement) {
this.name = name;
this.displacement = displacement;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Displacement getDisplacement() {
return displacement;
}
public void setDisplacement(Displacement displacement) {
this.displacement = displacement;
}
@Override
public String toString() {
return "nobody{" +
"id=" + id +
", name='" + name + '\'' +
", displacement=" + displacement +
'}';
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import com.example.demo.model.Nobody;
import org.springframework.data.jpa.repository.JpaRepository;
public interface NobodyDAO extends JpaRepository<Nobody, Long> {
}
public interface Displacement {
void move();
}
public class walk implements Displacement {
@Override
public void move() {
System.out.println("I walk with 2 members");
}
}
public class NotWalk implements Displacement {
@Override
public void move() {
System.out.println("I don't walk, but I use a wheelchair");
}
}
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
Nobody nobody = new Nobody();
nobody.setName("test");
nobody.sayWalking();
nobody.setDisplacement(new NotWalk());
nobody.sayWalking();
} |
et j'ai cette erreur:
Citation:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-06-06 01:55:15.625 ERROR 276196 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nobodyDAO' defined in com.example.demo.dao.NobodyDAO defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.example.demo.model.Nobody.displacement references an unknown entity: com.example.demo.model.Displacement
Je n'arrive pas à comprendre comment faire la persistance de l'interface. faut t'il une dao ? Merci pour vos explications.