Created
November 25, 2011 21:43
-
-
Save Promichel/1394479 to your computer and use it in GitHub Desktop.
Spring JPA Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.sharea.svnpad.dao.entities; | |
import javax.persistence.*; | |
@Entity | |
public class Repository { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Integer id; | |
private String name; | |
public Integer getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Basic | |
private String url; | |
public String getUrl() { | |
return url; | |
} | |
public void setUrl(String url) { | |
this.url = url; | |
} | |
@OneToOne | |
private Group group; | |
public Group getGroup() { | |
return group; | |
} | |
public void setGroup(Group group) { | |
this.group = group; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.sharea.svnpad.dao; | |
import de.sharea.svnpad.dao.entities.Repository; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
public interface RepositoryDao extends JpaRepository<Repository, Integer> { | |
/** | |
* Selects a repository by given repository name | |
* | |
* @param name Name of Repository | |
* @return Repository object | |
*/ | |
Repository findByName(String name); | |
/** | |
* Selects a repository by given repository id | |
* | |
* @param id Id of Repository | |
* @return Repository object | |
*/ | |
Repository findById(Integer id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.sharea.svnpad.dao; | |
import de.sharea.svnpad.dao.entities.Repository; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.test.context.transaction.TransactionConfiguration; | |
import static org.junit.Assert.assertTrue; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations = {"classpath:META-INF/applicationContext.xml"}) | |
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false) | |
public class RepositoryDaoTest { | |
@Autowired | |
private RepositoryDao dao; | |
private Repository repository; | |
@Before | |
public void insertTestRepository() { | |
repository = new Repository(); | |
repository.setName("Testrepo"); | |
repository.setUrl("http://127.0.0.1/svn/testrepo"); | |
dao.save(repository); | |
} | |
@Test | |
public void selectByName() { | |
Repository testRepo = dao.findByName("Testrepo"); | |
assertTrue(testRepo != null); | |
assertTrue("Check if name is correct", testRepo.getName().equals("Testrepo")); | |
assertTrue("Check if url is correct", testRepo.getUrl().equals("http://127.0.0.1/svn/testrepo")); | |
} | |
@After | |
public void deleteTestRepository() { | |
dao.delete(repository); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment