hibernate

Retrieve primary key from entity class from sessionfactory in hibernate

元气小坏坏 提交于 2021-01-29 13:21:32
问题 I am creating one SessionFactory using hibernate and I need the primary key of all the tables associated with the entity classes generated from the SessionFactory. Is there any way to achieve this? I have created SessionFactory and from the gathered the ClassMetaData. But unable to retrieve the primary key from the ClassMetaData. 回答1: I don't know which Hibernate version you have. This works for version 4.2.x: Configuration con = // get the org.hibernate.cfg.Configuration for(Iterator

Circular Dependency bidirectional @OneToMany JPA relationship

别说谁变了你拦得住时间么 提交于 2021-01-29 12:39:10
问题 Given the following two entities: @Entity public class Goal { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String description; private BigDecimal amount; @Email private String email; @Email private String supervisorEmail; private LocalDateTime deadline; @Enumerated(EnumType.STRING) @Column(nullable = false) private PaymentPurpose purpose; @Enumerated(EnumType.STRING) private GoalStatus status; @ManyToOne(cascade = CascadeType.ALL) private Person person; /

“Autogenerated PK as Id” and “Autogenerated UUIDs as String” in a single Entity Spring Data JPA

随声附和 提交于 2021-01-29 09:56:38
问题 My question is an extension to this, this and my own previous question. After loads of other readings too, I have the following in my Entity: @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id; @Column(name = "useruuid") @GenericGenerator(name = "uuid", strategy = "uuid4") private String useruuid; Question 1: Are above declarations valid? @Id is for sure yes as we all know it. The question is more focussed on @GenericGenerator(name = "uuid", strategy

Saving entity with @IdClass produces empty row

别来无恙 提交于 2021-01-29 09:40:02
问题 I'd like to save rows in a simple table that contains the ids of two other entities. After some tries, I arrived at the following configuration. Spring Boot now correctly creates the table, with column names task_run_id and project_id . But when I call repo.save(participant) a row is created but both columns contain 0, instead of the required ids. No error is thrown. What am I doing wrong? Also if there there are some declarations that are unnecessary to achieve a simple save and later query,

Spring data JPA - Dynamic SQL Execution

巧了我就是萌 提交于 2021-01-29 09:17:00
问题 I want to Execute the SQL , which will be constructed completely at runtime and it can be querying any tables in the schema. Something like @Repository public interface BaseRepository extends JpaRepository<Object, Integer> { @Query(":dynamicSQL") List<Object> dynamicExecution(@Param("dynamicSQL") String dynamicSQL); } Please suggest how this can be implemented 回答1: JpaRepository is not designed to be used like this. If you want to execute dynamic SQL query then use EntityManager. This is bad

Why is bean not found during Spring Boot?

早过忘川 提交于 2021-01-29 08:18:04
问题 THIS PROBLEM IS SOLVED. SEE THE CHECKED ANSWER BELOW. I reconfigured my DAOs to a more convenient way (by using JpaRepository) instead of doing all that boilerplate code manually. But now everytime I start the Spring Application it gives me the following error: *************************** APPLICATION FAILED TO START Description: Field userRepository in DAO.UserDAOService required a bean of type 'DAO.UserRepository' that could not be found. The injection point has the following annotations: -

Use MySQL datatypes in Apache Derby

孤者浪人 提交于 2021-01-29 08:13:13
问题 Is there a way to use MySQL data-types in Apache Derby? I am trying to make my application supportive of multiple databases and datatypes such as datetime, mediumtext are available in MySQL and not in Apache Derby. So is there a way to make Derby compatible with MySQL? 来源: https://stackoverflow.com/questions/65196315/use-mysql-datatypes-in-apache-derby

Spring Boot - Hibernate - OneToMany - foreign key

隐身守侯 提交于 2021-01-29 07:34:15
问题 I would like to ask a theoretical question. So let's assume I'm using spring boot and hibernate and I would like to persist the following data structure. @Entity @Table(name = "shoppingbasket") @NoArgsConstructor @AllArgsConstructor public class ShoppingBasket { @Getter @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Getter @Setter @Column(name = "userId", unique = true) private String userId; @Getter @Setter @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType

Jpa repository and JsonIgnoreProperties return the same object differently in getOne() and in findAll() methods

牧云@^-^@ 提交于 2021-01-29 07:06:51
问题 Brief background I have two methods in an API created on Spring Boot to retrieve data from a mySQL database (via Hibernate and JpaRepository). There is a method that returns all the ocurrences in a table called test , and another one that returns the test corresponding to the id passed as a parameter in the GET call. Both API entry points (by means of the services and repositories) end up calling two JpaRepository methods ( findAll() and getOne() respectively). Problem description The problem

JPA Hibernate cascade type for child of child

会有一股神秘感。 提交于 2021-01-29 06:44:42
问题 I was looking for documentation or answer how will the cascade work for child of child , example : public class Parent{ @OneToMany(fetch = FetchType.EAGER,mappedBy = "parent",cascade = CascadeType.ALL) private List<Child> child; } public class Child{ @OneToMany(mappedBy="child") private List<AnotherChild> anohterChild; } @ManyToOne private Parent parent; } now the question, will the cascade operation applied on "Child" from parent class apply to "AnotherChild" ? in other words if I persist