I am newbie and wanna to get some help in case of saving some data in database using form. I've done some work which is two @Entity classes
@Entity
public class Artist {
@Id
@GeneratedValue
private Integer id;
private String name;
@OneToMany(mappedBy = "artist")
private List<Song> songs;
// setters and getters goes here
}
and
@Entity
public class Song{
@Id
@GeneratedValue
private Integer id;
private String title;
@ManyToOne
@JoinColumn(name = "artist_id")
private Artist artist;
// setters an getters goes here
}
and two Repositories
public interface ArtistRepository extends JpaRepository<Artist, Integer> {
}
and
public interface SongRepository extends JpaRepository<Song, Integer> {
List<Song> findByArtist(Artist artist);
}
I can save data using the following class
@Transactional
@Service
public class DatabaseServiceInitializer {
@Autowired
private ArtistRepository artistRepository;
@Autowired
private SongRepository songRepository;
@PostConstruct
public void dbInitializer() {
Artist artist1 = new Artist();
artist1.setName("Rock Star");
artistRepository.save(artist1);
Song song1 = new Song();
song1.setTitle("Stand Up for Rock Star"); // Dummy/fake song name
song1.setArtist(artist1);
songRepository.save(song1);
Song song2 = new Song();
song2.setTitle("Sit Down for Looser"); // Dummy/fake song name
song2.setArtist(artist1);
songRepository.save(song1);
}
}
and get this data back using
@Service
public class ArtistService{
@Autowired
private ArtistRepository artistRepository;
@Autowired
private SongRepository songRepository;
@Override
public List<Artist> findAll() {
return artistRepository.findAll();
}
public Job findArtistById(Integer id) {
return artistRepository.findOne(id);
}
public Artist findArtistWithSong(Integer id) {
Artist artist= findArtistById(id);
artist.setSongs(songRepository.findByJob(artist));
return artist;
}
}
using this controller
@Controller
public class ArtistController {
@Autowired
private ArtistService artistService;
@RequestMapping(value = "/artists", method = RequestMethod.GET)
public String showArtists(Model model) {
model.addAttribute("artists", artistServicefindAll());
return "artists";
}
}
and I view this in artists.jsp as
<c:forEach items="${artists}" var="artist">
<a href='<spring:url value="/artists/${artist.id}"/>'> ${artist.name} </a>
</c:forEach>
and Adding one more method in above controller to see details of Artist with songs is
@RequestMapping(value = "/artists/{id}", method = RequestMethod.GET)
public String showArtistDetail(Model model, @PathVariable Integer id) {
model.addAttribute("artist", artistService.findArtistbWithSongs(id));
return "artist-detail";
}
and artist-detail.jsp is just simple as
<h1>
${artist.name }
</h1>
<ul>
<c:forEach items="${artist.songs}" var="song">
<li>${song.title}</li>
</c:forEach>
</ul>
now I want to save new Artist with categories(already added or add new category) using form but I' have no idea, I have found This So Question answer but there is a lot of JavaScript stuff which I do not want I want simple approach to save Artist using a form. please any help.