1

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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348

1 Answers1

0

It sounds like you want to do this all on the server side, using spring/java etc... If you add a field for categories that a user can select from, and another field for adding new categories; something that you can parse on the server and know what is being passed in. If you allow the user to select multiple categories, you can't escape the javascript of allowing them to add another field for a new category unless you use a textarea or a jquery plugin that would allow multiple entries. Either way, you will have to parse this on the server so that you can make the entries in the database accordingly.

I would write a separate service to handle this and make sure it is transactional so that if for some reason the new categories fail to be inserted, the other data won't be inserted. I'm going to reuse your code for the sake of creating a sample:

public void dbInitializer() {

    // INSERT CATEGORY FIRST
    Category c = new Category();
    c.setName("Heavy Metal");
    categoryRepository.save(c);

    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);
    song1.setCategory(c);   
    songRepository.save(song1);

    Song song2 = new Song();            
    song2.setTitle("Sit Down for Looser"); // Dummy/fake song name
    song2.setArtist(artist1);
    song2.setCategory(c);       
    songRepository.save(song1);         

}

Make sure to annotate the class @Transactional, you may want to check out Using @Transactional and get an idea of what you need to configure.

Hatem Jaber
  • 2,341
  • 2
  • 22
  • 38