This field "auftragsnummer" always stays null, even though it is annotated with #GeneratedValue:
#Entity
public class Auftrag implements Serializable
{
#Id
#GeneratedValue
private int id;
#Pattern(regexp = AUFTRAGSNUMMER_REGEXP, message = "{validator.auftragsnummer}")
#Length(min = 20, max = 20)
#GenericGenerator(name = "sequence_auftragsnummer", strategy = "de.software.AuftragsnummerGenerator")
#GeneratedValue(generator = "sequence_auftragsnummer")
#Column(unique = true, nullable = false)
private String auftragsnummer;
}
I store it using getHibernateTemplate().persist(t). The referenced generator class implements org.hibernate.id.IdentifierGenerator.
I have no idea why it is ignored.
Environment:
Hibernate 5.0.1
Spring 4.2.1
Java 7
DB2 10
JPA only mandates support for #GeneratedValue on #Id fields.
If your order number doesn't depend on the entity's primary key, then the easiest solution is to use #PrePersist e.g.
#PrePersist
public void onCreate() {
auftragsnummer = ...;
}
Related
Hello i have an entity that have an inner id autogenerated by db and the uuid used as the actual id in the rest of the project. Immagine something like this:
#Entity
#Data
public class Task {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(nullable = false)
private Long innerId;
#Column(nullable = false, unique = true)
#Type(type="uuid-char")
private UUID uuid;
private String name;
}
i would like to set the uuid as the ID for javers, is there any way of doing so ? becaus javers "detects" the "persistence id annotation" and establish that as its own id.
I tried adding to the uuid column the annotation
#org.javers.core.metamodel.annotation.Id
but in this case javers creates a composite id made of innerId and uuid, and this doesn't works for me
I am new to Spring and JPA and I am trying to write a job in Spring which runs every 3 hours and retrieve the records from Oracle Database.
I would like to only read the new/updated content from the past 3 hours (ideally from the last job run).
I have seen few examples in https://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/ where we can create queries and retrieve the data based on our requirements, but in my current use case, I am not using queries instead using the java classes with the annotations and using Join columns between different tables. There are chances that only one of the sub table is updated or all the tables are updated with new content. I need to get the results if at least one of the table is updated/inserted with new content.
Campus is the main table and retrieves the data from Group and Region, I need to fetch the data if any new data is updated in Campus table or even any group/region is added/modified.
I am using JDK7 as of now.
Is there a way to accomplish the above requirement?
Below are the sample Java classes for reference.
#Entity
#EntityListeners(AuditListener.class)
#Table(name = "TABLE_CAMPUS")
public class Campus implements Auditable {
#Id
#Column(name = "ID)
#SequenceGenerator(name = "SIMPLE_ID", sequenceName = "SIMPLE_ID")
#GeneratedValue(generator = "SIMPLE_ID", strategy = GenerationType.AUTO)
private Long id;
#Column(name = "CAMPUS_NAME")
private String campusName;
#Column(name = "CAMPUS_ID", nullable = false)
private Long campusId;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "GROUP_ID")
private GroupType groupType;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "REGION_ID")
private Region region;
....
...
}
#Entity
#EntityListeners(AuditListener.class)
#Table(name = "TABLE_GROUP_TYPE")
public class GroupType implements Auditable {
#Id
#Column(name = "GROUP_TYPE_ID")
#SequenceGenerator(name = "GROUP_TYPE_SEQUENCE", sequenceName = "GROUP_TYPE_ID")
#GeneratedValue(generator = "GROUP_TYPE_SEQUENCE", strategy = GenerationType.AUTO)
protected Long id;
#Column(name = "GROUP_TYPE_NAME", nullable = false)
protected String groupTypeName;
....
...
}
#Entity
#EntityListeners(AuditListener.class)
#Table(name = "TABLE_REGION")
public class Region implements Auditable {
#Id
#Column(name = "region_id")
#SequenceGenerator(name = "REGION_ID", sequenceName = "REGION_ID")
#GeneratedValue(generator = "REGION_ID", strategy = GenerationType.AUTO)
private Long id;
#Column(name = "REGION_NAME", nullable = false)
private String name;
...
..
}
Any help is Appreciated.
I have 2 entities that use the same sequence as the primary key, how do I map?
Example:
#Entity
#Table("employeT")
public class Employe(){
#SequenceGenerator(name = "generator_id", sequenceName = "seq_id")
#GeneratedValue(generator = "generator_id")
#colunm(name = "id")
private Integer id;
#colunm(name = "nameEmp")
private String name;
#JoinColumn(name = "id")
private Computer computer;
}
#Entity
#Table("computerT")
public class Computer(){
#SequenceGenerator(name = "generator_id", sequenceName = "seq_id")
#GeneratedValue(generator = "generator_id")
#colunm(name = "id")
private Integer id;
#colunm(name="name_computer")
private String nameComputer;
}
I need save employe and computer with same id, generated by Employe save.
There are three things to do with your code to work the way to want to.
Add #OneToOne annotation to indicate that Employee and Computer are in relation.
Delete information about #SequenceGenerator from your Computer entity and add #Id annotation
Add #MapsId annotation. [More info]
So it would look something like this :
#Entity
#Table("employeT")
public class Employe(){
#Id
private Integer id;
#Colunm(name = "nameEmp")
private String name;
#OneToOne
#JoinColumn(name = "computer_id")
#MapsId
private Computer computer;
}
Why?
#OneToOne annotation indicates relation between entities.
#SequenceGenerator is redudant since we "copy" id from Computer entity.
#Id annotation is mandatory to indicate that this field is our primary key.
Last but not least, #MapsId annotation do the magic, where it 'borrows' id from relation.
More info in the link I attached earlier.
I have a Table (for a long time ago), call it TABLE_A, and I have an entity class for this Table:
#Entity
#Table(name = "TABLE_A")
public class TableA implements Serializable {
#Id
#Basic(optional = false)
#Column(name = "ID")
//what else should I write here, to get the value from the existing sequence (seq_table_a_id) from database?
private Long id;
#Basic(optional = false)
#Column(name = "VALID_TO_DT")
private String name;
getters/setters...
}
I had created a sequence for this table in ORACLE a long time ago, and I want to give values for the new item's ID from this sequence. How should I write this code in java entity with annotations? If you could write an example for my code, that would be helpful!
And should I write anything else maybe in the persistance.xml?
The name of the existing sequence is: seq_table_a_id
You should check the annotation #GeneratedValue and #SequenceGenerator
#Id
#GeneratedValue(generator="seqGen")
#SequenceGenerator(name="seqGen",sequenceName="seq_table_a_id", allocationSize=1)
private Long id;
Check this link
I am using Hibernate(3.0) + Oracle DB (10g) for my application.
My domain object are like PluginConfig.java
#Entity
#Table(name = "plugin_config" , schema = "test")
#org.hibernate.annotations.Cache(usage =org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
public class Config {
private static final long serialVersionUID = -1959019321092627830L;
/** This object's id */
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
#OneToOne
#JoinColumn(name = "plugin_id")
private Plugin plugin;
#Column(name = "config_name")
#NaturalId(mutable = true)
private String name;
#Column(name = "config_desc")
private String description;
another domain object Plugin.java
#Entity
#Table(name = "plugin", schema = "test")
#org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
public class Plugin implements Serializable {
private static final long serialVersionUID = 5694761325202724778L;
/** This object's id */
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
#Column(name = "plugin_name")
#NaturalId
private String pluginName;
#OneToOne
#JoinColumn(name = "plugin_config_id")
private PluginConfig pluginConfig;
#Column(name = "plugin_desc")
private String description;
Whenever i try to load Plugin via my database service method(using #Transactional annotation and hence it loads all its children as well) i get the following error
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [PluginConfig#53]
There is actually no row in plugin_config table with id = 53.
And also no plugin table entry has plugin_config_id=53.
So from where is hibernate picking these values ?
I noticed one thing here, the value "53" is actually the row number from the plugin_config table which should have been reffered.
See the below image:
This is the plugin config that my query should be looking for. But it tries to search it with identifier #53(Row no:) instead of the #95(value of the primary key "id").
Where can i be going wrong with this ?
I don't know if this is teh best solution, but you can use #NotFound annotation with IGNORE command to let Hibernate discard exception adn set pluginConfig to null.
#OneToOne
#JoinColumn(name = "plugin_config_id")
#NotFound(action = NotFoundAction.IGNORE)
private PluginConfig pluginConfig;