Hibernate mapping camelCase field to snake_case - postgresql

I am using hibernate with postgresql. I am curious if is it possible to map for example this entity:
#Entity
#Data
#NoArgsConstructor
class User {
#Id
private Long id;
private String firstName;
private String lastName;
}
in database I have columns like first_name, last_name. I got error that I introduce wrong names (when I add for example #Column(name = "first_name"); everything is ok. How can I avoid adding #Column neither change database?

Related

How to auto generate id using MongoDB and Spring Data MongoDB?

I need to auto generate the id on my documents for persist on database. But if i don't set the id it has an error that cannot be null. How could I generate the id for reduce the repeated code and make it simple?
#Id
private ObjectId id;I found a solution that is,
must the type of the id be an org.bson.types.ObjectId, like above:
#ToString
#Getter
#NoArgsConstructor
#RequiredArgsConstructor(staticName = "of")
public class Guide {
#Id
private ObjectId id;
#NonNull
private String name;
}
The right place that are the solution is:
#Id private ObjectId id;

Repository findIn confusion

My database has an Exchanges class which contains a list of CurrencyPairs.
Is it possible to use to use a Repository method to directly obtain a CurrencyPair which matches on name within a given Exchange? I'm thinking of something like
CurrencyPairDbo findByExchangeNameAndCurrencyPairIn(...)
but I can't see quite how to tie it all together. Or do I need to write a custom query for this? And does this need to be in the ExchangeRepository or the CurrencyPairRespository?
#Entity()
#Table(name = "Exchanges")
public class ExchangeDbo {
#Id #GeneratedValue
#Getter private Long id;
#Getter private String exchangeName;
#OneToMany(mappedBy = "exchange",
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.EAGER)
#BatchSize(size=100)
#Getter private List<CurrencyPairDbo> listCurrencyPair = new ArrayList<>();
...
}
#Entity()
public class CurrencyPairDbo {
#Id #GeneratedValue
#Getter private Long id;
#Column(unique=true)
private String currencyPair;
#ManyToOne(fetch=FetchType.EAGER)
#Getter private ExchangeDbo exchange;
...
}
Edit:
I'm thinking it's not Find...In that I want at all. I think that something like:
List<CurrencyPairDbo> x = exchangeRepository.findByExchangeNameLowercaseAndListCurrencyPairCurrencyPair(exchangeName.toLowerCase(), currencyPair);
might work, except that in returns an Exchange object and a:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [biz.ianw.coindatabase.database.ExchangeDbo] to type [biz.ianw.coindatabase.database.CurrencyPairDbo]
This, in the currency pair repository, seems to do the job.
I added a lower case field for matching purposes and an index for efficiency.
CurrencyPairDbo findByExchangeExchangeNameLowercaseAndCurrencyPairNameLowercase( String exchangeName, String currencyPair );

#OneToMany and #ManyToOne in MongoDB and JPA -EISOneToOneMapping cannot be cast to

We're trying to do a #OneToMany and #ManyToOne relation with EclipseLink/MongoDB:
The #OneToMany declaration looks like this:
#Entity
#NoSql(dataType = "ServiceCatalog", dataFormat = DataFormatType.MAPPED)
public class ServiceCatalog {
#Id
#GeneratedValue
#Field(name = "_id")
private String id;
#OneToMany
private List<ServiceCatalogNeedCategory> serviceCatalogNeedCategories;
…
On the other side, the #ManyToOne declaration:
#Entity
#NoSql(dataType = "NeedCategory", dataFormat = DataFormatType.MAPPED)
public class ServiceCatalogNeedCategory {
#Id
#GeneratedValue
#Field(name = "_id")
private String id;
#Field(name = "title")
private String Title;
#ManyToOne(fetch=FetchType.LAZY)
private ServiceCatalog serviceCatalog;
...
The above configuration leads to the following error:
org.eclipse.persistence.eis.mappings.EISOneToOneMapping cannot be cast to org.eclipse.persistence.mappings.OneToOneMapping
We really need to be able to resolve both directions.
Cheers
Michael
Please include the full exception stack.
There was an issue fixed in the 2.6 dev stream for NoSQL, it may be related.
https://twitter.com/j_b_sutherland/status/339727557928833025
What version are you using, I would try at least 2.5, or a recent 2.6 dev build.
You missed the MappedBy Annontation on the OneToMany side.
See http://www.objectdb.com/api/java/jpa/OneToMany
[edit]
OMG it's NOSQL sorry.
ORM are no more what they used to be..
I don't know so, but if you're really using JPA API then it might be the trick

Cannot change colum names in JPA Eclipselink

I'm having problems making #Column(name="example") working.
I've got a User class:
#Entity
public class User {
#Id
private String username;
....
}
A Role one:
#Entity
public class Role {
#Id
private String name;
....
}
That are in a ManyToMany relationship. So I created a RoleMembership:
#Entity
#IdClass(UserRolePK.class)
public class RoleMembership {
#Id
#ManyToOne
#PrimaryKeyJoinColumn(name="USERNAME")
private User user;
#Id
#ManyToOne
#PrimaryKeyJoinColumn(name="ROLE")
private Role role;
....
}
As you can see, the primary key is defined in UserRolePK:
public class UserRolePK{
#Id
#Column(name="ROLE")
private String role;
#Id
#Column(name="USERNAME")
private String user;
...
}
In this class I use #Column(name="USERNAME") and #Column(name="ROLE") to force its name to that string, but it's not working: JPA gives it the default names USER_USERNAME and ROLE_NAME (that are in TABLE_ID format).
Can anyone help me finding the mistake?
Thanks,
Andrea
EDIT:
I need to have three tables:
user (username (pk), password ....)
user_role (username, role_name)
role (name (pk), description)
I cannot change User definition in my model.
Remove all the annotations in UserRolePK and change your #PrimaryKeyJoinColumn annotations to #JoinColumn annotations.
Your usage of PrimaryKeyJoinColumn does not seem to make sense. I think you should be using just a #JoinColumn, or are you also mapping the columns as basics?
Perhaps include you complete class, and the DDL that is generated.

JPA JoinColumn does not work

I have started with an example from a book. It was really a dummy oriented one and I am having a problem with the code the book gave.
Here is the code;
#Entity
public class Customer {
#Id
#GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
#OneToOne (fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
#JoinColumn(name = "address_fk")
private Address address;
//getter, setter, Constructor
--//-------------------------------------
#Entity
public class Address {
#Id #GeneratedValue(strategy = GenerationType.AUTO)
private String id;
private String city;
private String street;
private String number;
In here it is complaining on the JoinColumn annotation. It says the column address_fk is not found.
Is this an IDE related issue? Am I missing something?
Edit : No table yet created in the DB. I am expecting them to be seen on the DB automatically by my persistence.xml
<property name="eclipselink.ddl-generation" value="create-tables"/>
Using #JoinColumn annotation to specify foreign column name to be address_fk instead of default address_id is fine.
What likely does not to work is #GeneratedValue with String. According specification only integral types as generated primary keys are portable.