Last active
July 28, 2022 13:43
-
-
Save mikaelhg/8c85f3251c0ba5690167 to your computer and use it in GitHub Desktop.
Spring Boot + Spring Data JPA + Spring Data REST + orm.xml = retrofitting pre-existing POJOs for REST API use
This file contains hidden or 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
| @SpringBootApplication | |
| @RestController | |
| public class Application { | |
| @Configuration | |
| public static class HibernateConfiguration extends HibernateJpaAutoConfiguration { | |
| @Bean | |
| @Override | |
| public LocalContainerEntityManagerFactoryBean entityManagerFactory( | |
| EntityManagerFactoryBuilder factoryBuilder) | |
| { | |
| final LocalContainerEntityManagerFactoryBean ret = super.entityManagerFactory(factoryBuilder); | |
| ret.setMappingResources("orm.xml"); | |
| return ret; | |
| } | |
| } |
This file contains hidden or 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
| <?xml version="1.0" encoding="UTF-8" ?> | |
| <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" | |
| version="2.0"> | |
| <entity class="org.springframework.social.twitter.api.Tweet" access="FIELD"> | |
| <table name="tweets"/> | |
| <attributes> | |
| <id name="id"> | |
| <generated-value strategy="AUTO"/> | |
| </id> | |
| <basic name="text"> | |
| <column name="text" length="200"/> | |
| </basic> | |
| <basic name="createdAt"> | |
| <column name="created_at" column-definition="TIMESTAMP" /> | |
| </basic> | |
| <basic name="fromUser"> | |
| <column name="from_user" length="200"/> | |
| </basic> | |
| <basic name="profileImageUrl"> | |
| <column name="profile_image_url" length="200"/> | |
| </basic> | |
| <basic name="toUserId"> | |
| <column name="to_user_id" column-definition="INTEGER"/> | |
| </basic> | |
| </attributes> | |
| </entity> | |
| </entity-mappings> |
This file contains hidden or 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 io.mikael.demo; | |
| import org.springframework.data.jpa.repository.JpaRepository; | |
| import org.springframework.social.twitter.api.Tweet; | |
| public interface TweetRepository extends JpaRepository<Tweet, Long> { | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment