我的问题与 ***或 相同。 我需要人工确定一种不履约的自动生成价值(why? 进口旧数据)。 如? 采用Hibernate s entity = em.joint(entity)
,将作trick。
遗憾的是,我没有这样做。 我既无错误,也无其他警告。 该实体只是,不会出现在数据库上。 I m, Us Spring and Hibernate entitiesManager 3.5.3-Final.
任何想法?
我的问题与 ***或 相同。 我需要人工确定一种不履约的自动生成价值(why? 进口旧数据)。 如? 采用Hibernate s entity = em.joint(entity)
,将作trick。
遗憾的是,我没有这样做。 我既无错误,也无其他警告。 该实体只是,不会出现在数据库上。 I m, Us Spring and Hibernate entitiesManager 3.5.3-Final.
任何想法?
它以以下法典执行我的项目:
@XmlAttribute
@Id
@Basic(optional = false)
@GeneratedValue(strategy=GenerationType.IDENTITY, generator="IdOrGenerated")
@GenericGenerator(name="IdOrGenerated",
strategy="....UseIdOrGenerate"
)
@Column(name = "ID", nullable = false)
private Integer id;
以及
import org.hibernate.id.IdentityGenerator;
...
public class UseIdOrGenerate extends IdentityGenerator {
private static final Logger log = Logger.getLogger(UseIdOrGenerate.class.getName());
@Override
public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
if (obj == null) throw new HibernateException(new NullPointerException()) ;
if ((((EntityWithId) obj).getId()) == null) {
Serializable id = super.generate(session, obj) ;
return id;
} else {
return ((EntityWithId) obj).getId();
}
}
where you basically define your own ID generator (based on the Identity strategy), 以及if the ID is not set, you delegate the generation to the default generator.
主要的反馈是,它把你捆绑在解放党的提供者面前......但与我的我的我的我的我的我的我的我的我的我的我的我的我的我的我的我的我的后勤项目完全合作。
另一种执行方式更简单。
后者使用 两种“ 通知”或基于xml的配置:它依靠hibernate meta-data获得该物体的贴现值。 对演习的答复(按要求采用评分模式),没有进行真正的测试:public class UseExistingOrGenerateIdGenerator extends SequenceGenerator {
@Override
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
Serializable id = session.getEntityPersister(null, object)
.getClassMetadata().getIdentifier(object, session);
return id != null ? id : super.generate(session, object);
}
}
public class UseExistingOrGenerateIdGenerator implements IdentifierGenerator, Configurable {
private IdentifierGenerator defaultGenerator;
@Override
public void configure(Type type, Properties params, Dialect d)
throws MappingException;
// For example: take a class name and create an instance
this.defaultGenerator = buildGeneratorFromParams(
params.getProperty("default"));
}
@Override
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
Serializable id = session.getEntityPersister(null, object)
.getClassMetadata().getIdentifier(object, session);
return id != null ? id : defaultGenerator.generate(session, object);
}
}
更新Laurent Grégoire 解冻原5.2 ,因为它似乎已经改变。
public class UseExistingIdOtherwiseGenerateUsingIdentity extends IdentityGenerator {
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
Serializable id = session.getEntityPersister(null, object).getClassMetadata().getIdentifier(object, session);
return id != null ? id : super.generate(session, object);
}
}
(替代包装名称)
@Id
@GenericGenerator(name = "UseExistingIdOtherwiseGenerateUsingIdentity", strategy = "{package}.UseExistingIdOtherwiseGenerateUsingIdentity")
@GeneratedValue(generator = "UseExistingIdOtherwiseGenerateUsingIdentity")
@Column(unique = true, nullable = false)
protected Integer id;
I`m giving a solution here that worked for me:
create your own identifiergenerator/sequencegenerator
public class FilterIdentifierGenerator extends IdentityGenerator implements IdentifierGenerator{
@Override
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
// TODO Auto-generated method stub
Serializable id = session.getEntityPersister(null, object)
.getClassMetadata().getIdentifier(object, session);
return id != null ? id : super.generate(session, object);
}
}
更改以下实体:
@Id
@GeneratedValue(generator="myGenerator")
@GenericGenerator(name="myGenerator", strategy="package.FilterIdentifierGenerator")
@Column(unique=true, nullable=false)
private int id;
...
并同时避免使用<代码>persist(>>)
如果您正在使用hibernate s org.hibernate.id.UUIDGenerator
,以形成一种强硬,我建议:
public class UseIdOrGenerate extends UUIDGenerator {
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
Serializable id = session.getEntityPersister(null, object).getClassMetadata().getIdentifier(object, session);
return id != null ? id : super.generate(session, object);
}
}
根据。 选择性地产生新的ID 深植于解放论坛,joint(<>/code> 可能不是解决办法(至少是单独的解决办法),而你可能必须使用。 (这是你所显示的第二个环节)。
我对我本人进行了考验,因此我可以确认,但我建议阅读解放党论坛的座右铭。
对于那些想这样做的人来说,以上只是工作。 只是建议从物体中获取识别信息,而不是每个实体类别(Id的情况)拥有遗产,因此,你可以做以下事情:
import org.hibernate.id.IdentityGenerator;
public class UseIdOrGenerate extends IdentityGenerator {
private static final Logger log = Logger.getLogger(UseIdOrGenerate.class
.getName());
@Override
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
if (object == null)
throw new HibernateException(new NullPointerException());
for (Field field : object.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(Id.class)
&& field.isAnnotationPresent(GeneratedValue.class)) {
boolean isAccessible = field.isAccessible();
try {
field.setAccessible(true);
Object obj = field.get(object);
field.setAccessible(isAccessible);
if (obj != null) {
if (Integer.class.isAssignableFrom(obj.getClass())) {
if (((Integer) obj) > 0) {
return (Serializable) obj;
}
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return super.generate(session, object);
}
}
你们需要一笔交易。
3. 如果您的交易是人工管理的:
entityManager.getTransaction().begin();
(当然不会忘记作出承诺)
如果你使用申报性交易,则使用适当的声明(通过说明,很可能)
另外,在您的记录4j.properties中将hibernate伐木水平设定为debug
(
log4j.logger.org.hibernate=debug
),以便追踪更多细节中正在发生的情况。
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...