@ OneToMany / @ ManyToOne如何使用表单保存数据

时间:2015-09-07 15:17:14

标签: java spring-mvc spring-data-jpa spring-data

我是新手,想要在使用表单在数据库中保存一些数据的情况下获得一些帮助。 我做了一些工作,这是两个@Entity类

@Entity
public class Artist {


    @Id
    @GeneratedValue
    private Integer id;

    private String name;     

    @OneToMany(mappedBy = "artist")
    private List<Song> songs;

    // setters and getters goes here

}

@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

}

和两个存储库

public interface ArtistRepository extends JpaRepository<Artist, Integer> {

}

public interface SongRepository extends JpaRepository<Song, Integer> {
   List<Song> findByArtist(Artist artist);
}

我可以使用以下类

保存数据
@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);         

    }
}

并使用

获取此数据
@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;
    }    
}

使用此控制器

@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";
    }
}

我在artists.jsp中将其视为

<c:forEach items="${artists}" var="artist">
        <a href='<spring:url value="/artists/${artist.id}"/>'> ${artist.name} </a>
</c:forEach>

并在上面的控制器中添加一个方法来查看带歌曲的艺术家的详细信息

@RequestMapping(value = "/artists/{id}", method = RequestMethod.GET)
    public String showArtistDetail(Model model, @PathVariable Integer id) {
        model.addAttribute("artist", artistService.findArtistbWithSongs(id));
        return "artist-detail";
    } 

和artist-detail.jsp就像

一样简单
<h1>
    ${artist.name }
</h1> 

    <ul>
    <c:forEach items="${artist.songs}" var="song">
        <li>${song.title}</li>
    </c:forEach>
    </ul>

现在我想使用表格保存新的艺术家类别(已经添加或添加新类别),但我想要我不知道,我发现This So Question answer但是有很多JavaScript的东西,我不想要我想用简单的方法来保存艺术家使用表格。请任何帮助。

0 个答案:

没有答案