hibernate

Spring+Hibernate mysql dataBase initialized with sample data

半世苍凉 提交于 2021-01-29 02:04:17
问题 I want to deploy a Spring-mvc + Hibernate project, and I want: create all database schemas automatically for the first lauch update a schema if attributes are added. add some records in some tables, a root user for USER,ect. If hibernate.hbm2ddl.auto is set to "create" , I will lost previous data every time I restart the project. If set to "update" , my import.sql where i put sql to create sample records, won't be executed. So, what's the best pactice in a "product" environment? 回答1:

Custom type / converter in conjunction with Hibernate's Transformers.aliasToBean

邮差的信 提交于 2021-01-28 23:40:48
问题 I'm executing a native query in Hibernate and I want to transform the result set into a List of MyDto's. When using native queries, we typically follow this pattern: String query = "SELECT first_name as firstName, birth_date as birthDate ..." def results = session.createSQLQuery(query) .setResultTransformer(Transformers.aliasToBean(MyDto.class)) .list(); The problem is we're using Joda-time for our date classes (in this case birthDate is a LocalDate). With normal Hibernate queries this is

CrudRepository.save() appears to cascade

跟風遠走 提交于 2021-01-28 22:55:30
问题 Entity Survey: @Entity @Table(name = "Survey") public class Survey implements Serializable { private Long id; private String name; private String address; private List<Question> questions; @Id @Column(name = "id") public Long getId() { return id; } @Column(name = "name") public String getName() { return name; } @Column(name = "address") public String getAddress() { return address; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "survey") public List<Question> getQuestions() { return questions

How to handle DataIntegrityVilolationException while saving a list in Spring Data JPA?

别等时光非礼了梦想. 提交于 2021-01-28 22:38:46
问题 I am using Spring Data JPA in a Spring Boot Application, with MYSQL. There I am saving a list of entities with unique constraint over a field. Out of the list of entities, there is one entity that will throw DataIntegrityViolationException due to the unique constraint. I noticed that none of the entities get persisted in that case, even those that does not violate the unique constraint. What should be the ideal approach in this case so that those entities which do not violate the unique get

hibernate-configuration session-factory name

别说谁变了你拦得住时间么 提交于 2021-01-28 21:43:56
问题 I'm new to hibernate and I'm trying to connect to multiple DB's. I know that we can create a new cfg file separate for each DB and then create an factory of it like factory1 = new Configuration().configure(cfg1.xml).buildSessionFactory(); factory2 = new Configuration().configure(cfg2.xml).buildSessionFactory(); But wanted to know what is the meaning of having a name like session-factory name="SESS1" in hibernate-configuration and can I use that to define multiple DB sessions there instead of

org.hibernate.MappingException: Could not determine type for: at table: for columns: [org.hibernate.mapping.Column(plant)

旧时模样 提交于 2021-01-28 20:03:25
问题 I know this question have been asked several times but, they didn't help me. I have the following test: public class PlantCatalogTests { @Autowired PlantInventoryEntryRepository plantRepo; @Test public void queryPlantCatalog() { assertThat(plantRepo.count(), is(14l)); } and here is PlantInventoryEntryRepository @Repository public interface PlantInventoryEntryRepository extends JpaRepository<PlantInventoryEntry, Long> {} As you see, this repository is based on the class PlantInventoryEntry

Hibernate Criteria to pull records from OneToMany Relations

故事扮演 提交于 2021-01-28 19:49:22
问题 I am passing a collection of vehicles names like ['car','jeep','truck','bike'] and want to select those owners who owns vehicles in this list using Criteria query, Owner here can own multiple vahicles (OneToMany). I have a limitation that i need to use Criteria query. class Owner { @ID @GeneratedValue(strategy = IDENTITY) @Column(name = "owner_id") private Long ownerId; @OneToMany(cascade = CascadeType.ALL) @JoinColumn(name = "owner_id") private Set<Vehicles> vehicles; } class Vehicles { @ID

Jackson serialization issue. Only first object of the same entity serializes well

喜欢而已 提交于 2021-01-28 15:31:36
问题 I develop a REST voting system where users can vote on restaurants. I have a Vote class which contains User, Restaurant and Date. public class Vote extends AbstractBaseEntity { @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; @NotNull @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "restaurant_id") private Restaurant restaurant; @Column(name = "date", nullable = false) @NotNull private LocalDate date; } I need to find all votes of the day.

How to deal with circular references in JPA and Hibernate with associative entity

时光怂恿深爱的人放手 提交于 2021-01-28 15:00:06
问题 I've got 3 entities: Realestate , Realtor and the associative entity RealestateRealtor . One Realestate can have multiple Realtors and vice versa. The many-to-many relation has an extra entity RealestateRealtor with a specific url field. Question: How can I create a working reference using @JsonIgnore, @JsonManaged etc... so I can request a Realtor having all of it's Realestate childs and in another request a Realestate having all of it's Realtor childs without crippling one of them or being

How to deal with circular references in JPA and Hibernate with associative entity

本小妞迷上赌 提交于 2021-01-28 14:55:58
问题 I've got 3 entities: Realestate , Realtor and the associative entity RealestateRealtor . One Realestate can have multiple Realtors and vice versa. The many-to-many relation has an extra entity RealestateRealtor with a specific url field. Question: How can I create a working reference using @JsonIgnore, @JsonManaged etc... so I can request a Realtor having all of it's Realestate childs and in another request a Realestate having all of it's Realtor childs without crippling one of them or being