Автоматический первичный ключ JPA генерирует

Уже существует foreach оператор, включенный в язык, который делает задание большую часть времени.

я не хотел бы видеть следующее:

list.ForEach( item =>
{
    item.DoSomething();
} );

Вместо:

foreach(Item item in list)
{
     item.DoSomething();
}

последний более ясен и легче читать в большей части ситуации , хотя, возможно, немного дольше ввести.

Однако я должен признать, что изменил свою позицию по той проблеме; ForEach () дополнительный метод действительно был бы полезен в некоторых ситуациях.

Вот существенные различия между оператором и методом:

  • проверка Типа: foreach сделан во времени выполнения, ForEach () во время компиляции (Большой Плюс!)
  • синтаксис для вызова делегата действительно намного более прост: объекты. ForEach (DoSomething);
  • ForEach () мог быть объединен в цепочку: хотя злой / полноценность такой функции открыто для обсуждения.

Те - все большие точки, сделанные многими людьми здесь, и я вижу, почему люди пропускают функцию. Я не возражал бы против Microsoft, добавляющей стандартный метод ForEach в следующем повторении платформы.

17
задан Pascal Thivent 30 April 2010 в 19:56
поделиться

1 ответ

The @GeneratedValue(strategy=GenerationType.TABLE) tells the JPA provider to use a table to get IDs from when inserting newly created entities into the database.

When using Hibernate as provider, this will result in a table hibernate_sequences which has two columns: the entity name, and the max identity already assigned to this entity. Here, it seems Hibernate doesn't succeed to get the next ID from it for your entity but it's hard to say exactly why because you didn't provide enough informations for that.

So, could you please provide the full stacktrace? Also, please turn logging with hibernate.show_sql property set to true and set the proper log level log4j.logger.org.hibernate.SQL=DEBUG. Join the log to your question if possible.

Maybe just check that you did configure the correct hibernate.dialect for Oracle. Actually, join your hibernate configuration too if possible.

PS: The "traditional" way to generate PK with Oracle is to use sequences (you could let Hibernate guess the best strategy for your database type using GenerationType.AUTO or force it using SEQUENCE) but I'll assume you want the resultant data structure be database agnostic. If not, I'd suggest to go for sequences instead.

EDIT: Answering a comment from the OP about GenerationType.AUTO. Indeed, the default is a single global sequence called hibernate_sequence and this might be a problem. But, with the setup shown below, you can use GenerationType.AUTO and still control the name of the sequence for the cases where the database uses sequences:

@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
private long id;

In other words, you can use use a different sequence name for each table without loosing portability.

32
ответ дан 30 November 2019 в 11:17
поделиться
Другие вопросы по тегам:

Похожие вопросы: