I would like to make a unique field "number" with autogeneration(autoincrement, last number + 1). But it is not an id field. How can i reach that? #GeneratedValue doesn't work. Only id was generated. My code doens't work.
My entity
#Entity
#Table(schema = "public")
public class Policeman implements Serializable {
#Id
#GeneratedValue
private Long id;
#Column
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long number;
#Column
private String fam;
#Column
private String name;
#Column
private String otch;
//setters getters
}
This SO question looked the same: Link1
It says that the #GeneratedValue annotation is only used in conjunction with #Id to create auto-numbers. It cannot be used with non-id columns. However, there is a workaround that suggests to create a separate entity with a generated Id, something like this:
#Entity
public class GeneralSequenceNumber {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long number;
}
#Entity
#Table(schema = "public")
public class Policeman implements Serializable {
#Id
#GeneratedValue
private Long id;
#OneToOne(...)
private GeneralSequnceNumber myVal;
#Column
private String fam;
#Column
private String name;
#Column
private String otch;
//setters getters
}
You can also refer to the following link for more details on this work-around:Link2
I hope it helps.
Related
I am using Spring Boot with Spring Data JPA. I have two tables named User and Employee.
Employee.java
#Data
#NoArgsConstructor
public class Employee implements Serializable
{
private static final long serialVersionUID = -3732596549369515261L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String designation;
private Date dateOfJoining;
private String workLocation;
#Column(unique = true)
private Long contactNo;
#Column(unique = true)
private String email;
private Date dateOfLeaving;
}
User.java
#Entity
#Data
#NoArgsConstructor
public class User
{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;
}
I want to create a relation between Employee.email with User.username. Will this be possible via any JPA annotations.
I tried with Mappings from JPA and none of them is working as the User.username is String, unlike Employee object.
So here is an example close to my actual code:
#Entity
#Table(name="Products")
#Data
public class Product{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="productno")
private long ID;
#Column(name="name")
private String productName;
private ProductCategory category;
}
#Entity
#Table(name="Category")
#Data
public class ProductCategory{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="categoryno")
private long ID;
#Column(name="name")
private String categoryName;
#OneToMany
#JoinColumn(name="category")
private List<Product> product;
}
aswell as a thymeleaf template, that displays a bunch of those in a table using th:each
and a controller that sends the data to those templates. Those templates have been tested and they should work.
the problem is, when bootRunning and opening the page, i get a error:
SQL.SyntaxErrorException: Unknown column 'product0_.productCategory_category'
Looking at this error, it seems it tried to read the join column name as productCategory_category instead of just category (which is the desired one)
#JoinColumn needs to go on Product class on ProductCategory field and name="categoryno"
So i've solved this thing with
#Entity
#Table(name="Products")
#Data
public class Product{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="productno")
private long ID;
#Column(name="name")
private String productName;
#ManyToOne
#JoinColumn(name="category", referencedColumnName="categoryno")
private ProductCategory category;
}
#Entity
#Table(name="Category")
#Data
public class ProductCategory{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="categoryno")
private long ID;
#Column(name="name")
private String categoryName;
#OneToMany(mappedBy="category")
private List<Product> product;
}
i have no idea why this works, but hey: magic
I've got following two tables:
Customer
id
name
Order
id
product_name
customer_id
with a 1 to 1 relation
and java entities:
#Data
public class Customer{
#Id
private Long id;
private String name;
}
#Data
public class Order{
#Id
private Long id;
#Column("id")
private Customer customer; //i want to somehow map this
private String productName;
}
and a controller
#Controller
public class MyController{
//...
#GetMapping("/")
public String getmap(Model m){
System.out.println(repository.findAll()) //prints "nullrows" due to wrong sql statement
return "mytemplate";
}
}
my current issue is, that spring is executing following sql statement:
SELECT Order.id, Order.product_name, Customer.id, Customer.name
FROM Order LEFT OUTER JOIN Customer ON Customer.id = Order.id
what i actually want is to join on Customer.id = Order.customer_id while leaving the classes as they are i.e. the customer reference needs to stay in order.
i've tried every annotation that i could find so far and have made no progress.
EDIT:
I am not allowed to use jpa/hibernate
One workaround is to do the following:
#Data
public class Customer{
#Id
private Long id;
private String name;
}
#Data
public class Order{
#Id
private Long customerId;
private Long id;
#Column("id")
private Customer customer; //i want to somehow map this
private String productName;
}
causing this to automatically join on Customer.id = Order.customer_id
This does not look like a good fix however.
You can use #OneToOne and #JoinColumn annotations for your One-to-One relationship:
#Data
public class Customer{
#Id
#Column(name = "id")
private Long id;
#Column(name = "name")
private String name;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "order_id", referencedColumnName = "id")
private Order order;
}
#Data
public class Order{
#Id
#Column(name = "id")
private Long id;
#Column(name = "product_name")
private String productName;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "customer_id", referencedColumnName = "id")
private Customer customer;
}
I am new to hibernate and I am trying to implement a basic application that uses this schema (it does not follow the notation I just use it for clarity)
Here is the my classes
#Entity
#Table(name = "race")
public class Race {
#Id
#GeneratedValue
private UUID id;
private String name;
}
#Entity
#Table(name="np_character")
public class NPCharacter {
#Id
#GeneratedValue
private UUID id;
#OneToOne
private Race race;
private String name;
private int age;
}
#Entity
#Table(name="main_female_character")
public class MainFemaleCharacter {
#Id
#GeneratedValue
private UUID id;
#OneToOne
private Race race;
private String name;
private int age;
}
#Entity
#Table(name="copulation_registry")
public class CopulationRegistry {
// ??
private NPCharacter npCharacter;
// ??
private MainFemaleCharacter femCharacter;
private int times;
}
But I ran into the problem with copulation_registry class. I used everywhere OneToOne annotation, instead of using references to keys. But what I should do here? Pairs of id_femPlayer and id_npCharacter are unique.
Should I use EmbeddedId annotation or is it possible somehow to use association annotations to represent the same relation?
You can annotate class CopulationRegistry with #IdClass
#Entity
#IdClass(CopulationRegistryKey.class)
#Table(name="copulation_registry")
public class CopulationRegistry {
#Id
private NPCharacter npCharacter;
#Id
private MainFemaleCharacter femCharacter;
private int times;
}
public class CopulationRegistryKey{
private NPCharacter npCharacter;
private MainFemaleCharacter femCharacter;
}
I am new to Hibernate and I am using it with Spring. I have the following tables:
#Entity
#Configurable
public class Location {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long locationid;
private String locationName;
#ManyToOne
private Site site;
//getters setters skipped
}
#Entity
#Configurable
public class Site {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long siteid;
private String siteName;
#ManyToOne
private Country country;
//getters setters skipped
}
#Entity
#Configurable
public class Country {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long countryid;
private String countryName;
#ManyToOne
private Region region;
//getters setters skipped
}
#Entity
#Configurable
public class Region {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long regionid;
private String regionName;
//getters setters skipped
}
public class Assets {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long assetId;
#ManyToOne
private Location location;
//getters setters skipped
}
I want to pull all assets based on Regions. How should I do that?
How do I use the relationship between Region --> Country --> Site --> Location and pull the relevant records? How do I do it without affecting the performance?
Or should I just redesign the tables?
You can just complete it by a #NamedQuery in the class Assets :
select a from Asserts a where a.location.site.contry.regin.reginName = :reginName
Hope help.